Jump to content


Photo

MySQL Between Statament in PHP drop down


  • Please log in to reply
5 replies to this topic

#1 jarvis

jarvis
  • Members
  • PipPipPip
  • Advanced Member
  • 420 posts

Posted 07 March 2006 - 02:24 PM

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.
$query = "SELECT add1, property_id, property_type,rent,bedrooms, image_name,pcode FROM properties WHERE  ((rent < '$r1') OR  (rent BETWEEN '$r1' AND '$r2')) ";

I've got the values from
    //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;

My drop down is this
      <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>

Can someone please help?

Thanks in advanced!

#2 Barand

Barand
  • Moderators
  • Sen . ( ile || sei )
  • 18,023 posts

Posted 07 March 2006 - 07:22 PM

I'd do something like

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 ";

If you are still using mysql_ functions, STOP! Use mysqli_ or PDO. The longer you leave it the more you will have to rewrite.

Donations gratefully received






moon.png

|baaGrid| easy data tables - and more
|baaChart| easy line, column and pie charts

#3 jarvis

jarvis
  • Members
  • PipPipPip
  • Advanced Member
  • 420 posts

Posted 08 March 2006 - 02:12 PM

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

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

And this for the drop down
      <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>

Am I missing the obvious?

#4 jarvis

jarvis
  • Members
  • PipPipPip
  • Advanced Member
  • 420 posts

Posted 09 March 2006 - 10:18 AM

Is this just due to the brackets in the wrong place?

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

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

Anyone?

#5 Barand

Barand
  • Moderators
  • Sen . ( ile || sei )
  • 18,023 posts

Posted 09 March 2006 - 01:18 PM

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

... WHERE (rent BETWEEN $lo and $hi) AND (bedrooms >= $bn)

If you are still using mysql_ functions, STOP! Use mysqli_ or PDO. The longer you leave it the more you will have to rewrite.

Donations gratefully received






moon.png

|baaGrid| easy data tables - and more
|baaChart| easy line, column and pie charts

#6 jarvis

jarvis
  • Members
  • PipPipPip
  • Advanced Member
  • 420 posts

Posted 10 March 2006 - 08:55 AM

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!




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users