By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
449,042 Members | 1,036 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 449,042 IT Pros & Developers. It's quick & easy.

Filter / Sort MySQL results!

Yika
P: 1
Hello all! :)

I have a bit of a problem.. kind of!

I have a table with several columns, of course, and most of them are searchable. Basically what I did, was if you entered any text, it would search all the items in the table and return everything. So If I searched for Jessica, I would get Jessica under Staff, possible Jessica under Group, etc etc.

But what I want is to also be able to type, GEP Jessica, and for groups that ONLY contain both keywords to be shown.

Here is my code:

Expand|Select|Wrap|Line Numbers
  1. <?php
  2. include ('config.php');
  3.  
  4. $keywords = $_REQUEST['search'];
  5.  
  6. $words = explode(" ", $keywords);
  7. foreach ($words as $word) {
  8. if (isset($where)) {
  9. $where .= " OR ";
  10. }
  11. $where .= "group_name LIKE '%$word%' OR group_start_mm LIKE '%$word%' OR group_destination LIKE '%$word%' OR group_type LIKE '%$word%' OR group_lead LIKE '%$word%' OR group_status LIKE '%$word%' OR staff_user LIKE '%$word%' OR client_name LIKE '%$word%'";
  12. }
  13.  
  14. $vyear = "06";
  15.  
  16. $result = mysql_query("SELECT * FROM group06
  17. left join clients on group06.client_id = clients.client_id
  18. left join staff on group06.staff_id = staff.staff_id
  19. WHERE $where") 
  20. or die(mysql_error());  
  21.  
  22. $num_rows06 = mysql_num_rows($result); 
  23.  
  24.  
  25. if (!empty($num_rows06)) {
  26.  
  27. while($row = mysql_fetch_array( $result )) {
  28. ?>
  29.  
  30. <div class="view">
  31. <table width="100%">
  32. <tr><th colspan="7" align="center"><b>2006</b></th><td align="center"><b>Results: <?php echo $num_rows06; ?></b></td></tr>
  33.  
  34. <tr bgcolor=#E5F0FF>
  35. <td align="center"><b>File #</b></td>
  36. <td align="center"><b>Client Name</b></td>
  37. <td align="center"><b>Group Name</b></td>
  38. <td align="center"><b>Destination</b></td>
  39. <td align="center"><b>Dates</b></td>
  40. <td align="center"><b>Staff</b></td>
  41. <td align="center"><b>Status</b></td>
  42. <td align="center"><b>Lead</b></td>
  43. </tr>
  44.  
  45. <?php
  46. while ($row = mysql_fetch_array($result))
  47. {
  48. extract($row);
  49. if ($row['group_id'] > 99) {
  50. $file_number = $vyear.$row['group_id'];
  51. } elseif ($row['group_id'] > 9) {
  52. $file_number = $vyear.'0'.$row['group_id'];
  53. } else {
  54. $file_number = $vyear.'00'.$row['group_id'];
  55. }
  56.  
  57. $id=$row['group_id'];
  58.  
  59. $contents_here = "
  60. <td><a href=view_individual.php?database=$vyear&id=$id>".$file_number."</a></td>
  61. <td style=padding-left:3px;padding-right:3px>".$row['client_name']."</td>
  62. <td style=padding-left:3px;padding-right:3px>".$row['group_name']."</td>
  63. <td style=padding-left:3px;padding-right:3px>".$row['group_destination']."</td>
  64. <td style=padding-left:3px;padding-right:3px>".$row['group_start_mm']."</td>
  65. <td style=padding-left:3px;padding-right:3px>".$row['staff_user']."</td>
  66. <td style=padding-left:3px;padding-right:3px>".$row['group_status']."</td>
  67. <td style=padding-left:3px;padding-right:3px>".$row['group_lead']."</td>";
  68.  
  69. if ($i==0)
  70. {
  71. echo '<tr>'.$contents_here.'</tr>';
  72. }
  73.  
  74. else
  75. {
  76. echo '<tr bgcolor="#F0F0F0">'.$contents_here.'</tr>';
  77. }
  78.  
  79.  
  80. $i++; $i=$i%2;
  81. }
  82. ?>
  83.  
  84.  
  85. </table>
  86. </div>
  87.  
  88. <?php
  89. }
  90. ?>
  91.  
So If anyone can help me go in the right direction, MUCH Appreciated!
Nov 29 '06 #1
Share this question for a faster answer!
Share on Google+

Post your reply

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