473,668 Members | 2,508 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Sorting Results Problem

1 New Member
I have 3 drop down menus that are used in a search to locate restaurants in a db. All of the drop down menus function, a search can be submitted with any combination of drop downs and the results are sorted by restaurant name. The problem occurs when you do not select anything from a drop down ("All" is submitted for all drop downs) and submit the search, the following error occurs: "Can't connect because You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'BY `restaurant Name` ASC' at line 1." The error shows up after I add in "$restaurant_re sults = conn($find_rest aurants."ORDER BY `restaurant Name` ASC"); " line 93. Without this line, the reults are returned by index number and will not be sorted by restaurant name. I need to have all the results sorted by restaurant name. I have included the code below. Any help would be greatly appreciated.

Expand|Select|Wrap|Line Numbers
  1. <?php 
  2. /*------------------------------------------------------------------------ 
  3.             control codes 
  4. ------------------------------------------------------------------------*/ 
  5.  
  6. if (isset($_POST['submit'])) 
  7.  
  8.   search();       //call the search function 
  9.  
  10. }else{ 
  11.  
  12.   show_form();   //call the show form function 
  13.  
  14. }//end if 
  15.  
  16. /*------------------------------------------------------------------------ 
  17.             show the search form 
  18. ------------------------------------------------------------------------*/ 
  19.  
  20. function show_form() 
  21.   //call the dropdown function which creates an html string to build a select box for each element 
  22.   $neighborhood_dropdown   = dropdown('neighborhood','restaurant'); 
  23.   $cuisine_dropdown  = dropdown('cuisine1','restaurant'); 
  24.   $type_dropdown     = dropdown('type','restaurant');
  25.  
  26.  
  27.  
  28.  
  29.  
  30.  
  31.   echo "<form name='search' action='".$_SERVER['PHP_SELF']."' method='post'> 
  32.         <table width='50%' align='center' valign='center'> 
  33.         <tr> 
  34.           <td colspan='2' align='center'>Search Form</td> 
  35.         </tr> 
  36.         <tr> 
  37.           <td align='right'>Neighborhood:</td><td>$neighborhood_dropdown</td> 
  38.         </tr> 
  39.         <tr> 
  40.           <td align='right'>Cuisine:</td><td>$cuisine_dropdown</td> 
  41.         </tr> 
  42.         <tr> 
  43.           <td align='right'>Type:</td><td>$type_dropdown</td> 
  44.         </tr> 
  45.         <tr> 
  46.           <td colspan='2' align='center'> </td> 
  47.         </tr> 
  48.         <tr> 
  49.           <td colspan='2' align='center'><input type='submit' name='submit' value='Go!'></td> 
  50.         </tr> 
  51.         </table> 
  52.         </form>"; 
  53.  
  54. }//end function 
  55.  
  56.  
  57. /*------------------------------------------------------------------------ 
  58.             run the search and show the results 
  59. ------------------------------------------------------------------------*/ 
  60.  
  61. function search() 
  62.  
  63. //base sql 
  64. $find_restaurants = "select * FROM `restaurant` WHERE 1";
  65.  
  66. //get the values from the form 
  67. //NOTE: You should do way more valdation on the values before you attempt to process anything 
  68.  
  69.  
  70.   if ((!empty($_POST['neighborhood']))&&($_POST['neighborhood'] != 'all')) 
  71.   { 
  72. $find_restaurants .= " and neighborhood like '". addslashes($_POST['neighborhood']). "%'"; 
  73.   }
  74.  
  75.   if ((!empty($_POST['cuisine1']))&&($_POST['cuisine1'] != 'all')) 
  76.   { 
  77. $find_restaurants .= " and cuisine1 like '". addslashes($_POST['cuisine1'])."%'"; 
  78.   } 
  79.  
  80.   if ((!empty($_POST['type']))&&($_POST['type'] != 'all')) 
  81.   { 
  82. $find_restaurants .= " and type like '". addslashes($_POST['type'])."%' "; 
  83.   }
  84.  
  85.   //add more elements (or take away) as you desire...follow the same code structure as above 
  86.  
  87.  
  88.   //run query
  89.  
  90.     $restaurant_results = conn($find_restaurants."ORDER BY `restaurant Name` ASC"); 
  91.  
  92.   if (!$restaurant_results){ die("No results due to database error.<br>".mysql_error());  } 
  93.  
  94.   if (mysql_num_rows($restaurant_results)==0) 
  95.   { 
  96.     echo "No Results found!"; 
  97.   }else{ 
  98.  
  99.     echo "<table border='1'><th>Restaurant Name</th><th>neighborhood</th><th>cuisine</th><th>type</th></tr>"; 
  100.  
  101.     while ($rows= mysql_fetch_array($restaurant_results)) 
  102.     { 
  103.       echo "<tr>"; 
  104.       echo "<td>". $rows['restaurant Name'] ."</td>";
  105.       echo "<td>". $rows['neighborhood']  ."</td>"; 
  106.       echo "<td>". $rows['cuisine1'] ."</td>"; 
  107.       echo "<td>". $rows['type']    ."</td>";
  108.       echo "</tr>"; 
  109.  
  110.     }  
  111.     echo "</table>"; 
  112.   }//end if 
  113.  
  114. ?>
  115. <p align="center"><a href="test3.php">refine search</a></p>
  116. <?
  117.  
  118. }//end function 
  119.  
  120. /*------------------------------------------------------------------------ 
  121.             create the drop downs 
  122. ------------------------------------------------------------------------*/ 
  123.  
  124. function dropdown($field, $table) 
  125. {  
  126.   //initialize variables 
  127.   $oHTML  = ''; 
  128.   $get_dropdown_values = ''; 
  129.  
  130.   //check to see if the field is passed correctly 
  131.   if (($field == "")||($table == "")) 
  132.   { 
  133.     die("No column or table specified to create drop down from!"); 
  134.   } 
  135.  
  136.   $get_dropdown_values = "select distinct($field) from $table ORDER BY $field"; 
  137.  
  138.   //call the db function and run the query 
  139.   $result_drop_down = conn($get_dropdown_values); 
  140.  
  141.   //if no results are found to create a drop down return a textbox 
  142.   if ((!$result_drop_down) ||(mysql_num_rows($result_drop_down)==0)) 
  143.   { 
  144.     $oHTML .= "<input type='text' name='$field' value='' size='15'>"; 
  145.   }elseif (($result_drop_down)&&(mysql_num_rows($result_drop_down)>0)){ 
  146.  
  147.     //build the select box out of the results 
  148.     $oHTML .= "<select name='$field'>\n<option value='all'>All</option>\n"; 
  149.     while ($rows = mysql_fetch_array($result_drop_down)) 
  150.     { 
  151.       $oHTML .= "<option value='".$rows[$field]."'>".$rows[$field]."</option>\n"; 
  152.     } 
  153.     $oHTML .= "</select>\n"; 
  154.   } 
  155.  
  156.   //send the value back to the calling code 
  157.   return $oHTML; 
  158. }//end function 
  159.  
  160. /*------------------------------------------------------------------------ 
  161.             database connection function 
  162. ------------------------------------------------------------------------*/ 
  163.  
  164. function conn($database) 
  165. {    
  166. include 'config.php';
  167. include 'opendb.php';
  168.  
  169.     $result = mysql_query($database) or die ("Can't connect because ". mysql_error()); 
  170.  
  171.     return $result; 
  172.  
  173. }//end function      
  174.  
  175. ?>
  176.  
Dec 24 '07 #1
1 1623
helimeef
77 New Member
I have 3 drop down menus that are used in a search to locate restaurants in a db. All of the drop down menus function, a search can be submitted with any combination of drop downs and the results are sorted by restaurant name. The problem occurs when you do not select anything from a drop down ("All" is submitted for all drop downs) and subm...
Change line 93 to
Expand|Select|Wrap|Line Numbers
  1. $restaurant_results = die($find_restaurants."ORDER BY `restaurant Name` ASC");
  2.  
And tell me what it says on the page. FYI die() is a PHP function that ends the script and displays a message, so this will let me see what is getting queried.
I'm almost 100% sure that it not valid mysql to have a 2 word name for a column (or field as some peeps say), and maybe the random "1" placed at the end of the query on line 67.
Dec 26 '07 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

2
2734
by: Henry Wiz | last post by:
Hi all, I wanna ask how to do the following sorting function, for example Name Age Brian 22 Alice 17 Patrick 21
11
16371
by: Nikki | last post by:
Is it possible to sort a dataset rather than a dataview? I have a web service that returns a dataset which I would like to sort before returning it (this is so the sorting is standardised and so applications that see the results as xml don't have to fiddle around and sort it themselves later). I have tried sorting a dataview and adding that dataview's table to the dataset but the results don't remain sorted. The only way I can see to do...
7
3036
by: Karin Jensen | last post by:
Hi I am running a PHP program that connects to an Access 2000 database via ODBC: $results = odbc_exec($connection_id, $sql_select); Is it possible to sort the contents of $results? I wish to use a regex-based sort order that you can't do in Access 2000* SQL with "ORDER BY".
8
3509
by: Mike MacSween | last post by:
tblCourses one to many to tblEvents. A course may have an intro workshop (a type of event), a mid course workshop, a final exam. Or any combination. Or something different in the future. At the moment the printed output is usually going to Word. It's turning into an unholy mess, because I'm having to prepare umpteen different Word templates, and the queries that drive them, depending on what events a course has.
1
2637
by: aredo3604gif | last post by:
On Sun, 10 Apr 2005 19:46:32 GMT, aredo3604gif@yahoo.com wrote: >The user can dynamically enter and change the rule connection between >objects. The rule is a "<" and so given two objects: >a < b simply means that b < a can't be set, also it must be a != b. >And with three objects a < b , b < c means a < c > >I studied Quick Union Find algorithms a bit and if I understood them >correctly, once the user gives the input setting the...
7
8718
by: Brett Romero | last post by:
I have a dataset with one table, which has four columns. All are of type INT. I need to convert this dataset into a dataview so I can sort on the last three columns. I may sort one of the three or all three at once. This dataview will display its results in a winform datagrid. Right now, I create the dataset from a datareader: while(dr.Read()) { drow = _filteredCriteriaDataSet.Tables.NewRow();
1
1765
by: LoopyNZ | last post by:
Hi there, I have an IIF expression that is returning a 0 (exact match), 1 (partial match), or 2 (no match) based on comparing text fields (see below). However, when I try to sort the results (either using an ORDER BY _or_ using the A-Z toolbar button on the datasheet), it doesn't work. It sorts the 0s to the top, but the 1s and 2s are mixed up - the records where one of the text fields is NULL show as 2s, but sort as 1s!
4
1701
by: Trent | last post by:
Still have problems with this thing. Seems my results are not matching the "correct" example given. The three sets of numbers below the last 3 columns is suppose to be the number of comparisons made by eahc sorting function. I have having trouble getting the number of comparisons to match. One comment was that "the counts are in the wrongs place," but I have moved them all over the function and still they do not match.
1
7183
KevinADC
by: KevinADC | last post by:
Introduction In part one we discussed the default sort function. In part two we will discuss more advanced techniques you can use to sort data. Some of the techniques might introduce unfamiliar methods or syntax to a less experienced perl coder. I will post links to online resources you can read if necessary. Experienced perl coders might find nothing new or useful contained in this article. Short Review In part one I showed you some...
1
1951
by: John A Grandy | last post by:
In regard to a GridView that must support searching, filtering, sorting, and paging ... There is a tradeoff in performing the sorting and paging in the database versus to creating a CLR sort expression and allowing the GridView to perform the sorting and paging. How do people feel about each option for (1) simple sorts (alphabetical, compound alphabetical)
0
8893
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8656
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
6209
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5681
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4205
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4380
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2791
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2023
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1786
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.