Jump to content

MySQL Between Statament in PHP drop down


jarvis

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