我需要在我的应用程序流程中正确处理标签。我怎样才能做到这一点?步骤是
- 输入:纯文本标签
- 转换为小写
- 删除空格
- 发送到 PHP 服务器
- 在 SQL 中赋值给 $value
- 向服务器的数据库提交请求
- 输出:将结果(以 JSON 格式)返回到我的应用程序
目前的设计有11组,我根据点击的页面发送到不同的网址;我想扩展到 102 个组。
我认为可以使用 nsurlsession 发布我的 label.text(组名),将其发送到我的服务器,将其插入我的 SQL 代码,然后接收搜索结果。
// Toggle View Button (on button click open next view controller and populate table view with group matching data)
- (IBAction)ShowModels:(id)sender {
// 1. URL of the server page i want to send data to.
NSURL *url = [NSURL URLWithString:@"http://www.website.com/group.php"];
// 2. some how adapt this code to send grouplabel.text in plain text in lowercase without spaces.
NSDictionary *params = @{@"tableName": _groupLabel.text};
// 3. Adapt code to post the value of grouplabel.text to server.
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
[request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];
[request setHTTPMethod:@"POST"];
// 4. use a task to upload the data to the server and then have another task to recieve the completed data.
NSURLSessionDataTask *task = [[NSURLSession sharedSession] dataTaskWithRequest:request
completionHandler:^(NSData *data,
NSURLResponse *response,
NSError *error)
{
if (!error)
{
NSLog(@"Status code: %li", (long)((NSHTTPURLResponse *)response).statusCode);
}
else
{
NSLog(@"Error: %@", error.localizedDescription);
}
}];
// 5. Run the completed task.
[task resume];
}
在这些阶段我需要改变什么?
服务器端代码:
<?php
//connect to database.
include '../Includes/db-connect.php';
//set the table name variable
$table = "name"; //attach label.text value to this...
//Select all distinct values from table name.
$sql = "SELECT DISTINCT make FROM $table";
$result = mysqli_query($conn, $sql) or die ("Error in selecting" . mysqli_error($conn));
//create an array
$emparray = array();
while($row =mysqli_fetch_assoc($result))
{
$emparray[] = $row;
}
//echo json_encode($emparray); //TESTING.. implement to check response from query.
//Write to json file
$fp = fopen('name.json', 'w');
fwrite($fp, json_encode($emparray));
fclose($fp);
// Close connection.
mysqli_close($conn);
?>