2011-08-10 43 views
2

我是新來的PHP,並陷入循環。PHP的問題有一個循環中的值的總和

我有以下:

  • 含有0或沒有命名$receivers陣列。
  • 另一個陣列包含總數爲接收器的數字,名稱爲$totalreceivers
  • 函數查詢帶有id的表,返回0或數字,名爲status()

我想遍歷接收器的每個status()並在最後顯示總和。我無法得到它。

任何幫助將不勝感激。提前致謝。

這裏是我的嘗試:

for ($i = 0; $i < $totalreceivers; $i++) 
{ 
    $status = status($receivers[$i]); 
    foreach ($totalreceivers as $value) 
     $status = status($receivers[$i]); 
    echo "the sum of receiver having status different than 0 is"; 
    echo count($status); 
} 

function status($id) 
{ 
    global $dbhost; 
    global $dbuser; 
    global $dbpass; 
    global $dbname; 
    global $d1; 

    $q2 = mysql_query("select * from user where id ='$id' AND subscription_end_date IS NULL", $d1); 

    while($row2 = mysql_fetch_array($q2)) 
     $rowservice = $row2['value']; 
    return $rowservice; 
} 
+0

你沒有一個很好的方式來解決你的問題。請嘗試設置您的問題的格式,以便它可以解決您的問題 –

+0

您可以在每個*一個*查詢中執行MySQL中的和;參考:http://dev.mysql.com/doc/refman/5.0/en/group-by-functions.html#function_sum – hakre

回答

0

可以簡化通過讓MySQL的爲你做更多的工作這一任務。

你有一個數組$receivers這大概包含id的訂戶(接收器)的東西。 (我可以互換使用接收方和用戶。)

status()函數從數據庫中檢索value,其中subscription_end_date爲空。我將假定每個用戶只有一行,因爲您只通過變量$rowservice返回最後一個結果。

從您的代碼中不清楚您是否想要使用value而不是0的接收器計數,或者是否需要所有非零值的總和。那麼問題是:

  • 你想知道有多少訂戶有非零值?或者,
  • 您是否想知道具有非零值的訂閱者的所有值的總和?

好在這兩個可以很容易地發現:

多少接收機具有非零值?

$allreceivers = implode(',', $receivers); 
$query = "SELECT COUNT(id) AS total_receivers 
    FROM user 
    WHERE subscription_end_date IS NULL 
    AND value != 0 
    AND id IN ($allreceivers);"; 
$result = mysql_query($query, $d1); 
$row = mysql_fetch_array($result); 
$total_receivers = $row['total_receivers']; 

echo "The number of receivers having status (value) other than 0 is $total_receivers"; 

什麼是所有接收值的總和?

$allreceivers = implode(',', $receivers); 
$query = "SELECT SUM(value) AS receiver_value_sum 
    FROM user 
    WHERE subscription_end_date IS NULL 
    AND id IN ($allreceivers);"; 
$result = mysql_query($query, $d1); 
$row = mysql_fetch_array($result); 
$receiver_value_sum = $row['receiver_value_sum']; 

echo "The sum of receiver values is $receiver_value_sum"; 
+0

JYelton,你是一個明星,工作得很好。謝謝 – user888300

+1

不客氣@ user888300;不要忘記[投票](http://stackoverflow.com/privileges/vote-up)。 :) – JYelton

1

在代碼中有兩件事情沒有真正意義。首先,你嘗試兩次遍歷$totalreceivers,一個循環嵌套在另一個循環中。我懷疑這就是你想要的,所以你應該擺脫其中的一個。你的第一個循環有一個不好的表達式:在第一個for循環中遍歷$totalreceivers,你需要讓你的測試表達式與數組中元素的數量相反,而不是數組本身(可以使用count()):for ($i = 0; $i < count($totalreceivers); $i++)

其次,每當您在循環中呼叫status()時,您將重置$status的值。要添加到它,請使用+=運營商:$status += status($receivers[$i]);

第三,你在狀態()函數$rowservice;重置該循環的每次迭代。要麼設置一次,要麼總結一下。

0

乍一看,它看起來像你只是在每次迭代覆蓋「$狀態」。在循環開始之前,將$ status設置爲一個空數組,然後在每次迭代時添加它。例如:

$status = array(); 
    foreach ($totalreceivers as $value) 
    { 
    $status[] = status($receivers[$i]); 
    } 
    echo count($status); 
0

我會盡力勾勒出下面的代碼可能出現的問題:

for ($i = 0; $i < $totalreceivers; $i++) { //This open bracket has no end bracket 
/*I assume total receivers is the number of receivers; if that is the case, it shouldn't be an array, but merely a value.*/ 
      $status = status($receivers[$i]); 
/*I think you should define the status function before you call it; also, if you are just calling the status function for each value of $receivers, why don't you just do a foreach($receivers as $value) loop, like you did below with $totalreceivers. */ 
     foreach ($totalreceivers as $value) 
/*I'm not sure what $totalreceivers is. You said its an array containing total number of receivers; again, I would think this would be a single value, and not an array*/ 
     { 
     $status = status($receivers[$i]); 
/*looks like you're calling the status function again*/ 
     } 
      echo "the sum of receiver having status different thant 0 is"; echo count($status); 
/* $status at this point would count the value from row2 for the last value in $receivers, and not a count of array values. Perhaps you could do $status[] = status($receivers[$i]), and then count($status) to save each value to an array, then count the elements. */ 

function status($id){ 
global $dbhost; 
global $dbuser; 
global $dbpass; 
global $dbname; 
/* The values above aren't used in the function; you may want to connect to the database outside of the function, before the foreach loop. */ 
global $d1 ; 

$q2 = mysql_query("select * from user where id ='$id' AND subscription_end_date IS NULL",$d1); 
while($row2 = mysql_fetch_array($q2)){ 
$rowservice = $row2['value']; 
} 
return $rowservice; 
} 
/*the rest looks good*/ 
+0

感謝提示,幫助了我 – user888300