duandiao3961 2016-08-09 06:54
浏览 55
已采纳

php / mysql - while循环.pass导致数组?

my title is confusing..sorry about that..anyway..kindly look at my code:

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

now this is the result of the file:

{ "success": 1, "result": [ { "id": "254", "title": "Standard Room 202", "url": "exampledotcom", "class": "event-warning", "start": "1470693600000", "end": "1471384800000" } ] }

no problem about that. now, problem is that when there's more than 1 row in the table in my db the result becomes like this:

{ "success": 1, "result": [ { "id": "255", "title": "Standard Room 201", "url": "exampledotcom", "class": "event-warning", "start": "1471903200000", "end": "1472076000000" }, { "id": "256", "title": "Standard Room 202", "url": "exampledotcom", "class": "event-warning", "start": "1471903200000", "end": "1472076000000" }, ] }

notive the "comma" at the last entry "1472076000000" }, ] }

what my desired/expected result should be like this:

{ "success": 1, "result": [ { "id": "255", "title": "Standard Room 201", "url": "exampledotcom", "class": "event-warning", "start": "1471903200000", "end": "1472076000000" }, { "id": "256", "title": "Standard Room 202", "url": "exampledotcom", "class": "event-warning", "start": "1471903200000", "end": "1472076000000" } ] }

notice the "comma" after the first entry and between the second entry { "id: "...}, { "id: "...} and no comma after the very last entry.

i tried to echo the comma outside/end of while-loop. but in the result, the comma is only at the very last entry, no in-between

if last row is reached then there should be no comma at the very last entry. i don't know how can i make the desired result.

is there any other approach/way to this? like using array/json_encode? ..but i don't know how to do it

  • 写回答

3条回答 默认 最新

  • dongliang1654 2016-08-09 07:05
    关注

    You can either use the json_encode for this to get the proper json output or you can get rid of this "," (comma) by using this technique.

    //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 '}';
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 对于相关问题的求解与代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 信号傅里叶变换在matlab上遇到的小问题请求帮助
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料
  • ¥15 使用R语言marginaleffects包进行边际效应图绘制