Jump to content

Order/Sort mysql results


amg182

Recommended Posts

Hi guys,  Having problems sorting mysql results. I can easily order/sort table columns but would like to order the results by a variable.(Distance in Miles).

Havent much experience in PHP and really struggling with this. Basically a query echoing out car make and models and how far they are located from a postcode. Can get it to work no porblem, but not to order/sort by least distance.

 

Heres what i'm working with:

$res=mysql_query("SELECT * FROM cars");
while ($row = mysql_fetch_assoc($res)){

echo $row['Make'];
echo $row['Model'];

//variable will be determimed by user input form
$postcode="W12 3SR";

$start=$postcode;
$ends = array();

//finds value from database field
{
$ends[]=$row['Location'];
}

// Postcode entered by user via input form
$result=mysql_query("SELECT * FROM postcodes WHERE Pcode='$start'");
while($row=mysql_fetch_array($result)){
$gridn[0]=$row['Grid_N'];
$gride[0]=$row['Grid_E'];
}

foreach($ends as $fin){
// Postcodes within mysql table
$result=mysql_query("SELECT * FROM postcodes WHERE Pcode='$fin'");
while($row=mysql_fetch_array($result)){
	$gridn[1]=$row['Grid_N'];
	$gride[1]=$row['Grid_E'];
}

// TAKE GRID REFS FROM EACH OTHER TO WORK OUT DISTANCE.
$distance_n=$gridn[0]-$gridn[1];
$distance_e=$gride[0]-$gride[1];

// CALCULATE THE DISTANCE BETWEEN THE TWO POSTCODES AND DIVIDE BY 1.6 TO CONVERT KM TO MILES
$hypot=sqrt(($distance_n*$distance_n)+($distance_e*$distance_e))/1.609;

//VARIABLE FOR DISTANCE AND ROUNDED OF TO NEAREST WHOLE NUMBER.
$distance=''.round($hypot/1000,0).'';


  echo " $distance miles";
  echo "<br>";
}
}
?>

 

I wish i could just do something like this but isnt possible, is it :shrug:?

"Select * FROM cars ORDER BY $distance";

 

Thanks!

Link to comment
Share on other sites

In order to sort by a distance within MySQL, you would need to build the function inside MySQL.  There are ways to do that, but they can get confusing.  This tutorial should get you started, although you will have to make note that he isn't storing his data like you are.  So it would have to be modified.

Link to comment
Share on other sites

Ok, that makes sense. I have been trying to sort mysql results using sort functions, but no luck yet. My lack of experince is letting me down. :-[

 

I will keep trying... :-\

 

This has already been answered in your previous post. Either:

 

1. Build the logic into MySQL to calculate the distance and create a dynamic value to use the MySQL sorting functions on or

 

2. Get all the results from MySQL then process the data into a multi-dimensional array (while also creating the distance value for each record). Then you can sort the results using one of the PHP sorting functions. However, as I stated in your earlier post, this will not allow you to use the queries for pagination. Since you need to use LIMIT to get the correct results for the page AFTER they are sorted. So, if you go this route, you will need to extract the correct "page" of results from the array of all records.

Link to comment
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.