php/mysql - while 循环 ..将结果传递到数组中?

php/mysql - while loop ..pass result in array?

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

您可以为此使用 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 '}';

您可以使用 json_encode 轻松获得该输出。试试这个代码,它应该适合你。

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

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

    $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;

    $resulta = mysql_query("SELECT * FROM rooms_amenities WHERE id='$getRoomId'");

    while($rowa = mysql_fetch_array($resulta))
    {
        $getName = $rowa['name'];
    }

    $new_list= array('id'=>$getId,'title'=>$getName,'url'=>'http://example.com','class'=>'event-warning','start'=>$mSt,'end'=>$mEd);
    $response = array("success"=>1,"result"=>$new_list);    
}
echo json_encode($response);
?>

您可以获得问题中显示的输出..谢谢

$result = mysql_query("SELECT * FROM roominventory");
$jsonData = '';
while($row = mysql_fetch_array($result)) { //start while
    $jsonData .= json_encode($row);
}
echo $jsonData;

试试这个。