Jump to content

'WHILE' help


embsupafly

Recommended Posts

I have 10 sets of session vars, pr_(1-10) and q_(1-10), where pr is price and q is quantity.

I need to multiply each pr with its corresponding q (pr_1 * q_1) and add these all together, I thought a while loop would help, but I think I have the syntax wrong. Listed below is the snippet, I think I am just missing a little piece of the puzzle, I should step away and come back to it, but in the meantime, any help would be appreciated.

[code]
$i = 1;
while ($i <= 10) {
$wo_parts_total = ($_SESSION['pr_$i'] * $_SESSION['q_$i']) +;
$i++;
}[/code]
Link to comment
https://forums.phpfreaks.com/topic/6848-while-help/
Share on other sites

I think you'll find a for() loop more appropriate here especially seeing that you're only using the range 1-10.

[code]$wo_parts_total=0;
for ($i=1;$i<=10;$i++) {
  $pr="pr_$i";
  $q="q_$i";
  $wo_parts_total+=($_SESSION[$pr]*$_SESSION[$q]);
}[/code]
Enclosing variables in single quotes won't give you the values of the variables but will instead use that string exactly as shown - surrounding variables in double quotes will insert their contents.
Link to comment
https://forums.phpfreaks.com/topic/6848-while-help/#findComment-24908
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.