开发者

subtraction in a php while loop

开发者 https://www.devze.com 2023-01-27 03:45 出处:网络
I\'d like to think I\'m 开发者_高级运维far from a PHP novice, but for some reason I cannot get the following to work. I\'m trying to subtract a value from a number using a PHP while loop.

I'd like to think I'm 开发者_高级运维far from a PHP novice, but for some reason I cannot get the following to work. I'm trying to subtract a value from a number using a PHP while loop.

The code is below, but it only seems to be subtracting the first value.

I've obviously initiated a mysql query, and then have the following code:

 $scMins = 500;
 while($f=mysql_fetch_object($r)){

  $duration = $f->duration;

  $scRem = ($scMins - $duration);


 }


 return $scRem;

but its just notn working.

Any help would be MUCH appreciated.

Thanks, Nick


You always subtract from the initial value scMins

$scRem = 500;
while($f=mysql_fetch_object($r)){
  $duration = $f->duration;
  $scRem = ($scRem - $duration);
}


What is $r?

If it returns nothing, then it most likely means that your loop is wrong. I'd check if your query actually returns something (or if it errors), the problem most likely lies there.


In your while-loop the variables $scRem gets overwritten at each iteration. Try

$scRem += ($scMins - $duration);

But honestly it's not very clear what you are trying to achive, so this may be the wrong answer ;-)

0

精彩评论

暂无评论...
验证码 换一张
取 消