Jump to content

Archived

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

embsupafly

'WHILE' help

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]

Share this post


Link to post
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.

Share this post


Link to post
Share on other sites
You could have saved yourself a lot of code if you had made $pr and $q arrays, then you would have been able to do:
[code]<?php
$pr = $_SESSION['pr'];
$q = $_SESSION['q'];
for ($i=1;$i<=10;$i++)
   $wo_parts_total += $pr[$i] * $q[$i];
?>[/code]

Ken

Share this post


Link to post
Share on other sites

×

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.