Jump to content

Please HELP!!!


scottg1017

Recommended Posts

Disclaimer:  I am extremely new to programming!  I have taken one C++ class and am currently taking a PHP class.  I have an assignment to "Create a Web form that shows the mileage between European capitals.  Use a two-dimensional associative array to store the mileages."

I have wrote the code, moved it around to different places, deleted things, added things, and I continuously get error message that says it was expecting the end of the file.  I will post what I have below.  I am looking for guided help and not just the answer.  I have re-read the chapter and if I'm being honest, I don't completely understand it.  This is our 7 or 8th assignment and this is the first one I have had to reach out for help.  I appreciate any assistance that anyone is willing to give.  Thanks in advance!

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en"
    lang="en">
    <head>
        <meta http-equiv="Content-Type"
                content="text/html; charset=iso-8859-1" />
        <title>European Travel</title>
    </head>
    <body>
        <p>
            </p>
    </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
    )
);
$StartIndex = "";
 $EndIndex = "";
 $KMtoMiles = 0.62;
    if (isset($_POST['submit'])) {
            $StartIndex = stripslashes($_POST['Start']);
            $EndIndex = stripslashes($_POST['End']);
            if (isset(
                    $Distances[$StartIndex][$EndIndex]))
                    echo "<p>The distance from $StartIndex
                        to $EndIndex is " .
                        $Distances[$StartIndex][$EndIndex]
                        . " kilometers, or " . round(
                        ($KMtoMiles *
                        $Distances[$StartIndex][$EndIndex]),
                        2) . " miles.</p>\n";
                else
                    echo "<p>The distance from $StartIndex
                            to $EndIndex is not in the 
                            array.</p>\n";
    }
                foreach ($Distances as
                        $City => $OtherCities) {
                        echo "<option value='$City'";
                        if (strcmp($StartIndex,$City)==0)
                            echo " selected";
                        echo ">$City</option>\n";
                        }
                foreach ($Distances as
                        $City => $OtherCities) {
                        echo "<option value='$City'";
                        if (strcmp($EndIndex,$City)==0)
                            echo " selected";
                        echo ">$City</option>\n";
                        }
    <form action="EuropeanTravel.php" method="post">
    <p>Starting City:
    <select name="Start">
    </select></p>
    <p>Ending City:
    <select name="End">
    </select></p>
    <p><input type="submit" name="submit"
        value="Calculate Distance" /></p>
    </form>    
    ?>

 

Link to comment
Share on other sites

Just about all of your code is misplaced.

  • The PHP code should be first. (except for output which should be in the html/body section
  • Your <form> should be in the html/body section.
  • Your <options>..</options>s should be between the <select>..</select> tags

plus your course material appears to be many years out of date.

Edited by Barand
  • Thanks 1
Link to comment
Share on other sites

I really appreciate you guys pointing me in the right direction.  I was able to move two sections of my code and it works like a champ!  I'm sure I will be back with something else that has stumped me and will be trivial for the group, but, I appreciate the help.  I am a 42 year old college student and things just don't click like they once did.

Link to comment
Share on other sites

This was my rearrangement of your code

<?php
const KM2MILES = 0.62;

$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
        )
    );

/**
* convert Km to miles
*     
* @param float $km     distance in Kilometers
* @return string miles
*/
        function toMiles($km)
        {
            return number_format($km * KM2MILES, 2);
        }

/**
* create list of city options
* 
* @param array $Distances    distances array
* @param string $current     currently selected city
* @return string list of options
*/
        function cityOptions($Distances, $current)
        {
            $opts = "<option value=''> - Select city -</option>";
            foreach (array_keys($Distances) as $city) {
                $sel = $city == $current ? 'selected' : '';
                $opts .= "<option $sel> $city</option>";
            }
            return $opts;
        }

//
//   Initialise output variables
//
$StartIndex = "";
$EndIndex = "";
$kms = 0;;

//
//  Was form data posted?
//    
if ($_SERVER['REQUEST_METHOD']=='POST') {

    $StartIndex = $_POST['Start'] ?? '';
    $EndIndex = $_POST['End'] ?? '';
    $kms = $Distances[$StartIndex][$EndIndex] ?? 0;;
    
}
?>
 <!DOCTYPE html >
 <html>
    <head>
        <meta http-equiv="content-language" content="en">
        <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
        <title>European Travel</title>
    </head>
    <body>
       <form action="" method="post">
        <p>Starting City:
        <select name="Start">
            <?=cityOptions($Distances, $StartIndex)?>
        </select></p>
        <p>Ending City:
        <select name="End">
            <?=cityOptions($Distances, $EndIndex)?>
        </select></p>
        <p><input type="submit" name="btnSubmit"
            value="Calculate Distance" /></p>
        </form>
        <br><br>
        Distance <br><?=$kms?> Km
        <br><?=toMiles($kms)?> Miles    
    </body>

</html>

 

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.