Jump to content

Archived

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

jarvis

MySQL Between Statament in PHP drop down

Recommended Posts

Hi,

I've a menu that creates a drop down of prices from a db. The field is called rent. People add in the rental amount for example, 130.00 or 750.00 or 1000.00

I'm having trouble trying to get the drop down to work though. What I want is the drop down to display options of
0 -500
501 - 750
751 - 1000
I've put the details in the options but can't work out how to do the select statement to do the between part e.g.
[code]$query = "SELECT add1, property_id, property_type,rent,bedrooms, image_name,pcode FROM properties WHERE  ((rent < '$r1') OR  (rent BETWEEN '$r1' AND '$r2')) ";[/code]

I've got the values from
[code]    //check for rental amount no (optional)
    if (empty($_POST['rent'])) {
        $r .="'" . escape_data
            ($_POST['rent']) . "' ";
    }else{
        $r = escape_data($_POST['rent']);
    }
    $r1 = 500;
    $r2 = 750;
    $r3 = 1000; [/code]

My drop down is this
[code]      <tr>
        <td class="bodytext">
          <select name="rent">
            <option></option>
            <option value="$r1">0 - 500</option>
            <option value="$r2">501 - 750</option>
            <option value="$r3">751 - 1000</option>
            <option>1000+</option>
          </select></td>
      </tr> [/code]

Can someone please help?

Thanks in advanced!

Share this post


Link to post
Share on other sites
I'd do something like

[code]switch ($_POST['rent']) {
        case '500':
              $lo = 0;
              $hi = 500;
              break;
        case '750':
              $lo = 500;
              $hi = 750;
              break;
        case '1000':
              $lo = 750;
              $hi = 1000;
              break;
        case '1000+':
              $lo = 1000;
              $hi = 99999;
              break;
}
$query = "SELECT add1, property_id, property_type,rent,bedrooms, image_name,pcode
          FROM properties
          WHERE  rent BETWEEN $lo AND $hi ";[/code]

Share this post


Link to post
Share on other sites
Wow! Thanks, I never knew you could do that!
Am now trying to make my statement with this
[code] $query = "SELECT add1, property_id, property_type,rent,bedrooms, image_name,pcode FROM properties
          WHERE  (rent BETWEEN $lo AND $hi) OR (bedrooms='$bn')";[/code]

The first part works thanks to Barands switch case statement. I've this for the bedrooms drop down
[code]      if (empty($_POST['beds'])) {
        $bn .="'" . escape_data
            ($_POST['beds']) . "' ";
    }else{
        $bn = escape_data($_POST['beds']);
    }  [/code]

And this for the drop down
[code]      <tr>
        <td class="bodytext">
          <select name="beds">
            <option></option>
            <option>Zero</option>
            <option>1</option>
            <option>2</option>
            <option>3</option>
            <option>4</option>
            <option>5</option>
            <option>6</option>
          </select></td>
      </tr> [/code]

Am I missing the obvious?

Share this post


Link to post
Share on other sites
Is this just due to the brackets in the wrong place?

[code]WHERE  (rent BETWEEN $lo AND $hi) OR (bedrooms='$bn')"; [/code]

Have tried moving the brackets about but still to no avail!

Anyone?

Share this post


Link to post
Share on other sites
You don't say what's wrong.

But if you are looking for a 3 bedroom villa with a rental between 500 and 750 the query would be

[code]... WHERE (rent BETWEEN $lo and $hi) AND (bedrooms >= $bn)[/code]

Share this post


Link to post
Share on other sites
Sorry, what was happening was that the rent side would work if you searched by rent but if you searched by no of rooms it wouldn't work. I want it to be an OR statement. I think it's a problem with syntax though they should be ..."WHERE (bedrooms='$bn' OR rent BETWEEN '$lo' and '$hi')";

I think this is the right way!

Share this post


Link to post
Share on other sites

×

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.