2013-09-24 75 views
1

我有一些JS代碼顯示的圖形數據:把JS代碼的PHP while循環

series: [{ 
       name: 'Year 1800', 
       data: [107, 31, 635, 203, 2] 
      }, { 
       name: 'Year 1900', 
       data: [133, 156, 947, 408, 6] 
      }, { 
       name: 'Year 2008', 
       data: [973, 914, 4054, 732, 34] 
      }] 

我需要得到的數據在PHP while循環顯示。我曾經嘗試這樣做:

<?php 
       $sql="SELECT *, COUNT(category) AS my_groupcount from tickets where deleted = '' and DAY(datetime) = '".$day."' and MONTH(datetime) = '".$month."' and YEAR(datetime) = '".$year."' group by category order by datetime ASC "; 
       $rs=mysql_query($sql,$conn); 
       while($result=mysql_fetch_array($rs)) 
       { 
        echo "name: 'Cat ".$result["category"]."',"; 
        echo "data: [".$result["my_groupcount"]."]"; 
        echo "}, {"; 
       } 
       ?> 

我需要組中的票表中的類別列,併爲每個類別顯示的圖形,但它不工作 - 我認爲它是因爲在while循環與}, {結束,但我需要它以}]

我該如何解決這個問題 - 由於用戶能夠添加/刪除類別,因此ticket表中的類別項目數量一直在變化。

+8

查看PHP的'json_encode'。你真的不應該用手寫json - 如果這個類別最近被稱爲'New和/或'chic'',那該怎麼辦...... –

回答

1
<?php 
      $sql="SELECT *, COUNT(category) AS my_groupcount from tickets where deleted = '' and DAY(datetime) = '".$day."' and MONTH(datetime) = '".$month."' and YEAR(datetime) = '".$year."' group by category order by datetime ASC "; 
      $rs=mysql_query($sql,$conn); 
      $first = true; 
      echo 'series: [{'; 
      while($result=mysql_fetch_array($rs)) 
      { 
       if(!$first) { 
        echo "}, {"; 
       } else { 
        $first = false; 
       } 
       echo "name: 'Cat ".$result["category"]."',"; 
       echo "data: [".$result["my_groupcount"]."]"; 
      } 
      echo '}]'; 
?> 
0

替代括號,儘管最好是構建它然後回顯它,這樣你就可以擺脫最後的逗號。

$string = ''; 
while($result=mysql_fetch_array($rs)) 
{ 
    string.= "{"; 
    string.= "name: 'Cat ".$result["category"]."',"; 
    string.= "data: [".$result["my_groupcount"]."]"; 
    string.= "},"; 
} 
$string = trim($string,','); // gets rid of that last comma 

echo "[$string]"; 
4

爲什麼不去做這樣的:

<?php 
    $sql = "[.. SQL Statement ..]"; 
    $rs = mysql_query($sql, $conn); 
    $json = array(); 

    while($result = mysql_fetch_array($rs)) { 
     $json[] = array( 
      'name' => 'Cat '. $result['category'], 

      // This does assume that my_groupcount is an array with numbers 
      // i.e. array(1, 34, 54, 345) 
      // If not, you'll have to make it an array by doing: 
      // explode(', ', $result['my_groupcount']) 
      // This however does assume that the numbers are in 
      // the "12, 23" format 
      'data' => $result['my_groupcount'], 
     ); 
    }    

    echo json_encode($json); 
+0

我想在這裏添加一個快速提示:你在示例中顯示的JSON是無效的,因爲它在JSON解析器中不能很好地驗證。如果你想自己檢查JSON驗證,請看看這裏:http://jsonlint.com/ –

0

嘗試

$sql="SELECT *, COUNT(category) AS my_groupcount from tickets where deleted = '' and DAY(datetime) = '".$day."' and MONTH(datetime) = '".$month."' and YEAR(datetime) = '".$year."' group by category order by datetime ASC "; 
$rs=mysql_query($sql,$conn); 
$output = '['; 
while($result=mysql_fetch_array($rs)) 
{ 
    $output .= "name: 'Cat ".$result["category"]."',"; 
    $output .= "data: [".$result["my_groupcount"]."]"; 
    $output .= "}, {"; 
} 

$output = substr($output, 0, -3) . ']'; 

,但豐富的說,你真的不應該由手工編寫JSON。