Multiple Arrays and foreach loop

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
becky-atlanta
Forum Commoner
Posts: 74
Joined: Thu Feb 26, 2009 6:26 pm
Location: Atlanta, GA

Multiple Arrays and foreach loop

Post by becky-atlanta »

I am not an experienced programmer. I have been searching and can't find the answer anywhere.

I am trying to create a php order form and this is what I would like the output to look like:

Banana.....................$0.49/lb........QTY____
Apple........................$0.99/lb.......QTY____
Orange......................$0.50/ea......QTY____

My attempt was to create arrays. I need to have $fruit, $price, and $unit.

$fruit = array('Banana' => '0.49' , 'Apple' => '22.50' , 'Orange' => '0.50');

I also would like to create an array for the $unit, so that I can keep the $price a numeric value and use it to calculate the price further down the road.

I only found that I can use foreach ($fruit as $price) to populate the table. However, I can't figure out how to use foreach function to work with more than 2 array variables at the same time.

Please let me know if I am heading the right direction and give me some hints how to accomplish this.

Thanks in advance!

Becky
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: Multiple Arrays and foreach loop

Post by requinix »

Not entirely sure what you're trying to do, but have you considered an array like

Code: Select all

$fruit = array(
    'Banana' => array(0.49, "lb"),
    'Apple' => array(22.50, "lb"),
    'Orange' => array(0.50, "ea")
);
Tip: if something is supposed to be a number, don't make it a string. In PHP it doesn't matter so much but that's no excuse if you can help it.
User avatar
Benjamin
Site Administrator
Posts: 6935
Joined: Sun May 19, 2002 10:24 pm

Re: Multiple Arrays and foreach loop

Post by Benjamin »

Code: Select all

 
$x = array(
  1 => array('product' => 'Banana', 'price' => '0.49', 'quantity' => 0),
  2 => array('product' => 'Apple', 'price' => '0.99', 'quantity' => 0),
  3 => array('product' => 'Orange', 'price' => '0.50', 'quantity' => 0),
);
 
User avatar
becky-atlanta
Forum Commoner
Posts: 74
Joined: Thu Feb 26, 2009 6:26 pm
Location: Atlanta, GA

Re: Multiple Arrays and foreach loop

Post by becky-atlanta »

Thank you for the prompt response, tasairis and astions.

I am aware of the array format you mentioned. But I got stuck at the output part. I can't figure out how to use "foreach" to produce the desires output, I mean to have all 3 or 4 array variables on the same row. I only know foreach ($fruit as $a) and it only produces 2 outputs, i.e. banana......0.49. The measure unit would be missing or I don't know how to retrieve it. Can you explain further? Thanks.
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Re: Multiple Arrays and foreach loop

Post by John Cartwright »

Code: Select all

$fruits = array(
  1 => array('product' => 'Banana', 'price' => '0.49', 'quantity' => 0),
  2 => array('product' => 'Apple', 'price' => '0.99', 'quantity' => 0),
  3 => array('product' => 'Orange', 'price' => '0.50', 'quantity' => 0),
);
 
foreach ($fruits as $fruit) {
   echo 'Product: '. $fruit['product'] .'<br />';
   echo 'Price: '. $fruit['price'] .'<br />';
   echo 'Quantity: '. $fruit['quantity'] .'<br />';
   echo '<hr />';
} 
 
User avatar
becky-atlanta
Forum Commoner
Posts: 74
Joined: Thu Feb 26, 2009 6:26 pm
Location: Atlanta, GA

Re: Multiple Arrays and foreach loop

Post by becky-atlanta »

Thanks, John. That's exactly what I want. :D The code is simple and do the job. You are awesome! :wink:

Here is part of the code:

<?php
$pesach_start = array(
1 => array('start' => 'Seder Plate', 'price' => '25.00', 'unit' => ea),
2 => array('start' => 'Tri colored Gefilte Fish Terrine', 'price' => '22.50', 'unit' => ea),
3 => array('start' => 'Chopped Liver', 'price' => '14.50', 'unit' => lb),
4 => array('start' => 'Charoset', 'price' => '14.50', 'unit' => lb),
);

echo "<table width=\"100%\" border=\"0\" class=\"body\">\n";

foreach($pesach_start as $a) {
echo "<tr>\n";
echo "<td width=\"70%\">".$a['start']."</td>\n";
echo "<td width=\"15%\">$".$a['price']."/".$a['unit']."</td>\n";
echo "<td width=\"15%\">\n";
echo "<div alight=right\">Qty \n";
echo "<select name=\"".$a['start']."\">\n";
echo "<option>0</option>\n";
echo "<option>1</option>\n";
echo "<option>2</option>\n";
echo "<option>3</option>\n";
echo "<option>4</option>\n";
echo "<option>5</option>\n";
echo "</select>\n";
echo "</div></td>\n";
echo "</tr>\n";
}
echo "</table>\n";
?>

Now I just have to figure how to make those variables available for the final order form together with the customer's information. Wherever there is a form field, it would be easy. But there is also plain text. Using the same example, they are the fruit and the price. Should I use hidden fields to hold those information?
Post Reply