2016-08-09 166 views
0

我的標題是confusing..sorry關於我的代碼that..anyway..kindly看:php/mysql - while循環..pass導致數組?

<?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「:」標準間202「,」url「:」exampledotcom「,」class「:」事件警告「 :「1470693600000」,「end」:「1471384800000」}]}

沒有問題。現在,問題是,當我的分區表中有超過1行時,結果會變成這樣:

{「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的 「逗號」 的最後一項 「14720.76億」} ]}

什麼我期望/預期結果應該是這樣的:

{「success」:1,「result」:[{「id」:「255」,「title」:「標準房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「}]}

注意到第一個條目之後的」逗號「第二項{「id:」...},{「id:」...}並且在最後一次輸入後沒有逗號。

我試圖回顯while循環的外部/末端的逗號。但結果是,逗號只在最後一個入口處,沒有中間的

如果到達最後一行,則在最後一個入口處不應該有逗號。我不知道我怎麼能做出想要的結果。

有沒有其他方法/方法呢?像使用數組/ json_encode? ..但我不知道該怎麼做

+2

json_encode()是你的朋友,你永遠不應該建立這樣的JSON字符串 – Borjante

+0

只是做一個數組,然後編碼它,更容易。 –

+0

來糾正你的代碼,你應該把echo',';在第一個「回聲」之前{「;」 (isset($ ar){echo',';} –

回答

0

你可以使用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 '}'; 
+1

這一個完美的作品!謝謝。真的很感激:) – silver01

+0

歡迎你。 @ silver01 –

0

你可以很容易地使用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); 
?> 

你可以得到你在你的questions..thanks

+0

它的工作原理,但它刪除/擺脫我需要的白色空間。這次真是萬分感謝 :) – silver01

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

試試這一個顯示輸出。