如何填充表视图取决于用户输入


How to fill table view depend on user input

是否有任何方法来填写我的表视图取决于用户的输入?例如:用户有一个文本,无论他在该文本中输入什么,如果该文本与mySql中的相同数据匹配,则结果显示在我的表视图中。

在我使用这个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