Jump to content

Another array problem!


snow

Recommended Posts

Hey sorry I don't really know how to explain this but I will try most simply and best.

 

Here is the problem:

 

I have this array:

	$days = array
	(
  		"Monday"=>array
  			(
  			"Chicken Burger"=>array("$5.00"),
  			"Veal Schnitzel"=>array("$7.50"),
  			"Onion Rings"=>array("$3.00"),
  			"Hot Potato"=>array("$4.00")
  			),
  		"Tuesday"=>array
  			(
  			"Steak Sandwich"=>array("$10.00"),
  			"Chicken Pasta"=>array("$4.50"),
  			"Pork Chop"=>array("$6.50"),
  			"Taco"=>array("$1.50")
  			),
  		"Wednesday"=>array
  			(
  			"Chicken and Chips"=>array("$5.00"),
			"Beef Casserole"=>array("$6.50"),
  			"Sausage"=>array("$1.50"),
  			"Lasagne"=>array("$3.50")
  			),
  		"Thursday"=>array
  			(
  			"Fish and Chips"=>array("$5.00"),
  			"Tuna Pattie"=>array("$2.50"),
  			"Chicko Roll"=>array("$2.80"),
  			"Hot Dog"=>array("$3.00")
  			),
   		"Friday"=>array
  			(
  			"Nachos"=>array("$5.50"),
  			"Salad Sandwich"=>array("$4.00"),
  			"Macaroni Cheese"=>array("$3.50"),
  			"Bacon and Eggs"=>array("$7.50")
  			)
	);

 

The user then selects their day, it then goes to another page and prints out all the menu for that DAY in the form of radio buttons.

 

I had this working, UNTIL, I had to add another array for prices.

 

The previous code worked like this:

echo "<h3>Please select your meal for " . $_SESSION['day'] . ":</h3>";
$day = $_SESSION['day'];
$dayMenu = $days[$day];
foreach ($dayMenu as $item):
    echo "<input type=\"radio\" name=\"meal\" value=\"" . $item . "\">" . $item . "<br />";
endforeach;

 

Can anyone tell me the code which will print out all the menu and prices for a day a user selects, you can use the example above and modify it I would think would be easiest.

 

Thanks very much.

 

EDIT: I realised i haven't made much sense. I will need a series of radio buttons like this for example:

 

Monday

 

o Chicken Burger - $5.00

o Veal Schnitzel - $7.50

o Onion Rings - $3.00

o Hot Potato - $4.00

 

where o = a radio button!

 

Link to comment
Share on other sites

Example:

<?php

$days = array( 'Monday'     => array( 'Chicken Burger' => 5.00,
                                      'Veal Schnitzel' => 7.50,
                                      'Onion Rings'    => 3.00,
                                      'Hot Potato'     => 4.00
                                    ),

               'Tuesday'   => array( 'Steak Sandwich' => 10.00,
                                     'Chicken Pasta'  => 4.50,
                                     'Pork Chop'      => 6.50,
                                     'Taco'           => 1.50,
                                    ),

               'Wednesday' => array( 'Chicken and Chips' => 5.00,
                                     'Beef Casserole'    => 6.50,
                                     'Sausage'           => 1.50,
                                     'Lasagne'           => 3.50
                                   ),

               'Thursday'  => array( 'Fish and Chips' => 5.00,
                                     'Tuna Pattie'    => 2.50,
                                     'Chicko Roll'    => 2.80,
                                     'Hot Dog'        => 3.00
                                   ),

               'Friday'    => array( 'Nachos'          => 5.50,
                                     'Salad Sandwich'  => 4.00,
                                     'Macaroni Cheese' => 3.50,
                                     'Bacon and Eggs'  => 7.50
                                   )
             );

if(isset($_GET['day']) && array_key_exists($_GET['day'], $days))
{
    echo '<h3>Please select your meal for ' . $_GET['day'] . ':</h3>';

    $day     = $_GET['day'];
    $dayMenu = $days[$day];

    foreach ($dayMenu as $item => $price)
    {
        $price = number_format($price, 2);
        echo '<input type="radio" name="meal" value="' . $item . '">' . $item . ' ($' . $price . ")<br />\n  ";
    }
}
else
{
    echo '<h1>Please select day:</h1>

<form action="#" action="get">
  ';

   foreach($days as $day => $menu)
   {
        echo '<input type="radio" name="day" value="' . $day . '"> ' . $day . "<br />\n  ";
   }

   echo '<input type="submit" value="Submit">
</form>';

}

?>

Link to comment
Share on other sites

I have changed your format of the days variable. Notice I have changed it so the price is not an array within the days variable, as you are only storing 1 piece of information for each food item.

 

Make sure you are using the days variable in my code within your code. Sorry I forgot to mention that earlier.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.