php服務器端返回一組數據



// 該文件返回雙方的聊天記錄

    
    
require 'Class_DBOperation.php';
require 'globle.php';
//聯繫人對象,屬性與客戶端一致    
class Contact
{
    public  $sender;
    public  $receiver;
    public  $senddate;
    public  $sendcontent;
    
    function Contact($sender,$receiver,$senddate,$sendcontent)
    {
        $this->sender = $sender;
        $this->receiver = $receiver;
        $this->senddate  = $senddate;
        $this->sendcontent =$sendcontent;
    }
}
////接受客戶端的字段值,來查詢表    
$senderVar = $_POST['SENDER'];
$receiverVar = $_POST['RECEIVER'];
$contentVar = $_POST['SENDCONTENT'];;
$dbOperation = new class_DBOperation(DBHOST,DBUSER,DBPWD,DBNAME,DBCHARSET);
//   
$sql = "select * from chat where (SENDER ='$senderVar' and RECEIVER='$receiverVar') or (SENDER='$receiverVar' and RECEIVER='$senderVar') order by SENDDATE";     
$result = $dbOperation->query($sql);
    
//    $a = json_encode(array("aa"=>"22222"));
//    echo  $a;
$contacts = array();
while($contact = mysql_fetch_assoc($result))
{
    //$contact = new Contact($row->SENDER,$row-RECEIVER,$row->SENDDATE,$row->SENDCONTENT);
    $contacts[] = array("contact"=>$contact);
}
echo json_encode($contacts);    
    
$dbOperation->closeConnect();    
?>


客戶端接收:

  NSString  *responseString = [request responseString];
    
    NSLog(@"xxx:%@",responseString);
    
NSData *jsonData = [responseString dataUsingEncoding:NSUTF8StringEncoding];
    NSDictionary *dictionary = [[CJSONDeserializer deserializer] deserializeAsDictionary:jsonData error:nil];
    NSLog(@"count:%d",[dictionary count]);
//    
    NSArray *rounds = [responseString JSONValue];
    NSLog(@"pppp:%d",[rounds count]);
//    
    for(NSDictionary *item in rounds)
    {
        NSDictionary * info = [item objectForKey:@"contact"];
        //獲取字段的值
        NSString *sender = [info objectForKey:@"SENDER"];
        NSString *sendedate = [info objectForKey:@"SENDDATE"];
        NSString *sendcontent = [info objectForKey:@"SENDCONTENT"];
        
        //獲取消息內容
        ChatInfo *chatInfo = [[ChatInfo alloc] init];
        chatInfo.sender = sender;
        chatInfo.senddate = sendedate;
        chatInfo.sendcontent = sendcontent;
        
        [self.chatInfos addObject:chatInfo];
        [chatInfo release];
        
    }



另一個例子:

服務器端:

<?php

require 'Class_DBOperation.php';
require 'globle.php';
    // 檢查某一用戶是否爲有我用戶


$dbOperation = new class_DBOperation(DBHOST,DBUSER,DBPWD,DBNAME,DBCHARSET);

//Ω” ’øÕªß∂À≤Œ ˝
$user_pwd = $_POST['pwd'];//$_POST['pwd'];

$sqlCheckName = "select password from user ";
$checkResult = $dbOperation->query($sqlCheckName);
    
$yoWoContacts = array();
while($contact = mysql_fetch_assoc($checkResult))
{
    //$contact = new Contact($row->SENDER,$row-RECEIVER,$row->SENDDATE,$row->SENDCONTENT);
    $yoWoContacts[] = $contact;
}
echo json_encode($yoWoContacts);       
?>
客戶端:

-(void)requestFinished:(ASIHTTPRequest *)request
{
    NSString *responseString = [request responseString];
    NSArray *contacts = [responseString JSONValue];
    //在這裏檢測誰是有我聯繫人
    for(NSDictionary *youWoContact in contacts )
    {
        NSString *password = [youWoContact objectForKey:@"password"];
        
        for(Contact *contact  in [AppConfigure sharedConfigure].allContactsOfUser)
        {
            if([contact.contactTeleNum isEqualToString:password])
            {
                contact.contactType = kYOU_WO_CONTACT;
                [[AppConfigure sharedConfigure].youWoContactsOfUser addObject:contact];
            }
        }
    }
}










發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章