Jump to content
MakeWebGames

location times


Recommended Posts

Hi guys

I'm trying to add into our travel system different travel times based on where your travelling from and where your travelling to. Now in theory I have the general concept down, however the way i'm currently doing it seems veryyyy long winded and not very server friendly.

I've got this so far:

 

if($ir['location'] == 1 && $_GET['to'] == 2)
{
$time = "5";
}
else if ($ir['location'] == 1 && $_GET['to'] == 3)
{
$time = "15";
}
if($ir['location'] == 1 && $_GET['to'] == 4)
{
$time = "10";
}

 

and this goes on until to == 8.

Now this is only for calculating the travelling time from location 1 and i want to calculate travelling times for the other seven locations as well, and im thinking repeating what I've got above, 7 more times, is not the best way to do it.

Anybody got another method that i'm missing here?

thanks in advance

Link to comment
Share on other sites

i was thinking about this after i posted while i was driving to the gym....i could just add the infor to the databse, then do a query to select from the table where user location = 1 and travelling to location = 2 and so on. Simple!

Now just got to spend time going through every combination of travelling and put it in the database as a new table!

Thanks for the input Kyle, good idea but probably not ideal for what I'm looking for atm.

Link to comment
Share on other sites

Could do something like.

<?php


$locationTimes = array (
   1 => array (
       2 => 10,
       3 => 15,
       4 => 5,
       5 => 3,
       6 => 2,
       7 => 20,
   ),
   2 => array (
       1 => 10,
       3 => 15,
       4 => 5,
       5 => 3,
       6 => 2,
       7 => 20,
   ),
   3 => array (
       1 => 10,
       2 => 15,
       4 => 5,
       5 => 3,
       6 => 2,
       7 => 20,
   ),
   4 => array (
       1 => 10,
       2 => 15,
       3 => 5,
       5 => 3,
       6 => 2,
       7 => 20,
   ),
   5 => array (
       1 => 10,
       2 => 15,
       3 => 5,
       4 => 3,
       6 => 2,
       7 => 20,
   ),
   6 => array (
       1 => 10,
       2 => 15,
       3 => 5,
       4 => 3,
       5 => 2,
       7 => 20,
   ),
   7 => array (
       1 => 10,
       2 => 15,
       3 => 5,
       4 => 3,
       5 => 2,
       6 => 20,
   ),
);

if ( array_key_exists ( $ir['location'], $locationTimes ) && array_key_exists ( $_GET['to'], $locationTimes[$ir['location']] ) ) {
   $time = $locationTimes[$ir['location']][$_GET['to']];
} else {
   $time = 20; //default.
}

Array of all locations ( ID ), then for each location have an array of other locations ( ID ) and the respective time.

Link to comment
Share on other sites

that's a pretty easy straight forward way :p.

or create a hypothetical grid map and each location is on its own cell like A(3, 2) B(1,-3) now you take the distance between each x cord^ + each y cord^ and get the square root of that value. then you need to determine travel speed and thee you go.

btw the answer is 3-1=2^= 4 + 2- -3=5^ =25. 4+25=29. square root of 29= 5.4 rounded up

Edited by KyleMassacre
typo
Link to comment
Share on other sites

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.   Paste as plain text instead

  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...