Jump to content

[SOLVED] Simpler way of doing this???


ukweb

Recommended Posts

Hi All

 

I am making a site for a client, which sells motorbikes (see http://ktm-offroad.route55motorcycles.com/?file=vehicle_search

 

On one side I am making a 'narrow results' section which will, obviously narrow down the number of bikes returned.

 

I am making a filter for maximum price. The maximum price obviously changes depending on the stock being held, so I want the drop down box to list options at equal intevals from the price of the most expensive bike down to say £1000.

 

For example, if the most expensive bike on record is £12,041

  • The system makes the max price selectable as £12,500
  • Then for every £500 inteval outputs '<option value=$value>....
  • Stops when £1000 is reached

 

in psudo code this is how I am planning to do this, I'm sure if this is the best way, any ideas welcome

[*]Run query and return the highest value bike

[*]Round the value up to nearest £500

[*]Run for loop which outputs an option tag for that value, deduct 500

[*]Stop at £1000

 

Any ideas anyone? am I approaching this from the right angle?

Link to comment
https://forums.phpfreaks.com/topic/176304-solved-simpler-way-of-doing-this/
Share on other sites

Here's an example:

 

<?php

$max_price = 11215;
$cur_price = ceil($max_price / 500) * 500;

while ($cur_price >= 1000)
{
    echo $cur_price . '<br />';
    $cur_price = $cur_price - 500;
}

?>

 

Nice one, never come across the ceil function before!

<?php
// way 1
function formulateDropDown($m) {
$arr = array($num = 0);
while ($m > ($num += 500)) $arr[] = $num;
return $arr;
}
$s = mysql_query("SELECT MAX(price) As max, id, name, description FROM products");
$loop = 0;
while ($r = mysql_fetch_assoc($s)) {
if ($loop = 0) {
	$drop = formulateDropDown($r['max']);
	$loop++;
}
// do whatever with your results.
}
?>

 

<?php
// way 2
function formulateDropDown($m) {
$arr = array($num = 0);
while ($m > ($num += 500)) $arr[] = $num;
return $arr;
}
$s = mysql_query("SELECT MAX(price) As max FROM products LIMIT 1");
$drop = mysql_result($s,0);
$s2 = mysql_query("SELECT * FROM products");
while ($r = mysql_fetch_assoc($s2)) {
// do whatever with your results.
}
?>

 

not sure which was is faster :)

 

Archived

This topic is now archived and is closed to further replies.

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