我是php的新手并且陷入困境。
我有以下内容:
$receivers
的数组。$totalreceivers
。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 :(得分:1)
代码中的一些内容并没有多大意义。首先,你尝试迭代$totalreceivers
两次,一个循环嵌套在另一个循环中。我怀疑这是你想要的,所以你应该摆脱其中一个。并且您的第一个循环有一个错误的表达式:要在第一个$totalreceivers
循环中迭代for
,您需要让您的测试表达式与数组中的元素数量相对而不是数组本身(可以使用count()):for ($i = 0; $i < count($totalreceivers); $i++)
。
其次,每次在循环中调用$status
时,您都会重置status()
的值。要添加,请使用+=
运算符:$status += status($receivers[$i]);
第三,你在{(1}}的status()函数中做了同样的事情。重置它$rowservice
循环的每次迭代。设置一次,或者总结一下。
答案 1 :(得分:0)
乍一看,看起来你只是在每次迭代时覆盖“$ status”。在循环开始之前,将$ status设置为空数组,然后在每次迭代时添加到它。例如:
$status = array();
foreach ( $totalreceivers as $value)
{
$status[] = status($receivers[$i]);
}
echo count($status);
答案 2 :(得分:0)
您可以让MySQL为您完成更多工作,从而简化此任务。
你有一个数组$receivers
,可能包含订阅者(某些人的接收者)的id。 (我可以互换使用接收器和用户。)
status()
函数从数据库中检索value
为空的每个接收者的subscription_end_date
。我将假设每个订阅者只有一行,因为您只通过变量$rowservice
返回最后一个结果。
您的代码中不清楚您是否需要计数除{0}以外的value
的接收器,或者您想要所有非零值的总和。那么问题是:
幸运的是,这些都很容易找到:
有多少接收器具有非零值?
$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";
答案 3 :(得分: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*/