Expand|Select|Wrap|Line Numbers
- <?php
- /*------------------------------------------------------------------------
- control codes
- ------------------------------------------------------------------------*/
- if (isset($_POST['submit']))
- {
- search(); //call the search function
- }else{
- show_form(); //call the show form function
- }//end if
- /*------------------------------------------------------------------------
- show the search form
- ------------------------------------------------------------------------*/
- function show_form()
- {
- //call the dropdown function which creates an html string to build a select box for each element
- $neighborhood_dropdown = dropdown('neighborhood','restaurant');
- $cuisine_dropdown = dropdown('cuisine1','restaurant');
- $type_dropdown = dropdown('type','restaurant');
- echo "<form name='search' action='".$_SERVER['PHP_SELF']."' method='post'>
- <table width='50%' align='center' valign='center'>
- <tr>
- <td colspan='2' align='center'>Search Form</td>
- </tr>
- <tr>
- <td align='right'>Neighborhood:</td><td>$neighborhood_dropdown</td>
- </tr>
- <tr>
- <td align='right'>Cuisine:</td><td>$cuisine_dropdown</td>
- </tr>
- <tr>
- <td align='right'>Type:</td><td>$type_dropdown</td>
- </tr>
- <tr>
- <td colspan='2' align='center'> </td>
- </tr>
- <tr>
- <td colspan='2' align='center'><input type='submit' name='submit' value='Go!'></td>
- </tr>
- </table>
- </form>";
- }//end function
- /*------------------------------------------------------------------------
- run the search and show the results
- ------------------------------------------------------------------------*/
- function search()
- {
- //base sql
- $find_restaurants = "select * FROM `restaurant` WHERE 1";
- //get the values from the form
- //NOTE: You should do way more valdation on the values before you attempt to process anything
- if ((!empty($_POST['neighborhood']))&&($_POST['neighborhood'] != 'all'))
- {
- $find_restaurants .= " and neighborhood like '". addslashes($_POST['neighborhood']). "%'";
- }
- if ((!empty($_POST['cuisine1']))&&($_POST['cuisine1'] != 'all'))
- {
- $find_restaurants .= " and cuisine1 like '". addslashes($_POST['cuisine1'])."%'";
- }
- if ((!empty($_POST['type']))&&($_POST['type'] != 'all'))
- {
- $find_restaurants .= " and type like '". addslashes($_POST['type'])."%' ";
- }
- //add more elements (or take away) as you desire...follow the same code structure as above
- //run query
- $restaurant_results = conn($find_restaurants."ORDER BY `restaurant Name` ASC");
- if (!$restaurant_results){ die("No results due to database error.<br>".mysql_error()); }
- if (mysql_num_rows($restaurant_results)==0)
- {
- echo "No Results found!";
- }else{
- echo "<table border='1'><th>Restaurant Name</th><th>neighborhood</th><th>cuisine</th><th>type</th></tr>";
- while ($rows= mysql_fetch_array($restaurant_results))
- {
- echo "<tr>";
- echo "<td>". $rows['restaurant Name'] ."</td>";
- echo "<td>". $rows['neighborhood'] ."</td>";
- echo "<td>". $rows['cuisine1'] ."</td>";
- echo "<td>". $rows['type'] ."</td>";
- echo "</tr>";
- }
- echo "</table>";
- }//end if
- ?>
- <p align="center"><a href="test3.php">refine search</a></p>
- <?
- }//end function
- /*------------------------------------------------------------------------
- create the drop downs
- ------------------------------------------------------------------------*/
- function dropdown($field, $table)
- {
- //initialize variables
- $oHTML = '';
- $get_dropdown_values = '';
- //check to see if the field is passed correctly
- if (($field == "")||($table == ""))
- {
- die("No column or table specified to create drop down from!");
- }
- $get_dropdown_values = "select distinct($field) from $table ORDER BY $field";
- //call the db function and run the query
- $result_drop_down = conn($get_dropdown_values);
- //if no results are found to create a drop down return a textbox
- if ((!$result_drop_down) ||(mysql_num_rows($result_drop_down)==0))
- {
- $oHTML .= "<input type='text' name='$field' value='' size='15'>";
- }elseif (($result_drop_down)&&(mysql_num_rows($result_drop_down)>0)){
- //build the select box out of the results
- $oHTML .= "<select name='$field'>\n<option value='all'>All</option>\n";
- while ($rows = mysql_fetch_array($result_drop_down))
- {
- $oHTML .= "<option value='".$rows[$field]."'>".$rows[$field]."</option>\n";
- }
- $oHTML .= "</select>\n";
- }
- //send the value back to the calling code
- return $oHTML;
- }//end function
- /*------------------------------------------------------------------------
- database connection function
- ------------------------------------------------------------------------*/
- function conn($database)
- {
- include 'config.php';
- include 'opendb.php';
- $result = mysql_query($database) or die ("Can't connect because ". mysql_error());
- return $result;
- }//end function
- ?>