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

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.