coolphpdude Posted October 3, 2007 Share Posted October 3, 2007 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?? Quote Link to comment https://forums.phpfreaks.com/topic/71647-solved-less-than-greater-than/ Share on other sites More sharing options...
shocker-z Posted October 3, 2007 Share Posted October 3, 2007 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 Quote Link to comment https://forums.phpfreaks.com/topic/71647-solved-less-than-greater-than/#findComment-360690 Share on other sites More sharing options...
coolphpdude Posted October 3, 2007 Author Share Posted October 3, 2007 i'll give that a try. Cheers Quote Link to comment https://forums.phpfreaks.com/topic/71647-solved-less-than-greater-than/#findComment-360692 Share on other sites More sharing options...
coolphpdude Posted October 3, 2007 Author Share Posted October 3, 2007 Thanks mate that works a treat!! Quote Link to comment https://forums.phpfreaks.com/topic/71647-solved-less-than-greater-than/#findComment-360702 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.