0

Ok guys i am working on a program that will compute the distance in miles from Chicago or dallas(radio buttons)to specific destinations. The destination cities are in a selection list. The output should be the distance between the two cities, the time it would take to drive at 60 mph. and the time it would take to walk at 5 mph. the following code that i wrote only displays the headings for output. Please tell me where i am going wrong with this.

<?php
$cities =array ('Dallas' => 803, 'Toronto' => 435, 'Boston' => 848, 'Nashville' => 406, 'Las Vegas' => 1526, 'San Francisco' => 1835, 
				'Washington, DC' => 595, 'Miami' => 1189, 'Pittsburgh' => 409);
$cities2 = array ('Chicago' => 803, 'Toronto' => 1202, 'Boston' => 1548, 'Nashville' => 615, 'Las Vegas' => 1073, 'San Francisco' => 1483, 
				'Washington, DC' => 1181, 'Miami' => 1107, 'Pittsburgh' => 1068);
				
	if (isset($_POST['dal'])  && isset($cities['destination'])) {
		$distance = $cities['destination'];
	if (isset($_POST['chi']) && isset($cities['destination'])){
		$distance = $cities2['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 hr. it would take $time hrs.";
		print "<br> Walking at 5 Miles per hr. it would take $walktime hrs.";}
	else {
		print "Please select a Starting city and a Destination.";}}
	
				

 
?>
3
Contributors
11
Replies
12
Views
6 Years
Discussion Span
Last Post by jetlife76
0

I can see a problem with the structure of your if/else conditions. Currently your innermost code where you calculate $time and $walktime and print your outputs will only be executed if $_POST and $_POST are both set. If I'm understanding what you want to do correctly, the user would select either 'Chicago' or 'Dallas', but not both.

So the first thing you'll need to fix will be changing your code structure to be

if (isset($_POST['dal']) && isset($cities['destination']))
{
  //Put your code for Dallas as the selection here
}
else if (isset($_POST['chi']) && isset($cities['destination']))
{
  //Put your code for Chicago as the selection here
}
else
{
  print "Please select a Starting city and a Destination.";
}

A few other things you'll probably want to check for:
-Are the Starting city and the Destination city the same? You'll probably want to add 'Chicago' => 0 to $cities and 'Dallas' => 0 to $cities2 or else you'll get an error.
-Where are you setting the value for $cities? Do you mean to be checking isset($_POST) instead? If that's the case, you'll want to alter the code where you set $distance to be more like:

$distance = $cities[$_POST['destination']]

...assuming that the value of $_POST is a value in your $cities or $cities2 arrays.

Other than that, the idea of how you will find the distance between to cities looks to be pretty solid. It looks like you just need to tweak the structure a bit for everything to work.

Also, for what it's worth, the Google Maps API offers this kind of functionality. That may be too involved for what you're trying to do here, but if this is part of something larger where you may eventually have to add a wider variety of Starting and Destination cities, it would be worth checking out.

Edited by TySkby: n/a

0

OK sounds good. i ve restructured my code as you suggested but now am confused on where to place my "{}" because i keep getting a unexpected "{" error, i know it has something to do with my variables that i've set for ($time, $walktime and the print statements)

0

Here's what i have now, and i'm getting an unexpected elseif error

<?php
$cities =array ('Chicago' => 0,'Dallas' => 803, 'Toronto' => 435, 'Boston' => 848, 'Nashville' => 406, 'Las Vegas' => 1526, 'San Francisco' => 1835, 
				'Washington, DC' => 595, 'Miami' => 1189, 'Pittsburgh' => 409);
				
$cities2 = array ('Dallas' => 0,'Chicago' => 803, 'Toronto' => 1202, 'Boston' => 1548, 'Nashville' => 615, 'Las Vegas' => 1073, 'San Francisco' => 1483, 
				'Washington, DC' => 1181, 'Miami' => 1107, 'Pittsburgh' => 1068);
				
	if (isset($_POST['dal'])  && isset($cities['destination'])) {
	$distance = $cities[$_POST['destination']];
	
	elseif (isset($_POST['chi']) && isset($cities['destination'])){
	$distance = $cities2[$_POST['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 hr. it would take $time hrs.";
		print "<br> Walking at 5 Miles per hr. it would take $walktime hrs.";}
	else {
		print "Please select a Starting city and a Destination.";}
	
				

 
?>
0

You are missing a curly bracket at the end of line 9 jn the above code.

$distance = $cities[$_POST['destination']]};
0

Ok i tried it the way you suggested with the missing curly bracket and i kept getting an "Unexpected elseif", so i tinkered with it a bit and here's what i have. Now my output is always my error message that is printed after the "Else",

<?php
$cities =array ('Chicago' => 0,'Dallas' => 803, 'Toronto' => 435, 'Boston' => 848, 'Nashville' => 406, 'Las Vegas' => 1526, 'San Francisco' => 1835, 
				'Washington, DC' => 595, 'Miami' => 1189, 'Pittsburgh' => 409);
				
$cities2 = array ('Dallas' => 0,'Chicago' => 803, 'Toronto' => 1202, 'Boston' => 1548, 'Nashville' => 615, 'Las Vegas' => 1073, 'San Francisco' => 1483, 
				'Washington, DC' => 1181, 'Miami' => 1107, 'Pittsburgh' => 1068);
$destination = $_POST['destination'];
				
	if (isset($_POST['dal'])  && isset($_POST[$destination])) {
	$distance = $cities[$_POST[$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 hr. it would take $time hrs.";
		print "<br> Walking at 5 Miles per hr. it would take $walktime hrs.";}
		
	elseif (isset($_POST['chi']) && isset($_POST[$destination])){
	$distance2 = $cities2[$_POST[$destination]];	
	$time2 = round(($distance2 / 60), 2);
	$walktime2 = round(($distance2 / 5), 2);
		print "The distance between Chicago and <i>[$destination]</i> is $distance2 miles.";
		print "<br> Driving at 60 Miles per hr. it would take $time2 hrs.";
		print "<br> Walking at 5 Miles per hr. it would take $walktime2 hrs.";}
	
	else {
		print "Please select a Starting city and a Destination.";}


 
?>
0

On line 7 of your code, you set

$destination = $_POST['destination']

That is correct, but now you don't need to use the $_POST variable anymore to reference that value. So the problem is in lines 9, 10, 17, and 18 where you are doing $_POST[$destination] because what that really works out to is $_POST[$_POST].

So if you alter your references a bit, you should be good. Try applying this to the lines mentioned above (here I'm using what should go on lines 9 and 10):

if (isset($_POST['dal'])  && isset($_POST['destination'])) {
	$distance = $cities[$destination];
0

yea i noticed that as well, i made that change and it works fine, my only other question is, that my radio buttons won't alternate when i try to select them, if i select dallas then try to select chicago, they both are selected instead of one or the other.

0

Could this be an issue with my input form? Is it because my buttons have different names? I'm trying different adjustments to find the problem.

0

Ok i tried to give the buttons the same name, this fixed the issue of the buttons not alternating but now it only gives me the destinations from chicago. I hope i do not have to redo my entire logic because of these radio buttons, do you have any suggestions.

0

I don't think you need to change anything except a few tweaks to your HTML form and the way PHP handles the value from the radio buttons.

Both radio button elements should have identical name attributes. In the value attributes, one button should be set to 'Chicago' and the other to 'Dallas'. So in your form you'd have something like this:

<input type="radio" name="origin" value="Chicago" /> Chicago <br />
<input type="radio" name="origin" value="Dallas" /> Dallas <br />

Then, you should add another variable (I'll call it $origin for consistency) and set it the same way you set the $destination variable, like this:

$origin = $_POST['origin'];
$destination = $_POST['destination'];

In your if/else statements, you can then do something like

if ($origin == 'Dallas' && isset($_POST['destination'])
{
  //Dallas code
}
else if ($origin == 'Chicago' && isset($_POST['destination'])
{
  //Chicago code
}
else
{
  //...
}

One last thing I'd recommend is that you do your isset() checks where you set the $destination and $origin variables.

//Declare you variables as empty here to prevent errors
$origin;
$destination;

//Check that there is a value for 'origin' and 'destination'
if (isset($_POST['origin']) && isset($_POST['destination']))
{
  $origin = $_POST['origin'];
  $destination = $_POST['destination'];
}

That will allow you to keep things neat and separated by doing all of your form validation in one place, and will also prevent errors from showing up in case the user failed to make a selection for 'origin' or 'destination'.

Hope that helps!

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.