2012-01-04 77 views
0

嘿傢伙,我想重複這個聲明13次通過改變這個語句在一個循環或什麼。 "WHERE spending.SectorID = 1,2,3,4,5,6,7,8,9,10,11,12,13"我怎樣才能成功地做到這一點,而無需再次輸入此代碼12次。我會單獨顯示數據雖然〜喜歡where sectorid = 1;屬於一個表有一個按鈕來啓動該特定查詢PHP Multiarray LOOP WHERE CLAUSE

<?php 
$spendingname= array(); 
$spendingpercent = array(); 
$spendingid = array(); 

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 = 1"; 
$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); 
?> 
+0

但傢伙們將如何識別它?我必須$ {'消費'。$ i} = array(); – 2012-01-04 18:08:15

回答

1
<?php 
    $spendingname= array(); 
    $spendingpercent = array(); 
    $spendingid = array(); 

    mysql_select_db($database_conn2, $conn2); 
    for($x=1;$x<14;$x++) 
    { 
    $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 = $x"; 
    $Spending = mysql_query($query_Spending, $conn2) or die(mysql_error()); 
    $totalRows_Spending = mysql_num_rows($Spending); 
    while($row_Spending = mysql_fetch_assoc($Spending)) 
    { 
    $spendingname[$x] = $row_Spending['ExpenditureName']; 
    $spendingpercent[$x] = $row_Spending['SpendingPercent']; 
    $spendingid[$x]= $row_Spending['SpendingID']; 
    } 
    mysql_free_result($Spending); 
    } 

//To access and print all elements. 
for($x=1;$x<count($spendingname);$x++) 
{ 
echo "The value for query $x"; 
echo $spendingname[$x]." ".$spendingpercent[$x]." ".$spendingid[$x]."<br><br><br>"; 
} 

?> 
+0

該數組是否會同時存儲所有13個值?> – 2012-01-04 18:10:09

+0

13個查詢的結果將單獨存儲在數組元素中。運行這個例子,你會明白的。 – 2012-01-04 18:32:01

1

你只需要包裝腳本的主要部分:

for ($n in range(1,13)) { 

for ($n=1; $n<=13; $n++) { 

和恆定1與替換$n

編輯:還是取決於你想要如何呈現這些數據最後你也許可以從修改SQL:

WHERE spending.SectorID = 1 

WHERE spending.SectorID >= 1 AND spending.SectorID <= 13 

WHERE spending.SectorID IN (1,2,3,4,5,6,7,8,9,10,11,12,13) 

(因爲MySQL的優化器的工作方式應該同樣高效)

0

WHERE spend.SectorID > 0 AND spending.SectorID < 13

0

使用IN條款。具體而言,你可以做一些事情,如WHERE spending.SectorID IN ('" . implode("','", range(1,13)) ...

1

有一件事你不要想做的是做一個單一的查詢就足夠了13數據庫查詢。查詢一旦與以下WHERE條款,然後遍歷結果:

WHERE spending.SectorID <= 13 "; 

- 或 -

WHERE spending.SectorID IN (1,2,3,4,5,6,7,8,9,10,11,12,13)"; 
+0

我想單獨顯示數據,雖然〜像where sectorid = 1;屬於帶有按鈕的表以啓動該特定查詢。可能嗎?我不想一次顯示全部 – 2012-01-04 18:16:48

+0

是的。我建議在['mysql_fetch_array()'](http://php.net/manual/en/function.mysql-fetch-array.php)和['mysql_fetch_assoc()'](http:/ /php.net/manual/en/function.mysql-fetch-assoc.php) – rdlowrey 2012-01-04 18:20:33

1

你可以使用PreparedStatement與參數,然後,你可以循環的作爲查詢的執行以及結果檢索。

但我不明白這樣做的原因,除非你想做一些不同的結果。否則,你爲什麼不使用

WHERE spending.SectorID BETWEEN 1 AND 13 ORDER BY spending.SectorID 

0

如果你想循環一段代碼特定的次數,你可以使用for循環。這是一個PHP腳本,可以循環13次。

$loops = 13; 
for ($i = 1; $loops <= $i; $i++) { 
    //Your code// 
} 

你也應該更換 「WHERE spending.SectorID = 1,2,3,4,5,6,7,8,9,10,11,12,13」 與「WHERE spending.SectorID = $ i「,並且這會增加您的SQL語句在每次代碼循環時搜索的內容。

希望這會有所幫助。

0
<?php 
$sectorcount = $row_SectorCount['COUNT(*)']; 
//number of rows in database 
mysql_select_db($database_conn2, $conn2); 
for($x=1; $x<=$sectorcount; $x++) 
{ 
${'spendingname'.$x} = array(); 
${'spendingpercent'.$x} = array(); 
${'spendingid'.$x} = array(); 

$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 = $x"; 
$Spending = mysql_query($query_Spending, $conn2) or die(mysql_error()); 
$totalRows_Spending = mysql_num_rows($Spending); 

while($row_Spending = mysql_fetch_assoc($Spending)) 
{ 
${'spendingname'.$x}[] = $row_Spending['ExpenditureName']; 
${'spendingpercent'.$x}[] = $row_Spending['SpendingPercent']; 
${'spendingid'.$x}[]= $row_Spending['SpendingID']; 
} 
mysql_free_result($Spending); 
}     
?> 

我設法解決它但不過花費名是唯一無法檢索。消費名是唯一的字符串,其他百分比和ID和所有整數。這就是爲什麼發生這個問題