Jump to content

Recommended Posts

$distance = $cities[toronto];

$time = round( ($distance / 60), 2);

$walktime = round( ($distance / 5), 2);

print "The distance between Chicago and <i> toronto</i> is $distance miles.";

print "<br>Driving at 60 miles per hour it would take $time hours.";

print "<br>Walking at 5 miles per hour it would take $walktime hours.";

 

else {

print "Sorry, do not have destination information for $destination.";

}

?>

</body></html>

Link to comment
https://forums.phpfreaks.com/topic/98320-syntax-errors/#findComment-503098
Share on other sites

This is all my code

 

<html>
<head><title>Distance and Time Calculations</title></head>
<body>
<?php
$cities = array ('Boston' => 1551, 'Dallas' => 803, 'Las Vegas' => 1077, 'Miami' => 1108, 'Nashville' => 615, 'Pittsburgh' => 1069, 'San Francisco' => 1493, 'Toronto' => 1203);
print $location;
if (isset($cities[$destination])) {
$distance = $cities[$destination];
$time = round( ($distance / 60), 2);
$walktime = round( ($distance / 5), 2);
print "The distance between Chicago and <i> $destination</i> is $distance miles.";
print "<br>Driving at 60 miles per hour it would take $time hours.";
print "<br>Walking at 5 miles per hour it would take $walktime hours.";
}
$distance = $cities[boston];
$time = round( ($distance / 60), 2);
$walktime = round( ($distance / 5), 2);
print "The distance between Chicago and <i> boston</i> is $distance miles.";
print "<br>Driving at 60 miles per hour it would take $time hours.";
print "<br>Walking at 5 miles per hour it would take $walktime hours.";

$distance = $cities[dallas];
$time = round( ($distance / 60), 2);
$walktime = round( ($distance / 5), 2);
print "The distance between Chicago and <i> dallas</i> is $distance miles.";
print "<br>Driving at 60 miles per hour it would take $time hours.";
print "<br>Walking at 5 miles per hour it would take $walktime hours.";

$distance = $cities[lasvegas];
$time = round( ($distance / 60), 2);
$walktime = round( ($distance / 5), 2);
print "The distance between Chicago and <i> lasvegas</i> is $distance miles.";
print "<br>Driving at 60 miles per hour it would take $time hours.";
print "<br>Walking at 5 miles per hour it would take $walktime hours.";

$distance = $cities[miami];
$time = round( ($distance / 60), 2);
$walktime = round( ($distance / 5), 2);
print "The distance between Chicago and <i> miami</i> is $distance miles.";
print "<br>Driving at 60 miles per hour it would take $time hours.";
print "<br>Walking at 5 miles per hour it would take $walktime hours.";

$distance = $cities[nashville];
$time = round( ($distance / 60), 2);
$walktime = round( ($distance / 5), 2);
print "The distance between Chicago and <i> nashville</i> is $distance miles.";
print "<br>Driving at 60 miles per hour it would take $time hours.";
print "<br>Walking at 5 miles per hour it would take $walktime hours.";

$distance = $cities[pittsburgh];
$time = round( ($distance / 60), 2);
$walktime = round( ($distance / 5), 2);
print "The distance between Chicago and <i> pittsburgh</i> is $distance miles.";
print "<br>Driving at 60 miles per hour it would take $time hours.";
print "<br>Walking at 5 miles per hour it would take $walktime hours.";

$distance = $cities[sanfrancisco];
$time = round( ($distance / 60), 2);
$walktime = round( ($distance / 5), 2);
print "The distance between Chicago and <i> sanfrancisco</i> is $distance miles.";
print "<br>Driving at 60 miles per hour it would take $time hours.";
print "<br>Walking at 5 miles per hour it would take $walktime hours.";

$distance = $cities[toronto];
$time = round( ($distance / 60), 2);
$walktime = round( ($distance / 5), 2);
print "The distance between Chicago and <i> toronto</i> is $distance miles.";
print "<br>Driving at 60 miles per hour it would take $time hours.";
print "<br>Walking at 5 miles per hour it would take $walktime hours.";

else {
print "Sorry, do not have destination information for $destination.";
}
?>	
</body></html>

Link to comment
https://forums.phpfreaks.com/topic/98320-syntax-errors/#findComment-503109
Share on other sites

Your else is in the wrong the place. It should got directly after the if statement:

if (isset ($cities[$destination]))
{
  $distance = $cities[$destination];
  $time = round(($distance / 60), 2);
  $walktime = round(($distance / 5), 2);
  print "The distance between Chicago and <i> $destination</i> is $distance miles.";
  print "<br>Driving at 60 miles per hour it would take $time hours.";
  print "<br>Walking at 5 miles per hour it would take $walktime hours.";
}
else
{
  print "Sorry, do not have destination information for $destination.";
}

Link to comment
https://forums.phpfreaks.com/topic/98320-syntax-errors/#findComment-503114
Share on other sites

I moved my else statement which got rid of the syntax error, but now my code looks like this

 

Sorry, do not have destination information for .The distance between Chicago and boston is miles.

Driving at 60 miles per hour it would take 0 hours.

Walking at 5 miles per hour it would take 0 hours.The distance between Chicago and dallas is miles.

Driving at 60 miles per hour it would take 0 hours.

Walking at 5 miles per hour it would take 0 hours.The distance between Chicago and lasvegas is miles.

Driving at 60 miles per hour it would take 0 hours.

Walking at 5 miles per hour it would take 0 hours.The distance between Chicago and miami is miles.

Driving at 60 miles per hour it would take 0 hours.

Walking at 5 miles per hour it would take 0 hours.The distance between Chicago and nashville is miles.

Driving at 60 miles per hour it would take 0 hours.

Walking at 5 miles per hour it would take 0 hours.The distance between Chicago and pittsburgh is miles.

Driving at 60 miles per hour it would take 0 hours.

Walking at 5 miles per hour it would take 0 hours.The distance between Chicago and sanfrancisco is miles.

Driving at 60 miles per hour it would take 0 hours.

Walking at 5 miles per hour it would take 0 hours.The distance between Chicago and toronto is miles.

Driving at 60 miles per hour it would take 0 hours.

Walking at 5 miles per hour it would take 0 hours.

Link to comment
https://forums.phpfreaks.com/topic/98320-syntax-errors/#findComment-503128
Share on other sites

Ok, let me see if I can explain this.  I created a drop down menu that lists 5 or 6 cities in which a person can click on a city click a submit button and a new page would appear with distances and times from this city to Chicago.  I now want to add a feature to that where if someone clicks in a check box, the distance and time calculations would appear for all the cities at once rather than just the one city.  Does this make sense?  I can show you the other part of my code if that is helpful

Link to comment
https://forums.phpfreaks.com/topic/98320-syntax-errors/#findComment-503133
Share on other sites

This is the first part

 

<html>
<head><title> Distance from Chicago!</title></head>
<body>
<font size=4 color=blue> Welcome to the Distance Calculation Page.</font>
<br>The page that calculates the distances from Chicago!
<br>Select a destination: 
<form action="http://localhost/julie/c5-e4a.php" method="post">
<select name="destination" size=3 multiple>
<option> Boston<option> Dallas<option> Las Vegas<option> Miami<option> Nashville<option> Pittsburgh<option> San Francisco<option> Toronto<option> 
</select>
To see distance information for every city, click here
<input type="checkbox" name="location" value="yes">
<br>
<input type="SUBMIT" value="Click To Submit">
<input type = "RESET" value = "Reset" > 
<br>
</form></body></html>

 

This is the second part

 

<html>
<head><title>Distance and Time Calculations</title></head>
<body>
<?php
$cities = array ('Boston' => 1551, 'Dallas' => 803, 'Las Vegas' => 1077, 'Miami' => 1108, 'Nashville' => 615, 'Pittsburgh' => 1069, 'San Francisco' => 1493, 'Toronto' => 1203);
print $location;
if (isset($cities[$destination])) {
$distance = $cities[$destination];
$time = round( ($distance / 60), 2);
$walktime = round( ($distance / 5), 2);
print "The distance between Chicago and <i> $destination</i> is $distance miles.";
print "<br>Driving at 60 miles per hour it would take $time hours.";
print "<br>Walking at 5 miles per hour it would take $walktime hours.";
}
else {
print "Sorry, do not have destination information for $destination.";
}

$distance = $cities[boston];
$time = round( ($distance / 60), 2);
$walktime = round( ($distance / 5), 2);
print "The distance between Chicago and <i> boston</i> is $distance miles.";
print "<br>Driving at 60 miles per hour it would take $time hours.";
print "<br>Walking at 5 miles per hour it would take $walktime hours.";

$distance = $cities[dallas];
$time = round( ($distance / 60), 2);
$walktime = round( ($distance / 5), 2);
print "The distance between Chicago and <i> dallas</i> is $distance miles.";
print "<br>Driving at 60 miles per hour it would take $time hours.";
print "<br>Walking at 5 miles per hour it would take $walktime hours.";

$distance = $cities[lasvegas];
$time = round( ($distance / 60), 2);
$walktime = round( ($distance / 5), 2);
print "The distance between Chicago and <i> lasvegas</i> is $distance miles.";
print "<br>Driving at 60 miles per hour it would take $time hours.";
print "<br>Walking at 5 miles per hour it would take $walktime hours.";

$distance = $cities[miami];
$time = round( ($distance / 60), 2);
$walktime = round( ($distance / 5), 2);
print "The distance between Chicago and <i> miami</i> is $distance miles.";
print "<br>Driving at 60 miles per hour it would take $time hours.";
print "<br>Walking at 5 miles per hour it would take $walktime hours.";

$distance = $cities[nashville];
$time = round( ($distance / 60), 2);
$walktime = round( ($distance / 5), 2);
print "The distance between Chicago and <i> nashville</i> is $distance miles.";
print "<br>Driving at 60 miles per hour it would take $time hours.";
print "<br>Walking at 5 miles per hour it would take $walktime hours.";

$distance = $cities[pittsburgh];
$time = round( ($distance / 60), 2);
$walktime = round( ($distance / 5), 2);
print "The distance between Chicago and <i> pittsburgh</i> is $distance miles.";
print "<br>Driving at 60 miles per hour it would take $time hours.";
print "<br>Walking at 5 miles per hour it would take $walktime hours.";

$distance = $cities[sanfrancisco];
$time = round( ($distance / 60), 2);
$walktime = round( ($distance / 5), 2);
print "The distance between Chicago and <i> sanfrancisco</i> is $distance miles.";
print "<br>Driving at 60 miles per hour it would take $time hours.";
print "<br>Walking at 5 miles per hour it would take $walktime hours.";

$distance = $cities[toronto];
$time = round( ($distance / 60), 2);
$walktime = round( ($distance / 5), 2);
print "The distance between Chicago and <i> toronto</i> is $distance miles.";
print "<br>Driving at 60 miles per hour it would take $time hours.";
print "<br>Walking at 5 miles per hour it would take $walktime hours.";

?>	
</body></html>

Link to comment
https://forums.phpfreaks.com/topic/98320-syntax-errors/#findComment-503143
Share on other sites

Use the following code, for display the distances:

<?php

$cities = array ( 'Boston'        => 1551,
                  'Dallas'        => 803,
                  'Las Vegas'     => 1077,
                  'Miami'         => 1108,
                  'Nashville'     => 615,
                  'Pittsburgh'    => 1069,
                  'San Francisco' => 1493,
                  'Toronto'       => 1203
                );

if(isset($_POST['location']) && $_POST['location'] == 'yes')
{
    // use a foreach loop to loop through the cities array, rather than type out the same code for every city
    foreach($cities as $city_name => $city_distance)
    {
        $time = round( ($city_distance / 60), 2);
        $walktime = round( ($city_distance / 5), 2);
        print "<p>The distance between Chicago and <b>$city_name</b> is <b>$city_distance</b> miles.<br />";
        print "Driving at 60 miles per hour it would take <b>$time</b> hours.<br />";
        print "Walking at 5 miles per hour it would take <b>$walktime</b> hours.</p>";
    }
}
elseif(isset($_POST['destination']) && isset($cities[$_POST['destination']]))
{
    $city_name = $_POST['destination'];
    $city_distance = $cities[$city_name];
    $time = round( ($city_distance / 60), 2);
    $walktime = round( ($city_distance / 5), 2);
    print "<p>The distance between Chicago and <b>$city_name</b> is <b>$city_distance</b> miles.<br />";
    print "Driving at 60 miles per hour it would take <b>$time</b> hours.<br />";
    print "Walking at 5 miles per hour it would take <b>$walktime</b> hours.</p>";
}
else
{
    echo 'Sorry, do not have destination information for';
}

?>

Link to comment
https://forums.phpfreaks.com/topic/98320-syntax-errors/#findComment-503156
Share on other sites

The php code is for the second part.

 

Also I just looked at your HTML code and your HTML is invalid so that maybe why the code is not working. Use the following HTML code instead.

<html>
<head><title>Distance from Chicago!</title></head>
<body>
<h1>Welcome to the Distance Calculation Page.</h1>

<p>The page that calculates the distances from Chicago!</p>

<form action="http://localhost/julie/c5-e4a.php" method="post">
<p>
  <strong>Destination</strong>:<br />
  <select name="destination" size="5">
    <option value="Boston">Boston</option>
    <option value="Dallas">Dallas</option>
    <option value="Las Vegas">Las Vegas</option>
    <option value="Miami">Miami</option>
    <option value="Nashville">Nashville</option>
    <option value="Pittsburgh">Pittsburgh</option>
    <option value="San Francisco">San Francisco</option>
    <option value="Toronto">Toronto</option>
  </select>
</p>

<p>To see distance information for every city, click here <input type="checkbox" name="location" value="yes"></p>
<input type="SUBMIT" value="Click To Submit"><input type = "RESET" value = "Reset" >
</form>

</body>
</html>

 

If the code still does not work (it has been tested and works for me) then make sure you set error_reporting to E_ALL and set display_errors to On within the php.ini. Save the php.ini and restart your server software (Apache or IIS or whatever server you're using).

 

When developing PHP scripts error_reporting should be set to atleast E_ALL and display_errors turned on this makes it easier to debug code when nothing happens. Also can you post your PHP version too.

Link to comment
https://forums.phpfreaks.com/topic/98320-syntax-errors/#findComment-503202
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.