Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- $host = "host";
- $db = "db";
- $user = "user";
- $pass = "pass";
- $connection = mysql_connect($host, $user, $pass);
- if(!$connection)
- {
- die("Database server connection failed.");
- }
- else
- {
- $dbconnect = mysql_select_db($db, $connection);
- if(!$dbconnect)
- {
- die("Unable to connect to the specified database!");
- }
- else
- {
- $query = "SELECT * FROM tests";
- $resultset = mysql_query($query, $connection);
- $records = array();
- while($r = mysql_fetch_assoc($resultset))
- {
- $records[] = $r;
- }
- echo json_encode($records);
- }
- }
- ?>
- CREATE TABLE IF NOT EXISTS `tests` (
- `id` int(11) NOT NULL AUTO_INCREMENT,
- `testName` varchar(255) DEFAULT NULL,
- `testPop` int(11) DEFAULT NULL,
- PRIMARY KEY (`id`)
- ) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=13 ;
- INSERT INTO `tests` (`id`, `testName`, `testPop`) VALUES
- (1, 'Test 1', '0'),
- (2, 'Test 2', '0'),
- (3, 'Test 3', '0'),
- (4, 'Test 4', '0'),
- (5, 'Test 5', '0'),
- (6, 'Test 6', '0'),
- (7, 'Test 7', '0'),
- (8, 'Test 8', '0'),
- (9, 'Test 9', '0'),
- (10, 'Test 10', '0'),
- (11, 'Test 11', '0'),
- (12, 'Test 12', '0');
- func retrieveData() {
- let getDataURL = "http://exampleip.org/tests.php"
- let url: NSURL = NSURL(string: getDataURL)!
- do {
- let data: Data = try Data(contentsOf: url as URL)
- jsonArray = try JSONSerialization.jsonObject(with: data, options: .mutableContainers) as! NSMutableArray
- // Looping through jsonArray
- for i in 0..<jsonArray.count {
- // Create Test Object
- let tID: String = (jsonArray[i] as AnyObject).object(forKey: "id") as! String
- let tName: String = (jsonArray[i] as AnyObject).object(forKey: "testName") as! String
- let tPop: String = (jsonArray[i] as AnyObject).object(forKey: "testPop") as! String
- // Add Test Objects to Test Array
- testArray.append(Test(testName: tName, andTestPop: tPop, andTestID: tID))
- }
- }
- catch {
- print("Error: (Retrieving Data)")
- }
- myTableView.reloadData()
- }
- @IBAction func addPointButtonClick(_ sender: UIButton!) {
- // Adding row to tag
- let buttonPosition = (sender as AnyObject).convert(CGPoint.zero, to: self.myTableView)
- if let indexPath = self.myTableView.indexPathForRow(at: buttonPosition) {
- // Change pointAdd to addedPoint Image
- (sender as UIButton).setImage(UIImage(named: "pointAdd.png")!, for: .normal)
- cell.pointAddButton.isHidden = true
- cell.addedPointButton.isHidden = false
- self.myTableView.beginUpdates()
- // ----- Inserting Cell to Section 0 -----
- addedPointsArray.insert(testArray[indexPath.row], at: 0)
- myTableView.insertRows(at: [IndexPath(row: 0, section: 0)], with: .fade)
- // ----- Removing Cell from Section 1 -----
- testArray.remove(at: indexPath.row)
- let rowToRemove = indexPath.row
- self.myTableView.deleteRows(at: [IndexPath(row: rowToRemove, section: 1)], with: .fade)
- self.myTableView.endUpdates()
- }
- class Test: NSObject {
- //Strings
- var testName: String?
- var testID: String?
- var testPop: String?
- override init() {
- }
- //Converting Strings into Objects
- init(testName tName: String,
- andTestPop tPop: String,
- andTestID tID: String)
- {
- super.init()
- self.testName = tName
- self.testPop = tPop
- self.testID = tID
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement