Note that there are some explanatory texts on larger screens.

plurals
  1. POProper code to filter a price range in PHP / SQL Query
    primarykey
    data
    text
    <p>Really hoping someone can help me with this. I'm building a PHP / MySQL search form that, hopefully, will allow users to search our wine database and filter the results according to a range of prices selected via a drop down menu. </p> <p>The form works fine searching for, and returning, a nice list of accurate results. But it does NOT price filter the results. </p> <p>After days of searching and experimenting, I've mashed together various code snippets to get this far but, overall, PHP is still very much a mystery to me. </p> <p>It's the correct coding and syntax I struggle with.</p> <p>How might I code the PHP posted here to properly integrate the Price Range filter? I suspect my inclusion of "pricerange" in the sql query is way off base.</p> <ul> <li>MySQL Server version: 5.1.65-cll</li> <li>Price column type: decimal(10,2)</li> </ul> <p>Any help would be hugely appreciated. Please check the code blocks below. </p> <p>Thanks a ton!</p> <h2>HTML</h2> <pre><code> &lt;form method="post" action="winesearch.php?go" id="searchform"&gt; &lt;input type="text" size="35" name="user-entry"/&gt; &lt;select name="pricerange" size="1" id="pricerange"&gt; &lt;option value=""&gt;Price Range&amp;nbsp;&lt;/option&gt; &lt;option value="1"&gt;$&amp;nbsp;10 - $20&lt;/option&gt; &lt;option value="2"&gt;$&amp;nbsp;21 - $30&lt;/option&gt; &lt;option value="3"&gt;$&amp;nbsp;31 - $50&lt;/option&gt; &lt;option value="4"&gt;$&amp;nbsp;51 - $75&lt;/option&gt; &lt;option value="5"&gt;$&amp;nbsp;76 - $100&lt;/option&gt; &lt;option value="6"&gt;$101 - $200&lt;/option&gt; &lt;option value="7"&gt;$201 - Plus&lt;/option&gt; &lt;/select&gt; &lt;input type="submit" name="submit" value="Wine Search"/&gt; &lt;/form&gt; </code></pre> <h2>PHP</h2> <pre><code>&lt;?php if(isset($_POST['submit'])){ if(isset($_GET['go'])){ if(preg_match("/^[a-zA-Z0-9]+/", $_POST['user-entry'])){ $cob=$_POST['user-entry']; $pricerange=$_POST['pricerange']; //connect to the database $db=mysql_connect ("server", "user", "pass") or die (mysql_error()); //-select the database to use $mydb=mysql_select_db("db_name"); if($pricerange == 0) $pricerange = 1; switch ($pricerange) { case 1 : $pricerange = " where Price BETWEEN 10.00 AND 20.00 "; break; case 2 : $pricerange = " where Price BETWEEN 21.00 AND 30.00 "; break; case 3 : $pricerange = " where Price BETWEEN 31.00 AND 50.00 "; break; case 4 : $pricerange = " where Price BETWEEN 51.00 AND 75.00 "; break; case 5 : $pricerange = " where Price BETWEEN 76.00 AND 100.00 "; break; case 6 : $pricerange = " where Price BETWEEN 101.00 AND 200.00 "; break; case 7 : $pricerange = " where Price &gt; 200.00 "; break; } //-query the database table $sql=" SELECT ID, CSPC, Country, Producer, Wine, Year, Price FROM winecellar WHERE CSPC LIKE '%" . $cob . "%' OR Country LIKE '%" . $cob ."%' OR Producer LIKE '%" . $cob ."%' OR Wine LIKE '%" . $cob ."%' OR Year LIKE '%" . $cob ."%' OR Price LIKE '%" . $pricerange ."%' "; //-run the query against the mysql query function $result=mysql_query($sql); //-create while loop and loop through result set while($row=mysql_fetch_array($result)){ $CSPC=$row['CSPC']; $Country=$row['Country']; $Producer=$row['Producer']; $Wine=$row['Wine']; $Year=$row['Year']; $Price=$row['Price']; $ID=$row['ID']; //-display the result of the array echo "&lt;ul&gt;\n"; echo "&lt;li&gt;" . $CSPC . "&lt;/li&gt;\n"; echo "&lt;li&gt;" . $Country . "&lt;/li&gt;\n"; echo "&lt;li&gt;" . $Producer . "&lt;/li&gt;\n"; echo "&lt;li&gt;" . $Wine . "&lt;/li&gt;\n"; echo "&lt;li&gt;" . $Year . "&lt;/li&gt;\n"; echo "&lt;li&gt;" . "&lt;a href=" . $Price . "&gt;" . "$" . $Price . "&lt;/a&gt;&lt;/li&gt;\n"; echo "&lt;/ul&gt;"; } } else{ echo "&lt;p&gt;Please enter a search query&lt;/p&gt;"; } } } ?&gt; </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload