Page 1 of 1
While loop question
Posted: Thu Sep 02, 2004 8:36 pm
by gjb79
I'm having a bit of difficulty. I have a series of 29 items that need to run through a script that does several functions to them. I realize I may not be using the correct terms, so let me just write it out.
These are my variables.
$Q01 - $Q29 (for quantity)
$P01 - $P29 (for price)
$D01 - $D29 (for description)
These variables are not in an array, they are all listed seperately, unfortunately they are sent from a form and cannot be sent as an array.
This is what I have so far:
Code: Select all
<?php
if ($HTTP_POST_VARS['Q01'] > 0) {
$Quantity1 = $HTTP_POST_VARS['Q01'];
$Description1 = $HTTP_POST_VARS['D01'];
$Price1 = $HTTP_POST_VARS['P01'];
$Amount1 = $Quantity1 * $Price1;
$Item1 = $Description1 . ", Quantity = " . $Quantity1 . ", Amount = " . $Amount1;
}
$Total = $Amount1 + $Amount2 + etc....;
?>
The If statement works perfectly, however Rather than write it out 29 times, I thought I could use a while statement that would simply replace the 1 in all the variables incrementally so as the loop proceeds to loop 2, all the 1's become 2's then 3's etc...
Is this possible?
Thanks for your help.
Posted: Thu Sep 02, 2004 8:51 pm
by feyd
do you have access to the form code? If so, you can alter the script/file such that it will get loaded as arrays in your processor here.
anyways, on to your answer:
Code: Select all
<?php
$stop = 29;
$start = 1;
$Total = 0;
for($n = $start; $n <= $stop; $n++)
{
$N = sprintf('%02d',$n);
${'Quantity' . $n} = isset($_POST['Q' . $N]) ? intval($_POST['Q' . $N]) : 0;
${'Price' . $n} = isset($_POST['P' . $N]) ? (float)$_POST['P' . $N] : 0;
${'Amount' . $n} = ${'Quantity' . $n} * ${'Price' . $n};
${'Item' . $n} = ${'Description' . $n} . ', Quantity = ' . ${'Quantity' . $n} . ', Amount = ' . ${'Amount' . $n};
$Total += ${'Amount' . $n};
}
?>
however, it'd be easier to set everything up to use arrays.

Foreach is the best way.
Posted: Sat Sep 04, 2004 10:33 am
by wasabi
Use the foreach($_POST)
Form should look something like this:
Code: Select all
<form name="whateveryouwant" method="post" action="confirm.php">
<select name="Accessory_Kit_accessories" id="Accessories" style="background:#FFF; color: #666;">
<option value="0">Accessories</option>
<option value="1">> 1</option>
</select>
<input type="submit" name="Submit" value="Continue">
</form>
PHP example
Code: Select all
<?php
foreach($_POST as $num => $item)
checkitems($item);
function checkItem($item) {
$item = split("_",$item); // cuts up the string at every underscore
$tail = array_pop($item); // cuts the last bit off (eg. w3)
$item = join(' ',$item); // sews everything back together
$item = $item . " ("$tail")"; // add the tail
return $item;
}
?>
The rest you can put together.
have fun!
?>
Posted: Sat Sep 04, 2004 11:01 am
by feyd
Wasabi, I fail to see how your solution applies to gjb79's problem..

Posted: Sat Sep 04, 2004 11:05 am
by wasabi
feyd wrote:Wasabi, I fail to see how your solution applies to gjb79's problem..

gjb 79's These variables are not in an array, they are all listed seperately, unfortunately they are sent from a form and cannot be sent as an array.
There isn't any need for a while loop, using the foreach loop is the better option. Just showing him it is possible.
Posted: Sun Sep 05, 2004 11:37 pm
by ongray
if you need to pass 29 items and hv access to form coding. Try this...
run a for loop on yr form
for($n = 1; $n <= 29; $n++) {
<input name=Q type=text ... >
<input name=P type=text ... >
<input name=D type=text ... >
}
'OnSubmit' the form, i use javascript to alter the name of the variable to ...
for (var i=1;i<=29; i++){
document.form.Q.name = 'Q[]';
document.form.P.name = 'P[]';
document.form.D.name = 'D[]';
}
I know it works, but i have no idea on if this is a good practise.
Posted: Mon Sep 06, 2004 12:07 am
by feyd
totally relying on javascript isn't the best of ideas.. however, some sites can "require" it.. but not many...