New Associative Array Problem

Ignore the other post, I accidentally submitted it before I was finished outlining my problem.

I have an HTML form that uses a drop-down list for users to choose a destination (their are five options Barcelona, Cairo, Rome, Santiago, Tokyo). Once the user’s choice has been submitted to the php program, I need to create two associative arrays ($farePerPerson which is the cost of a plane tix depending on the user’s choice. Each choice has a different value depending on the user’s $destination and $hotelPerNight which is the cost of a hotel room depending on the user’s choice. Each choice has a different value depending on the user’s $destination). I have included the HTML code and PHP code below. I need help with how to write the associative arrays so that the info will print the user’s $destination, $farePerPerson and $hotelPerNight.

Thanks in advance :slight_smile:

Here is the HTML code:

TRAVEL COSTS

TRAVEL COSTS

Select your travel destination: BarcelonaCairoRomeSantiagoTokyo

Now the user’s choice is submitted to travelCosts.php and here is the php code:

TRAVEL COSTS <?php $destination = $_POST['destination']; $farePerPerson = ; $hotelPerNight = ;
  print ("<h1>TRAVEL COSTS for $destination</h1>");
  print ("<p>The air fare is $".number_format($farePerPerson, 2)." per person</p>");
  
  print ("<p>The hotel rates are $".number_format($hotelPerNight, 2)." per night.</p>");

?>

Thanks,
Jason

I’m not sure I entirely I understand the flow of the site, but it may help you create an array of destinations (which will be in a common include file such as config.php or common.php - or at the top of a header.php all depending on your template):
[php]$travel_destinations = array(
//“Destination” => [flight cost]
“Barcelona” => 120
,“Cairo” => 140
,“Rome” => 160
,“Santiago” => 235
,“Tokyo” => 180
);[/php]

Then your select drop-down can be generated like so:
[php]echo ‘’;
foreach($travel_destinations as $destination => $price)
{
echo ‘’.$destination.’’;
}
echo ‘’;[/php]

This array will also mean that in travelCosts.php you can use this code:
[php]$flightcost = isset($travel_destinations[$_POST[‘destination’]]) ? $travel_destinations[$_POST[‘destination’]] : false;
if($flightcost !== false)
{
echo ‘

Flight cost is $’.number_format($flightcost,2).’

’;
}
else
{
echo ‘Aaargh something went wrong!’; //Or maybe no else statement… however you’d want to deal with this likelihood!
}[/php]

At this point I am confused in regards to hotel costs and how they are decided etc. Hopefully this stuff helped a bit?

Sponsor our Newsletter | Privacy Policy | Terms of Service