php服务器端返回一组数据

<?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];
            }
        }
    }
}










评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值