embsupafly Posted April 7, 2006 Share Posted April 7, 2006 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] Quote Link to comment Share on other sites More sharing options...
Yesideez Posted April 7, 2006 Share Posted April 7, 2006 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. Quote Link to comment Share on other sites More sharing options...
embsupafly Posted April 7, 2006 Author Share Posted April 7, 2006 Cool just what I was looking for. Quote Link to comment Share on other sites More sharing options...
kenrbnsn Posted April 8, 2006 Share Posted April 8, 2006 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 Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.