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 nginx中的CORS策略应该如何配置
  • ¥30 信号与系统实验:采样定理分析
  • ¥100 我想找人帮我写Python 的股票分析代码,有意请加mathtao
  • ¥20 Vite 打包的 Vue3 组件库,图标无法显示
  • ¥15 php 同步电商平台多个店铺增量订单和订单状态
  • ¥15 关于logstash转发日志时发生的部分内容丢失问题
  • ¥17 pro*C预编译“闪回查询”报错SCN不能识别
  • ¥15 微信会员卡接入微信支付商户号收款
  • ¥15 如何获取烟草零售终端数据
  • ¥15 数学建模招标中位数问题