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
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
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
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.