2012-01-04 25 views
1

我遇到問題從我的數據庫使用where子句與for循環變量檢索數據。此代碼當前可以從數據庫中檢索數據,但是從數據庫中檢索到的值存儲。在for循環中的數字1仍然很好。但是當它進入下一個。收集的數據是1和2的組合。當它最終到達循環結尾時,顯示所有數據。 哪個代碼的一部分,必須進行編輯,以數據的顯示改變到非累積PHP SQL其中聲明與循環變量

<?php 

$sectorcount = $row_SectorCount['COUNT(*)']; 
//number of rows in database 
$spendingname= array(); 
$spendingpercent= array(); 
$spendingid= array(); 

?> 
// Add some data to the details pie chart 
options_detail_1.series.push({ name: 'Tax Spending Detail', data: [] }); 

$(document).ready(function() { 
chart_main = new Highcharts.Chart(options_main); 
chart_detail_1 = new Highcharts.Chart(options_detail_1); 
}) 

function push_pie_detail(sectorid) { 
switch(sectorid) 
{ 
<?php 

for ($i=1; $i<=$sectorcount; $i++) 
    { 
    echo "case 'sector".$i."':" , 
      "setTimeout", "(" , '"chart_detail_1.series[0].remove(false)", 1000);' , 
      "chart_detail_1.addSeries(options_detail_1, false);" , 
      "chart_detail_1.series[1].setData(["; 

      mysql_select_db($database_conn2, $conn2); 
      $query_Spending = "SELECT CONCAT(spending.SectorID,  spending.ExpenditureID) AS 'SpendingID', 
      expenditure.ExpenditureName, spending.SpendingPercent, spending.SectorID 
      FROM spending 
      INNER JOIN expenditure ON spending.ExpenditureID = expenditure.ExpenditureID 
      WHERE spending.SectorID = '.$i.'"; 
      $Spending = mysql_query($query_Spending, $conn2) or die(mysql_error()); 
      $totalRows_Spending = mysql_num_rows($Spending); 
      while($row_Spending = mysql_fetch_assoc($Spending)) 
      { 
      $spendingname[] = $row_Spending['ExpenditureName']; 
      $spendingpercent[] = $row_Spending['SpendingPercent']; 
      $spendingid[]= $row_Spending['SpendingID']; 
      } 
      mysql_free_result($Spending); 

      $a = 0; 

      foreach ($spendingid as $sgi => $sgid) 
      { 
      if ($a > 0) echo ', '; // add the comma 

      echo "{ 
       name: '$spendingname[$sgi]', 
       y: ". $spendingpercent[$sgi] * $tax .", 
       id: ' $sgid', 
        }"; 

      $a++; 
      } 

      echo "], false);" , 
        "chart_detail_1.redraw();", 
        "break;"; 
      }  


      ?> 
      default: 
      break; 
     } 

}

回答

1

你需要用你的變量「X」單引號的where子句中,否則會覺得你的意思是ID = true,這對所有的人都是如此=)我最近

有同樣的問題編輯:所以,而不是說花的地方.SectorID = $我,你應該有'somevalue',只是做一個新的變量$ example =「'」。 $ i。 「'」,並使用該變量在WHERE子句中

我寫了一個函數,它的文本並把它封裝在單引號只是針對這種情況

+0

你如何設法解決它?即時通訊真的很新的PHP請指導我:D – 2012-01-04 02:39:22

+0

其不工作:(餅圖即時嘗試顯示仍然顯示幾乎所有累計 – 2012-01-04 02:53:02

+0

然後我會指出一個問題與餅圖,而不是SQL或循環,我還沒有餅圖功能的知識 – 2012-01-04 03:03:34

0

移動在循環內部的$spendingname = array();和其它陣列定義。

+0

我試着移動它,但它不工作 – 2012-01-04 01:29:46