Jump to content

[SOLVED] less than, greater than


coolphpdude

Recommended Posts

Hi there,

 

I am building a search query as i go by giving the user a number of different search options via a drop down box. I have removed all of the other search options that i am giving the user as they are irrelivent to what i am asking. The code I have so far is:

 

FORM:

echo		  "<select name='minmark'>";
echo		    "<option value='0'>No Preference</option>";
echo		    "<option value='20'>20</option>";
echo		    "<option value='25'>25</option>";
echo		    "<option value='30'>30</option>";
echo		    "<option value='35'>35</option>";
echo		    "<option value='40'>40</option>";
echo		    "<option value='45'>45</option>";
echo		    "<option value='50'>50</option>";
echo		    "<option value='60'>60</option>";
echo		    "<option value='70'>70</option>";
echo		    "<option value='80'>80</option>";
echo		    "<option value='90'>90</option>";
echo		    "<option value='100'>100</option>";

echo		  "<select name='maxmark'>";
echo		    "<option value='0'>No Preference</option>";
echo		    "<option value='20'>20</option>";
echo		    "<option value='25'>25</option>";
echo		    "<option value='30'>30</option>";
echo		    "<option value='35'>35</option>";
echo		    "<option value='40'>40</option>";
echo		    "<option value='45'>45</option>";
echo		    "<option value='50'>50</option>";
echo		    "<option value='60'>60</option>";
echo		    "<option value='70'>70</option>";
echo		    "<option value='80'>80</option>";
echo		    "<option value='90'>90</option>";
echo		    "<option value='100'>100</option>";

 

 

CODE:

$querystring = "SELECT * FROM grades WHERE 1";


//test query

if ($minmark != '0')
{
$querystring .= " AND mark>='$minmark'";
}

if ($maxmark != '0')
{
$querystring .= " AND mark<='$maxmark'";
}


echo "DEBUG: query = $querystring";

 

What i am trying to do is give the user the option of choosing to search for all records where a mark is within a minimum chosen mark and a maximum to chosen mark (the two drop down menu's from my form code), however, if the user chooses no preference it should search with no preference! My code is not returning correct results. Cany anyone tell me where i am going wrong??

Link to comment
https://forums.phpfreaks.com/topic/71647-solved-less-than-greater-than/
Share on other sites

try this

 

 

$querystring = "SELECT * FROM grades ";


//test query

if ($minmark != '0')
{
  $where=true;
  $querystring .= "WHERE (mark >= $minmark)";
}

if ($maxmark != '0')
{
  if ($where) {
    $querystring .= " AND (mark<=$maxmark)";
  } else {
    $querystring .= "WHERE (mark <= $maxmark)";
  }
}


echo "DEBUG: query = $querystring";

 

 

Remember when your looking at numeric values and > and < you dont need the quotes..

 

Regards

Liam

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.