PDA

View Full Version : how to do this in PHP?


animal
2-6-02, 06:43 AM
I was working on a shopping cart and I wrote an order script in PHP. I am sending the script numbered variables for each item ordered. name1,quantity1,price1,itemtotal1. There would be a set of these variables for each item ordered, Ex name2, name3. These variables are coming out of the cart software and I never know how many there will be.

I wrote this loop to display the items ordered in a table. The problem is I have the limit hard-coded to 15 items. I wanted to check and see if quantity$i was null and stop looping. Any ideas?

for ($i=1; $i <= 15; $i++)

{
$temp_a = "Name$i";
$temp_b = "Quantity$i";
$temp_c = "Price$i";
$temp_d = "ItemTotal$i";


print "<tr>";
print "<td width=70%>".$$temp_a."</td>";
print "<td width=10%>".$$temp_b."</td>";
print "<td width=10%>".$$temp_c."</td>";
print "<td width=10%>".$$temp_d."</td>";
print "</tr>";
}

sophiespo
2-7-02, 12:42 AM
try a while loop??

while ($temp_b != 0) {
// the code for the loop
}

as soon as $temp_b is 0 itll exit the loop... is that what youre after?

sophie

animal
2-7-02, 05:44 AM
I thought of that, but $temp_b doesn't exist intil the loop I wrote is executed.

------------------------------------------------------------------------------------
for ($i=1; $i <= 15; $i++) // create i=1, i=2 ,...etc..., i=15

{
$temp_a = "Name$i"; // Name1, Name2, Name3....
$temp_b = "Quantity$i";
$temp_c = "Price$i";
$temp_d = "ItemTotal$i";

------------------------------------------------------------------------------------

Also, the variables I am creating are not able to be checked. They do not contain values, just pointers to other variables. $temp_b is not going to be 1,2,3.... it will be Quantity1,Quantity2.... l would like to be able to only create the number of i's that there are variables coming from the html form.

jfbell66
2-8-02, 10:29 AM
I think this is what you want:

for ($i=1; $i <= 15; $i++)

{
$temp_a = "Name$i";
$temp_b = "Quantity$i";
$temp_c = "Price$i";
$temp_d = "ItemTotal$i";

if ($$temp_b==null) continue 2;

print "<tr>";
print "<td width=70%>".$$temp_a."</td>";
print "<td width=10%>".$$temp_b."</td>";
print "<td width=10%>".$$temp_c."</td>";
print "<td width=10%>".$$temp_d."</td>";
print "</tr>";
}

jfbell66
2-8-02, 10:40 AM
My bad.

It should actually be:

for ($i=1; $i <= 15; $i++)

{
$temp_a = "Name$i";
$temp_b = "Quantity$i";
$temp_c = "Price$i";
$temp_d = "ItemTotal$i";

if ($$temp_b==null) break;

print "<tr>";
print "<td width=70%>".$$temp_a."</td>";
print "<td width=10%>".$$temp_b."</td>";
print "<td width=10%>".$$temp_c."</td>";
print "<td width=10%>".$$temp_d."</td>";
print "</tr>";
}