php循环中总是取最后一个值,PHP的问题有一个循环中的值的总和

我将尝试在下面的代码中概述一些可能的问题:

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*/

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值