· 9 years ago · Dec 05, 2016, 04:52 AM
1<?php
2
3$host = "host";
4$db = "db";
5$user = "user";
6$pass = "pass";
7
8$connection = mysql_connect($host, $user, $pass);
9
10if(!$connection)
11{
12 die("Database server connection failed.");
13}
14else
15{
16 $dbconnect = mysql_select_db($db, $connection);
17
18 if(!$dbconnect)
19 {
20 die("Unable to connect to the specified database!");
21 }
22 else
23 {
24 $query = "SELECT * FROM tests";
25 $resultset = mysql_query($query, $connection);
26
27 $records = array();
28
29 while($r = mysql_fetch_assoc($resultset))
30 {
31 $records[] = $r;
32 }
33
34 echo json_encode($records);
35 }
36}
37
38?>
39
40CREATE TABLE IF NOT EXISTS `tests` (
41`id` int(11) NOT NULL AUTO_INCREMENT,
42`testName` varchar(255) DEFAULT NULL,
43`testPop` int(11) DEFAULT NULL,
44PRIMARY KEY (`id`)
45) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=13 ;
46
47INSERT INTO `tests` (`id`, `testName`, `testPop`) VALUES
48(1, 'Test 1', '0'),
49(2, 'Test 2', '0'),
50(3, 'Test 3', '0'),
51(4, 'Test 4', '0'),
52(5, 'Test 5', '0'),
53(6, 'Test 6', '0'),
54(7, 'Test 7', '0'),
55(8, 'Test 8', '0'),
56(9, 'Test 9', '0'),
57(10, 'Test 10', '0'),
58(11, 'Test 11', '0'),
59(12, 'Test 12', '0');
60
61func retrieveData() {
62
63 let getDataURL = "http://exampleip.org/tests.php"
64 let url: NSURL = NSURL(string: getDataURL)!
65
66 do {
67
68 let data: Data = try Data(contentsOf: url as URL)
69 jsonArray = try JSONSerialization.jsonObject(with: data, options: .mutableContainers) as! NSMutableArray
70
71 // Looping through jsonArray
72 for i in 0..<jsonArray.count {
73
74 // Create Test Object
75 let tID: String = (jsonArray[i] as AnyObject).object(forKey: "id") as! String
76 let tName: String = (jsonArray[i] as AnyObject).object(forKey: "testName") as! String
77 let tPop: String = (jsonArray[i] as AnyObject).object(forKey: "testPop") as! String
78
79 // Add Test Objects to Test Array
80 testArray.append(Test(testName: tName, andTestPop: tPop, andTestID: tID))
81
82 }
83 }
84 catch {
85 print("Error: (Retrieving Data)")
86 }
87
88 myTableView.reloadData()
89}
90
91@IBAction func addPointButtonClick(_ sender: UIButton!) {
92
93 // Adding row to tag
94 let buttonPosition = (sender as AnyObject).convert(CGPoint.zero, to: self.myTableView)
95 if let indexPath = self.myTableView.indexPathForRow(at: buttonPosition) {
96
97 // Change pointAdd to addedPoint Image
98 (sender as UIButton).setImage(UIImage(named: "pointAdd.png")!, for: .normal)
99 cell.pointAddButton.isHidden = true
100 cell.addedPointButton.isHidden = false
101
102 self.myTableView.beginUpdates()
103
104 // ----- Inserting Cell to Section 0 -----
105 addedPointsArray.insert(testArray[indexPath.row], at: 0)
106 myTableView.insertRows(at: [IndexPath(row: 0, section: 0)], with: .fade)
107
108 // ----- Removing Cell from Section 1 -----
109 testArray.remove(at: indexPath.row)
110 let rowToRemove = indexPath.row
111 self.myTableView.deleteRows(at: [IndexPath(row: rowToRemove, section: 1)], with: .fade)
112
113 self.myTableView.endUpdates()
114
115 }
116
117class Test: NSObject {
118
119//Strings
120var testName: String?
121var testID: String?
122var testPop: String?
123
124override init() {
125
126}
127
128//Converting Strings into Objects
129init(testName tName: String,
130 andTestPop tPop: String,
131 andTestID tID: String)
132{
133
134 super.init()
135
136 self.testName = tName
137 self.testPop = tPop
138 self.testID = tID
139
140 }
141}
142
143<?php
144
145$host = "host";
146$db = "db";
147$user = "user";
148$pass = "pass";
149
150$connection = mysql_connect($host,$user,$pass);
151
152// Posting into MySQL Object
153$testPop = $_POST["a"];
154
155// Checking if connection can be established
156if(!$connection){
157 die("Connection Failed");
158}
159else
160{
161 // Selecting Database
162 $dbconnect = mysql_select_db($db, $connection);
163
164 // Check if it can connect to Database
165 if(!$dbconnect){
166 die("Unable to connect to Database");
167 }
168 else
169 {
170 $query = "INSERT INTO $db.`tests` (`testPop`)
171 VALUES (`$testPop`);
172
173 mysql_query($query, $connection) or die(mysql_error());
174
175 echo "Successfully added";
176 echo $query;
177 }
178}
179
180?>
181
182print("Current Pop: (testObject.testPop!)")
183 let popValue = testObject.value(forKey: "testPop") as? String ?? ""
184 let updatedPopValue = Int(popValue)! + 1
185 print("Updated Pop: (updatedPopValue)")