php - 如何根据用户输入填充表格 View

标签 php mysql xcode uitableview

有没有办法根据用户输入填充我的表格 View ?例如:用户有一个文本,无论他在该文本中输入什么,如果该文本与 mySql 中的相同数据匹配,那么结果将显示在我的 TableView 中。

目前我正在使用这个 php 代码:

<?php

$host="localhost"; // Host name 
$username="UserName"; // Mysql username 
$password="PassWord"; // Mysql password 
$db_name="DataBase"; // Database name 
$tbl_name="Notes"; // Table name 

// Connect to server and select databse.
mysql_connect("$host", "$username", "$password")or die("cannot connect"); 
mysql_select_db("$db_name")or die("cannot select DB");




// To protect MySQL injection (more detail about MySQL injection)
$mytitle = stripslashes($mytitle);
$mytitle = mysql_real_escape_string($mytitle);


$mytitle = $_POST['title'];
$sql="SELECT * FROM $tbl_name WHERE title = '$mytitle'";

 echo "Data found but not loaded";


$result=mysql_query($sql);

// Mysql_num_row is counting table row
$count=mysql_num_rows($result);

// If result matched $myusername and $mypassword, table row must be 1 row


if ($count==1){
   echo "Success";
} else {
   echo "No data matching found";
}


?>


And mu Xcode code is:


NSInteger success = 0;
    @try {

        if([[self.lbOne text] isEqualToString:@""] ) {

            [self alertStatus:@"lbOne is empty" :@"Failed to Search" :0];

        } else {
            NSString *post =[[NSString alloc] initWithFormat:@"title=%@",[self.lbOne text]];
            NSLog(@"PostData: %@",post);

            NSURL *url=[NSURL URLWithString:@"http://MyDomain/ReadNotesByTitle.php"];

            NSData *postData = [post dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES];

            NSString *postLength = [NSString stringWithFormat:@"%lu", (unsigned long)[postData length]];

            NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
            [request setURL:url];
            [request setHTTPMethod:@"POST"];
            [request setValue:postLength forHTTPHeaderField:@"Content-Length"];
            [request setValue:@"application/json" forHTTPHeaderField:@"Accept"];
            [request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];
            [request setHTTPBody:postData];

            //[NSURLRequest setAllowsAnyHTTPSCertificate:YES forHost:[url host]];

            NSError *error = [[NSError alloc] init];
            NSHTTPURLResponse *response = nil;
            NSData *urlData=[NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];

            NSLog(@"Response code: %ld", (long)[response statusCode]);

            if ([response statusCode] >= 200 && [response statusCode] < 300)
            {
                NSString *responseData = [[NSString alloc]initWithData:urlData encoding:NSUTF8StringEncoding];
                NSLog(@"Response ==> %@", responseData);


                if([responseData isEqualToString:@"Success"])
                {

                    [self alertStatus:@"Search Success" :@"There are some result" :0];

                    NSURL *url = [NSURL URLWithString:getDataBySearch];
                    NSData *data = [NSData dataWithContentsOfURL:url];
                    jasonArray = [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:nil];


                    listArray = [[NSMutableArray alloc]init];
                    for (int i = 0; i < jasonArray.count; i++) {

                        NSString *cUserName = [[jasonArray objectAtIndex:i]objectForKey:@"userName"];
                        NSString *cTitle = [[jasonArray objectAtIndex:i]objectForKey:@"title"];
                        NSString *cComments = [[jasonArray objectAtIndex:i]objectForKey:@"comments"];
                        NSString *cTimeC = [[jasonArray objectAtIndex:i]objectForKey:@"commentsTime"];
                        NSString *cDateC = [[jasonArray objectAtIndex:i]objectForKey:@"commentsDate"];




                        [listArray addObject:[[ListOfObjects alloc]initWithUserName:cUserName andTitle:cTitle andComments:cComments andtimeC:cTimeC andDateC:cDateC]];
                    }





                    [self.tableView reloadData];


                } else {

                    [self alertStatus:@"No result Found" :@"Search Failed" :0];

                }

            } else {
                //if (error) NSLog(@"Error: %@", error);
                [self alertStatus:@"Check Connection" :@"Search Failed" :0];
            }
        }
    }
    @catch (NSException * e) {
        NSLog(@"Exception: %@", e);
        [self alertStatus:@"Search Failed" :@"Error" :0];
    }
    if (success) {

        NSLog(@"Data match , but not loaded");


    }

这段代码一直显示没有匹配结果,我确信有相同的数据。

有什么办法可以做到吗?

谢谢大佬

最佳答案

您尝试过使用 switch 语句吗? http://www.w3schools.com/php/php_switch.asp

我一直在使用它来改变 html 基于数据的显示方式。当我使用它时,它用于下拉菜单。所以,很容易就有案子了。然后我只是根据案例编号回显 html。

另一种可能性是根据查询创建一个变量,然后在变量之间进行比较。 http://php.net/manual/en/control-structures.if.php

关于php - 如何根据用户输入填充表格 View ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24974331/

相关文章:

c# - 在 PHP/Bash/C# 中从 PDF 中删除图层/背景#

javascript - 通过 Ajax 的 MYSQL 数据库到 Highcharts 图表

iphone - 如何在 xcode 中创建模板子类文件?

swift - "Extensions may not contain stored properties"除非你是苹果?我错过了什么?

mysql - 将新表与旧表连接,更新不匹配的 ID

objective-c - 让微调器在加载 View 后立即开始

php - 正则表达式数组与字符串问题

Php:数组嵌套循环性能

mysql - 尝试将一个表插入另一个表时出现问题

mysql - Gorm处理HasOne关系