Question

This is a PHP programming class

Create a Web form that shows the mileage between European capitals. Use a two-dimensional associative array to store the mileage. The form should allow the user to enter a start city and an end city. Your code should then search the two dimensional for the distance from each city. For Example: A user enters Berlin to Paris. You should return "The distance from Berlin to Paris is: 879.96 km"

Add the following code to the script section to declare an associative array of European cities and the distances between them in kilometers, and the conversion factor for kilometers to miles:

Distancesarray ( Berlinarray (Berlin-> 0, Moscow1607.99, Paris 876.96, Prague280.34, Rome>1181.67), Moscowarray (Berlin> 1607.99, Moscow , Paris -> 2484.92, Prague 1664.04, Rome2374.26), Paris> array (Berlin -876.96, Moscow641.31, Paris ->0, Prague 885.38, Rome1105.76) Prague-array (Berlin -> 280.34, Moscow1664.04, Paris->885.38, Prague -0, Rome922), Rome - array (Berlin1181.67, Moscow 2374.26, Paris1105.76, Prague -> 922, Rome -> 0)): $KMtoMiles-0.62:

Save the document as eudistance.php.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Here is the php code eudistance.php

i did't make it stylish , but it work fine , the php function foreach() , is used to get the single row at a time like the for() loop , untill the array . please comment if you have any doubts

<html>
<head>
   <h1> eudistance</h1>
</head>
<body>
<form method='post' action='us.php'>
   start city
   <input type='text' name='from_place' ><br>
   end city
   <input type='text' name='to_place' ><br>
   <input type='submit' name='go' value="GO" >


</form>
</body>
</html>
<?php

$Distances = array(
              "Berlin" => array("Berlin" => 0, "Moscow" => 1607.99, "Paris" => 876.96, "Prague" => 280.34, "Rome" =>1181.67),
              "Moscow" => array("Berlin" => 1607.99, "Moscow" => 0, "Paris" => 2484.92, "Prague" => 1664.04, "Rome" =>2374.26),
              "Paris" => array("Berlin" => 876.96, "Moscow" => 641.31, "Paris" => 0, "Prague" => 885.38, "Rome" =>1105.76),
              "Prague" => array("Berlin" => 280.34, "Moscow" => 1664.04, "Paris" => 885.38, "Prague" => 0, "Rome" =>922),
              "Rome" => array("Berlin" => 1181.67, "Moscow" => 2374.26, "Paris" => 1105.76, "Prague" => 922, "Rome" =>0),
              );
$KMtoMiles = 0.62;

if(isset($_POST['from_place']) && $_POST['to_place']){ // check if the values are submitted or not
   $distance = 0;
   $flag = 0;       // flag just to check entered values are right
   foreach($Distances as $key => $value)    // for each row take the row as key and it's value ie key is from_place and value is an array
   {
       if($key == $_POST['from_place'])   // if from_place match then
       {
          
           foreach($value as $place => $dist) // for each value that distance from the from_place
           {
               if($place == $_POST['to_place'])
               {
                   $flag = 1;
                   $distance = $dist;
               }
           }
       }
              
   }
   if($flag == 1){
      
   $miles = $distance*$KMtoMiles; // convert to miles and print
    echo "The distance from ".$_POST['from_place']." to ".$_POST['to_place'] ." is:".$distance." km. And ". $miles ." miles";
   }
   else{
       echo " Not a known place";
   }

}

output

localhost/web/HomeworkLib/php/us.php eudistance start city Berlin end city Paris GO The distance from Berlin to Paris is:876.96 km.

Add a comment
Know the answer?
Add Answer to:
This is a PHP programming class Create a Web form that shows the mileage between European...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • PHP Programming In this project, you will create a Web page that allows visitors to your...

    PHP Programming In this project, you will create a Web page that allows visitors to your site to sign a guest book that is saved to a database. Create a new document in your text editor and type the <!DOCTYPE> declaration, <html> element, document head, and <body> element. Use the strict DTD and “Guest Book” as the content of the <title> element. Add the following text and elements to the document body: <h2>Enter your name to sign our guest book</h2>...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT