While loop question

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
gjb79
Forum Commoner
Posts: 96
Joined: Fri Jul 18, 2003 6:35 am
Location: x <-- (DC)
Contact:

While loop question

Post 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.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post 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. :)
wasabi
Forum Newbie
Posts: 17
Joined: Sat Sep 04, 2004 9:38 am
Location: Adelaide

Foreach is the best way.

Post 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!

?>
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

Wasabi, I fail to see how your solution applies to gjb79's problem.. :?
wasabi
Forum Newbie
Posts: 17
Joined: Sat Sep 04, 2004 9:38 am
Location: Adelaide

Post 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.
ongray
Forum Newbie
Posts: 14
Joined: Wed Sep 01, 2004 1:08 am

Post 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.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

totally relying on javascript isn't the best of ideas.. however, some sites can "require" it.. but not many...
Post Reply