1

For training purposes i need to make a function which tells me the 'travel cost' between 2 cities. The book tells me to type this function:

<?php
function travelcost($start, $destination)
{
$travelcost = array();
$travelcost[1] = array();
$travelcost[2] = array();
$travelcost[3] = array();
$travelcost[4] = array();
$travelcost[1][1] = 0;
$travelcost[1][2] = 30;
$travelcost[1][3] = 60;
$travelcost[1][4] = 90;

echo($travelcost[$start][$destination] . " Euro's");
}
?>

In addition i've created this form to ask for a start and a destination:

<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
Start: <select name="start" value="true">
<option value="start[]">Amsterdam</option>
<option value="start[]">Utrecht</option>
<option value="start[]">Den Haag</option>
<option value="start[]">Rotterdam</option>
</select>

Destination: <select name="destination" value="true">
<option value="destination[]">Amsterdam</option>
<option value="destination[]">Utrecht</option>
<option value="destination[]">Den Haag</option>
<option value="destination[]">Rotterdam</option>
</select>
<p><input type="submit" name="calculate" value="Calculate"</p>
</form>

Followed by:

<?php

if(isset($_POST["start"])&& isset($_POST["destination"]))
{
travelcost($_POST['start'], $_POST['destination']);
}

?>

This gives me Undefined index: start[]

I know im doing it wrong, but i just can't see the logic in the function and the array. I assume the function is correct because it's right out of the book but i'm also not sure about that.

Can someone help me out?

4
  • Indeed it does. But i am trying to get the function to work. the function ends with an echo. the error says that on that line "start" is undifined Commented Dec 8, 2015 at 15:09
  • I'm trying to be a little bit ironic.. Sincerely I would not like to maintain such code.. no offense.. please take a look at the code next year.. if you understand anything in less than 10 minutes you will have a beer from me... Commented Dec 8, 2015 at 15:23
  • While just starting, i also absolutely don't think this is the way to go. Thing is, i'm learning by a book (no school) and the book wants me to do it this way. The book is somewhat old, and i know there are better ways then the book tells me. Plan is to finish the book, and then to learn 'the right way' online. For now my goal is to understand how things work, and what they do. Asking my questions here, and getting the awnsers truly help me understand it and enable me to go futher with learning. Commented Dec 8, 2015 at 15:28
  • Ok I understand.. good luck! Though I recommend you to find a new book Commented Dec 8, 2015 at 15:48

1 Answer 1

1

This is wrong,

<option value="start[]">Amsterdam</option>
              ^      ^

It should be

<option value="start">Amsterdam</option>

or

<option value="Amsterdam">Amsterdam</option>

Same for all options in start and destination.

According to your function `travelcost(), your select should be

Start: <select name="start" value="true">
    <option value="1">Amsterdam</option>
    <option value="1">Utrecht</option>
    <option value="1">Den Haag</option>
    <option value="1">Rotterdam</option>
</select>

Destination: <select name="destination" value="true">
    <option value="1">Amsterdam</option>
    <option value="2">Utrecht</option>
    <option value="3">Den Haag</option>
    <option value="4">Rotterdam</option>
</select>
Sign up to request clarification or add additional context in comments.

6 Comments

Thanks for the quick response! I've changed it to "start", but it is still undefined in the function.
values for all options is same??
no it should fill up the array that is used in the function. o darn. value should be what is in the array, i meant the name to be the same
According to your function, value for start should be 1 and destination 1 to 4...
Thanks! That did the trick! Now that it works i can try and find out why it works ;-) Somehow i just don't get the logic of it.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.