Click to See Complete Forum and Search --> : ...hmm, something like...


Bungholio
08-04-2003, 11:04 AM
Hi, thanks for looking,

Ive got a client with about 25 items on a form page like this:

<input name="prod1_desc" type="hidden" value="PhoneCard, $10.00">
<input name="prod1_price" type="hidden" value="9.50">
<input name="prod1_quantity" type="text" size=3 maxlength=5>

and it goes up prod2_..., prod3_... etc, etc till about 26

now i want to make a dynamic script for this, but dont know if the following will work (i get a parse error on the if statement)


$i = 1;
while ($i <= 26){
if (!$prod$i_quantity == "") {
$prod$i_subtotal = $prod$i_price * $prod$i_quantity;
echo ("Desc: ".$prod$i_desc.", Price: ".$prod$i_price.", Qnty: ".$prod$i_quantity.", Subtotal: ".$prod$i_subtotal."<br>");
}
$i++;
}


the idea is if they enter a quantity, then that product,price,qnty,total will be printed out.

this is just a temp script, ill have more complicated stuff then the echo probably, but what do you think ...?? maybe there is some funtion that i dont know about for this?? is it possible to use an $i for the number like that?

thanks,
Allan

Quasibobo
08-04-2003, 11:47 AM
Your script might work in PHP 4.1 but not PHP4.2+.
For that (it'll work in PHP4.1 also) you'll need to change some things:
Firstly change the formfields into: prod_desc1, prod_price1, prod_quant1 etc...etc....

And your script will/could look like this:

$i=1;
$iprod_desc = "prod_desc1";
$iprod_price = "prod_price1";
$iprod_quant = "prod_quant1";

while (isset($_POST[$iprod_desc]))
{
$isubtotal=$_POST[$iprod_price] * $_POST[$iprod_quant];
echo"Desc: ".$_POST[$iprod_desc].", Price: ".$_POST[$iprod_price].", Qnty: ".$_POST[$iprod_quant].", Subtotal: $isubtotal<br>";

$i++;
$iprod_desc = "prod_desc".$i;
$iprod_price = "prod_price".$i;
$iprod_quant = "prod_quant".$i;
}

Now it doesn't matter if there are 1 or 100 products and it will even work when you or your host is upgrading to PHP4.2+

Quasibobo
08-04-2003, 11:50 AM
Also take a look here about Superglobals: http://www.phpfreaks.com/phpmanual/page/language.variables.predefined.html

Bungholio
08-04-2003, 12:17 PM
ok, thanks VERY MUCH :) :) for your help, ill give that a try :)

Bungholio
08-04-2003, 12:35 PM
hi again, ok, thanks for your help, i got everything working, i modified what you gave me just a little because i wanted only those items with a quantity provided to be listed


while ($_POST[$iprod_quantity] != "")
{
$isubtotal=$_POST[$iprod_price] * $_POST[$iprod_quantity];
echo "Desc: ".$_POST[$iprod_desc].", Price: ".$_POST[$iprod_price].", Qnty: ".$_POST[$iprod_quantity].", Subtotal: $isubtotal<br>";
$i++;
$iprod_desc = "prod_desc".$i;
$iprod_price = "prod_price".$i;
$iprod_quantity = "prod_quantity".$i;
continue;
}


thanks again !

Quasibobo
08-04-2003, 01:08 PM
I usually use:
while (!empty($_POST[$iprod_quantity]))
{
instead of
while ($_POST[$iprod_quantity] != "")
{
But there's not really a difference.......