duankangzi766767 2015-02-16 05:37
浏览 86
已采纳

如何使用echo php获取数组中的输出

Hi in the below I am printing the group name using echo function.But I want to return the echo message in the form of array.Because these echo message I am reading in client side. for example my output coming like this:

NewGroup-->New is one groupname and Group is the second groupname

Excepted output:

{New},{Group}

php

case "DispalyGroupDetails" :
    $userId = authenticateUser($db, $username, $password);

    if ($userId != NULL) {

        if (isset($_REQUEST['username'])) {
            $username = $_REQUEST['username'];

            $sql = "select Id from users where username='$username' limit 1";

            if ($result = $db -> query($sql)) {
                if ($row = $db -> fetchObject($result)) {

                    $sql = "SELECT g.id,g.groupname
                            FROM `users` u, `friends` f, `group` g
                            WHERE u.Id=f.providerId and                   
                            f.providerId=g.providerId
                            GROUP BY g.id, g.groupname";

                    $theResult = $db -> query($sql);

                    if ($theResult) {

                        while ($theRow = $db -> fetchObject($theResult)) {

                            echo $theRow -> groupname;

                        }
                        $out = SUCCESSFUL;
                    } else {
                        $out = FAILED;
                    }

                } else {
                    $out = FAILED;
                }
            } else {
                $out = FAILED;
            }
        } else {
            $out = FAILED;
        }
    } else {
        $out = FAILED;
    }
    break;
  • 写回答

1条回答 默认 最新

  • doujie3888 2015-02-16 06:18
    关注

    Add the group name into an array, then json_encode($array);.

    NOTE: this code is not tested as I do not have your database.

    like this:

     $userId = authenticateUser($db, $username, $password);
     $array = array();
        if ($userId != NULL) {
    
            if (isset($_REQUEST['username'])) {               
                 $username = $_REQUEST['username'];
                 $sql = "select Id from users where username='$username' limit 1";
    
                 if ($result = $db->query($sql)) {
                        if ($row = $db->fetchObject($result)) {    
                            $sql = "SELECT g.id,g.groupname FROM `users` u, `friends` f, `group` g WHERE u.Id=f.providerId and f.providerId=g.providerId GROUP BY g.id, g.groupname";
                            $theResult = $db->query($sql);
                            if ($theResult) {
                        $count = 0;
                        while( $theRow = $db->fetchObject($theResult)) {  
                            $array[$count] = $theRow->groupname;
                            $count++;
                        }
                            $out = SUCCESSFUL;
                            echo json_encode($array);
                            } else {
                                $out = FAILED;
                            }
    
                        } else {
                            $out = FAILED;                      
                        }
                 } else {
                        $out = FAILED;
                 }              
            } else {
                    $out = FAILED;
            }           
        }
        else {
            $out = FAILED;
        }   
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 多台相同IP产品如何组网
  • ¥30 关于php页面调取、更新数据提交刷新等问题
  • ¥50 使用 java+selenium 接管手动打开的IE浏览器
  • ¥15 ios 新安装app收不到fcm推送
  • ¥15 有没有实力的写手?有过成品的优先
  • ¥15 图像信息库的建立与识别
  • ¥15 韩国网站购物,KG支付的支付回调如何解决
  • ¥15 workstation导入ovf文件,报错,怎么解决呢?
  • ¥15 关于#c语言#的问题:构成555单稳态触发器,采用LED指示灯延时时间,对延时时间进行测量并显示(如楼道声控延时灯)需要Proteus仿真图和C语言代码
  • ¥15 workstation加载centos进入emergency模式,查看日志报警如图,怎样解决呢?