的PHP / MySQL的-循环时..传递结果在数组?

银01

我的标题令人困惑..对此感到抱歉..无论如何..请看一下我的代码:

<?php
include('../connectDB.php'); //connect to db

echo '{ ';
    echo '"success": 1, ';
        echo '"result": [ ';
            $result = mysql_query("SELECT * FROM roominventory");
            while($row = mysql_fetch_array($result)) { //start while
                $getId = $row['id']; //get value
                $getRoomId = $row['room'];

                echo '{ ';
                    $ar = $row['arrival']; //assign value to variable
                    $dep = $row['departure'];

                    $date = str_replace('-', '/', $ar); //format the date
                    $formatArr =  date('m/d/Y', strtotime($date));

                    $date2 = str_replace('-', '/', $dep); //format the date
                    $formatDep =  date('m/d/Y', strtotime($date2));

                    $mSt = strtotime($formatArr) * 1000; //convert to milliseconds
                    $mEd = strtotime($formatDep) * 1000;

                    echo '"id": ' . '"' . $getId. '"' . ', ';

                    $resulta = mysql_query("SELECT * FROM rooms_amenities WHERE id='$getRoomId'");
                    while($rowa = mysql_fetch_array($resulta)) {
                        $getName = $rowa['name'];
                    }

                    echo '"title": ' . '"' . $getName . '"' . ', ';
                    echo '"url": ' . '"' . 'http://example.com' . '"' . ', ';
                    echo '"class": ' . '"' . 'event-warning' . '"' . ', ';
                    echo '"start": ' . '"' . $mSt . '"' . ', '; //echo the converted date
                    echo '"end": ' . '"' . $mEd . '" ';
                echo '} ';
                echo ', '; //comma (should only in between entries and not in the very end of the very last entry)
            } //end while
    echo '] ';
echo '}';
?>

现在,这是文件的结果:

{“成功”:1,“结果”:[{“ id”:“ 254”,“ title”:“ Standard Room 202”,“ url”:“ exampledotcom”,“ class”:“事件警告”,“开始”:“ 1470693600000”,“结束”:“ 1471384800000”}]}

没问题的。现在的问题是,当数据库中的表中有多于1行时,结果将如下所示:

{“成功”:1,“结果”:[{“ id”:“ 255”,“ title”:“ Standard Room 201”,“ url”:“ exampledotcom”,“ class”:“事件警告”,“开始”:“ 1471903200000”,“结束”:“ 1472076000000”},{“ id”:“ 256”,“标题”:“ Standard Room 202”,“ url”:“ exampledotcom”,“ class”:“ event-警告”,“开始”:“ 1471903200000”,“结束”:“ 1472076000000”},]}

notive的“逗号”的最后一项“14720.76亿”} ]}

我期望/期望的结果应该是这样的:

{“成功”:1,“结果”:[{“ id”:“ 255”,“ title”:“ Standard Room 201”,“ url”:“ exampledotcom”,“ class”:“事件警告”,“开始”:“ 1471903200000”,“结束”:“ 1472076000000”} {“ id”:“ 256”,“标题”:“ Standard Room 202”,“ url”:“ exampledotcom”,“ class”:“ event-警告”,“开始”:“ 1471903200000”,“结束”:“ 1472076000000”}]}

注意第一个条目之后和第二个条目{“ id:” ...},{“ id:” ...}之间的“逗号”,最后一个条目之后没有逗号。

我试图回声while循环结束时的逗号。但结果是逗号仅在最后一个条目处,中间没有逗号

如果到达最后一行,则最后一个条目不应有逗号。我不知道如何才能取得理想的结果。

还有其他方法/方式吗?喜欢使用array / json_encode吗?..但我不知道该怎么做

达瓦尔·普罗希特(Dhaval Purohit)

您可以为此使用json_encode以获得正确的json输出,也可以使用此技术摆脱此“,”(逗号)。

//first use an variable $cnt=0;
//then check into the while loop
//whether the $cnt==0 then not to put the , before the making of an entry 
//for example,
<?php
include('../connectDB.php'); //connect to db
$cnt=0;

echo '{ ';
echo '"success": 1, ';
    echo '"result": [ ';
        $result = mysql_query("SELECT * FROM roominventory");
        while($row = mysql_fetch_array($result)) { //start while
            $getId = $row['id']; //get value
            $getRoomId = $row['room'];
            if($cnt==0)
            {
                 echo '{ ';
            }
            else
            {
                 echo ',{';
            }
                $ar = $row['arrival']; //assign value to variable
                $dep = $row['departure'];

                $date = str_replace('-', '/', $ar); //format the date
                $formatArr =  date('m/d/Y', strtotime($date));

                $date2 = str_replace('-', '/', $dep); //format the date
                $formatDep =  date('m/d/Y', strtotime($date2));

                $mSt = strtotime($formatArr) * 1000; //convert to milliseconds
                $mEd = strtotime($formatDep) * 1000;

                echo '"id": ' . '"' . $getId. '"' . ', ';

                $resulta = mysql_query("SELECT * FROM rooms_amenities WHERE id='$getRoomId'");
                while($rowa = mysql_fetch_array($resulta)) {
                    $getName = $rowa['name'];
                }

                echo '"title": ' . '"' . $getName . '"' . ', ';
                echo '"url": ' . '"' . 'http://example.com' . '"' . ', ';
                echo '"class": ' . '"' . 'event-warning' . '"' . ', ';
                echo '"start": ' . '"' . $mSt . '"' . ', '; //echo the converted date
                echo '"end": ' . '"' . $mEd . '" ';
            echo '} ';
            //echo ', '; //comma (should only in between entries and not in the very end of the very last entry)
           $cnt=1;
        } //end while
echo '] ';
echo '}';

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章