Cracked it. It might have fried my brain but I got there.
Code: Select all
//Fetch the prices and let the user choose a range
$query = 'SELECT MIN(price), MAX(price) FROM cars'; //Build the query
$result = mysql_query($query); //Execute it
//If more than one record was returned
if(mysql_num_rows($result) > 0) {
echo '<span class="title">Price Range</span><br />'; //Show the search links list title
//Set the lowest and highest pricest
$lowest = mysql_result($result, 0, 0);
$highest = mysql_result($result, 0, 1);
echo (floor($lowest / 1000) * 1000).' - '.((floor($lowest / 1000) * 1000) + 999).'<br />';
echo '...<br />';
echo (floor($highest / 1000) * 1000).' - '.((floor($highest / 1000) * 1000) + 999).'<br />';
}
Could've used ceil for the highest range one and then switched the method but I thought I'd stick to what worked and thus save a little time
Haven't put the loop in yet, but I've got it working to as much of a degree so that it's now a no brainer to complete
Edit:
My finished snippet:
Code: Select all
//Fetch the prices and let the user choose a range
$query = 'SELECT MIN(price), MAX(price) FROM cars'; //Build the query
$result = mysql_query($query); //Execute it
//If more than one record was returned
if(mysql_num_rows($result) > 0) {
echo '<span class="title">Price Range</span><br />'; //Show the search links list title
//Set the range start and end
$rangestart = floor(mysql_result($result, 0, 0) / 1000) * 1000;
$rangeend = ceil(mysql_result($result, 0, 1) / 1000) * 1000;
//Loop through all the ranges
for($range = $rangestart; $range < $rangeend; $range += 1000) {
//Fetch the amount of vehicles that are in the price range
$query = "SELECT COUNT(*) FROM cars WHERE price BETWEEN " . $range . " AND " . ($range + 999); //Build the query
$amount = mysql_query($query); //Execute it
//Show the range link
echo '<a href="viewcar.php?field=price&criteria=' . $range . '-' . ($range + 999) . '">£' . $range . ' - £' . ($range + 999) . '</a> (' . mysql_result($amount, 0) . ')<br />';
}
echo '<hr />'; //Show a horizontal rule
}
Is there a better way to find out how many cars are in each price range without executing a query on every loop?