julien Posted March 28, 2008 Share Posted March 28, 2008 I am writing php code and when I view it on my testing site, it says unexpected else in line 71. I've tried adding/removing brackets, adding if so its elseif, but nothing seems to work. HELP! Quote Link to comment https://forums.phpfreaks.com/topic/98320-syntax-errors/ Share on other sites More sharing options...
wildteen88 Posted March 28, 2008 Share Posted March 28, 2008 Can you please post the necessary lines, I'd recommend posting lines 60 - 75 You maybe closing the if statement prematurely. Quote Link to comment https://forums.phpfreaks.com/topic/98320-syntax-errors/#findComment-503090 Share on other sites More sharing options...
dmccabe Posted March 28, 2008 Share Posted March 28, 2008 You need to show us your code otherwise we cant help Quote Link to comment https://forums.phpfreaks.com/topic/98320-syntax-errors/#findComment-503096 Share on other sites More sharing options...
julien Posted March 28, 2008 Author Share Posted March 28, 2008 $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> Quote Link to comment https://forums.phpfreaks.com/topic/98320-syntax-errors/#findComment-503098 Share on other sites More sharing options...
timmy0320 Posted March 28, 2008 Share Posted March 28, 2008 Place your whole if statement or are you even using IF? You can't use else without using if. Quote Link to comment https://forums.phpfreaks.com/topic/98320-syntax-errors/#findComment-503106 Share on other sites More sharing options...
julien Posted March 28, 2008 Author Share Posted March 28, 2008 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> Quote Link to comment https://forums.phpfreaks.com/topic/98320-syntax-errors/#findComment-503109 Share on other sites More sharing options...
wildteen88 Posted March 28, 2008 Share Posted March 28, 2008 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."; } Quote Link to comment https://forums.phpfreaks.com/topic/98320-syntax-errors/#findComment-503114 Share on other sites More sharing options...
julien Posted March 28, 2008 Author Share Posted March 28, 2008 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. Quote Link to comment https://forums.phpfreaks.com/topic/98320-syntax-errors/#findComment-503128 Share on other sites More sharing options...
wildteen88 Posted March 28, 2008 Share Posted March 28, 2008 What do you want your script do? Please explain more. Quote Link to comment https://forums.phpfreaks.com/topic/98320-syntax-errors/#findComment-503130 Share on other sites More sharing options...
julien Posted March 28, 2008 Author Share Posted March 28, 2008 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 Quote Link to comment https://forums.phpfreaks.com/topic/98320-syntax-errors/#findComment-503133 Share on other sites More sharing options...
wildteen88 Posted March 28, 2008 Share Posted March 28, 2008 Yes post the other part too. When you post your code wrap it in code tags ( ) Quote Link to comment https://forums.phpfreaks.com/topic/98320-syntax-errors/#findComment-503135 Share on other sites More sharing options...
julien Posted March 28, 2008 Author Share Posted March 28, 2008 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> Quote Link to comment https://forums.phpfreaks.com/topic/98320-syntax-errors/#findComment-503143 Share on other sites More sharing options...
wildteen88 Posted March 28, 2008 Share Posted March 28, 2008 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'; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/98320-syntax-errors/#findComment-503156 Share on other sites More sharing options...
julien Posted March 28, 2008 Author Share Posted March 28, 2008 is that is, cause its not working right Quote Link to comment https://forums.phpfreaks.com/topic/98320-syntax-errors/#findComment-503168 Share on other sites More sharing options...
wildteen88 Posted March 28, 2008 Share Posted March 28, 2008 That is the complete code yes. Why whats wrong? Quote Link to comment https://forums.phpfreaks.com/topic/98320-syntax-errors/#findComment-503175 Share on other sites More sharing options...
julien Posted March 28, 2008 Author Share Posted March 28, 2008 1 question: does this take the place of both sections of code i showed you earlier, or does it replace just the second part. Cause right now, when i click on a city or the checkbox, nothing is displayed. the screen goes white Quote Link to comment https://forums.phpfreaks.com/topic/98320-syntax-errors/#findComment-503182 Share on other sites More sharing options...
wildteen88 Posted March 28, 2008 Share Posted March 28, 2008 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. Quote Link to comment https://forums.phpfreaks.com/topic/98320-syntax-errors/#findComment-503202 Share on other sites More sharing options...
julien Posted March 28, 2008 Author Share Posted March 28, 2008 It worked! Thank you so much!!! Quote Link to comment https://forums.phpfreaks.com/topic/98320-syntax-errors/#findComment-503219 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.