Problem with PHP Search Code....

Reply

Join Date: Aug 2007
Posts: 87
Reputation: hemgoyal_1990 is an unknown quantity at this point 
Solved Threads: 7
hemgoyal_1990's Avatar
hemgoyal_1990 hemgoyal_1990 is offline Offline
Junior Poster in Training

Problem with PHP Search Code....

 
0
  #1
Apr 9th, 2008
Hi All,
I Have Some Problem with my PHP Searching Code...
I Want to Create Hyperlink of Multipul Data for Ex. :- a user Found example if this query have maney data this show as hyperlink view....

i am using this code.......

  1. <?php
  2. /**************************************************************************************
  3.  * Main Search Page - search.php
  4.  * Author: Your Name <user@something.com>
  5.  * This file searches the database
  6.  **************************************************************************************/
  7.  
  8. //Get variables from config.php to connect to mysql server
  9. $dbhost='localhost';
  10. $dbusername='user';
  11. $dbuserpass='password';
  12. $dbname = 'dbname';
  13.  
  14. // connect to the mysql database server.
  15. mysql_connect ($dbhost, $dbusername, $dbuserpass);
  16. //select the database
  17. mysql_select_db($dbname) or die('Cannot select database');
  18.  
  19. //search variable = data in search box or url
  20. if(isset($_GET['search']))
  21. {
  22. $search = $_GET['search'];
  23. }
  24.  
  25. //trim whitespace from variable
  26. $search = trim($search);
  27. $search = preg_replace('/\s+/', ' ', $search);
  28.  
  29. //seperate multiple keywords into array space delimited
  30. $keywords = explode(" ", $search);
  31.  
  32. //Clean empty arrays so they don't get every row as result
  33. $keywords = array_diff($keywords, array(""));
  34.  
  35. //Set the MySQL query
  36. if ($search == NULL or $search == '%'){
  37. } else {
  38. for ($i=0; $i<count($keywords); $i++) {
  39. $query = "SELECT * FROM table " .
  40. "WHERE Roll_No LIKE '%".$keywords[$i]."%'".
  41. " OR Name LIKE '%".$keywords[$i]."%'" .
  42. " ORDER BY Roll_No";
  43. }
  44.  
  45. //Store the results in a variable or die if query fails
  46. $result = mysql_query($query) or die(mysql_error());
  47. }
  48. if ($search == NULL or $search == '%'){
  49. } else {
  50. //Count the rows retrived
  51. $count = mysql_num_rows($result);
  52. }
  53.  
  54. echo "<html>";
  55. echo "<head>";
  56. echo "<title>Your Title Here</title>";
  57. echo "<link rel=\"stylesheet\" type=\"text/css\" href=\"style.css\" />";
  58. echo "</head>";
  59. echo "<body onLoad=\"self.focus();document.searchform.search.focus()\">";
  60. echo "<center>";
  61. echo "<br /><form name=\"searchform\" method=\"GET\" action=\"8thResult.php\">";
  62. echo "<font face=\"Times New Roman\"><b>Enter Roll No or Name : </b></font>";
  63. echo "<input type=\"text\" name=\"search\" size=\"20\" TABINDEX=\"1\" />";
  64. echo " <input type=\"submit\" value=\"Search\" />";
  65. echo "</form>";
  66. //If search variable is null do nothing, else print it.
  67. if ($search == NULL) {
  68. } else {
  69. echo "You searched for : <b><FONT COLOR=\"blue\">";
  70. foreach($keywords as $value) {
  71. print "$value ";
  72. }
  73. echo "</font></b>";
  74. }
  75. echo "<p> </p>";
  76. echo "</center>";
  77.  
  78. //If users doesn't enter anything into search box tell them to.
  79. if ($search == NULL){
  80. echo "<center><b><FONT COLOR=\"red\">Please enter a search parameter to continue.</font></b><br /></center>";
  81. } elseif ($search == '%'){
  82. echo "<center><b><FONT COLOR=\"red\">Please enter a search parameter to continue.</font></b><br /></center>";
  83. //If no results are returned print it
  84. } elseif ($count <= 0){
  85. echo "<center><b><FONT COLOR=\"red\">Your query returned no results from the database.</font></b><br /></center>";
  86. //ELSE print the data in a table
  87. } elseif ($count > 1){
  88. echo "<div align=\"center\">";
  89. echo "<table border=\"1\" style=\"border-collapse: collapse\" cellpadding=\"0\" cellspacing=\"0\" bordercolor=\"black\" width=\"450\">";
  90. echo "<tr>";
  91. echo "<td width=\"50%\">";
  92. echo "<p align=\"center\">Name</td>";
  93. echo "<td width=\"50%\">";
  94. echo "<p align=\"center\">Roll No</td>";
  95. echo "</tr>";
  96. echo "<tr>";
  97. echo "<td width=\"50%\" align=\"left\"><font face=\"Times New Roman\" color=\"blue\">&nbsp;".$row['Name']."</font></td>";
  98. echo "<td width=\"50%\" align=\"left\"><font face=\"Times New Roman\" color=\"blue\">&nbsp;".$row['Roll_No']."</font></td>";
  99. echo "</tr>";
  100. echo "</table>";
  101. echo "</div>";
  102. } else {
  103.  
  104. //Colors for alternation of row color on results table
  105. $color1 = "#d5d5d5";
  106. $color2 = "#e5e5e5";
  107. //While there are rows, print it.
  108. while($row = mysql_fetch_array($result))
  109. {
  110. //Row color alternates for each row
  111.  
  112. //table background color = row_color variable
  113. echo "<div align\"center\">";
  114. echo "<table border=\"1\" cellpadding=\"0\" cellspacing=\"0\" style=\"border-collapse: collapse\" bordercolor=\"black\" width=\"450\">";
  115. echo "<tr>";
  116. echo "<td>";
  117. echo "<table border=\"0\" cellpadding=\"0\" cellspacing=\"0\" style=\"border-collapse: collapse\" bordercolor=\"black\" width=\"100%\">";
  118. echo "<tr>";
  119. echo "<td width=\"100%\">";
  120. echo "<p align=\"center\"><font color=\"Red\"><b>DIET, Kuchaman City (Nagaur)</b></font></td>";
  121. echo "</tr>";
  122. echo "<tr>";
  123. echo "<td width=\"100%\">";
  124. echo "<p align=\"center\"><font color=\"Red\"><b>8 Board Result 2008</b></font></td>";
  125. echo "</tr>";
  126. echo "</table>";
  127. echo "</td>";
  128. echo "</tr>";
  129. echo "</table>";
  130. echo "<table border=\"1\" style=\"border-collapse: collapse\" cellpadding=\"0\" cellspacing=\"0\" bordercolor=\"black\" width=\"450\">";
  131. echo "<tr>";
  132. echo "<td width=\"222\" align=\"left\"><font face=\"Times New Roman\"><b>&nbsp;Roll No</b></font></td>";
  133. echo "<td width=\"15\" align=\"center\"><b>:</b></td>";
  134. echo "<td width=\"509\" align=\"left\"><font face=\"Times New Roman\" color=\"blue\">&nbsp;".$row['Roll_No']."</font></td>";
  135. echo "</tr>";
  136. echo "<tr>";
  137. echo "<td width+\"222\" align=\"left\"><font face=\"Times New Roman\"><b>&nbsp;Name</b></font></td>";
  138. echo "<td width=\"15\" align=\"center\"><b>:</b></td>";
  139. echo "<td width=\"509\" align=\"left\"><font face=\"Times New Roman\" color=\"blue\">&nbsp;".$row['Name']."</font></td>";
  140. echo "</tr>";
  141. echo "<tr>";
  142. echo "<td width+\"222\" align=\"left\"><font face=\"Times New Roman\"><b>&nbsp;Father Name</b></font></td>";
  143. echo "<td width=\"15\" align=\"center\"><b>:</b></td>";
  144. echo "<td width=\"509\" align=\"left\"><font face=\"Times New Roman\" color=\"blue\">&nbsp;".$row['F Name']."</font></td>";
  145. echo "</tr>";
  146. echo "<tr>";
  147. echo "<td width+\"222\" align=\"left\"><font face=\"Times New Roman\"><b>&nbsp;Hindi</b></font></td>";
  148. echo "<td width=\"15\" align=\"center\"><b>:</b></td>";
  149. echo "<td width=\"509\" align=\"left\"><font face=\"Times New Roman\" color=\"blue\">&nbsp;".$row['Hindi']."</font></td>";
  150. echo "</tr>";
  151. echo "<tr>";
  152. echo "<td width+\"222\" align=\"left\"><font face=\"Times New Roman\"><b>&nbsp;English</b></font></td>";
  153. echo "<td width=\"15\" align=\"center\"><b>:</b></td>";
  154. echo "<td width=\"509\" align=\"left\"><font face=\"Times New Roman\" color=\"blue\">&nbsp;".$row['English']."</font></td>";
  155. echo "</tr>";
  156. echo "<tr>";
  157. echo "<td width+\"222\" align=\"left\"><font face=\"Times New Roman\"><b>&nbsp;Science</b></font></td>";
  158. echo "<td width=\"15\" align=\"center\"><b>:</b></td>";
  159. echo "<td width=\"509\" align=\"left\"><font face=\"Times New Roman\" color=\"blue\">&nbsp;".$row['Science']."</font></td>";
  160. echo "</tr>";
  161. echo "<tr>";
  162. echo "<td width+\"222\" align=\"left\"><font face=\"Times New Roman\"><b>&nbsp;Maths</b></font></td>";
  163. echo "<td width=\"15\" align=\"center\"><b>:</b></td>";
  164. echo "<td width=\"509\" align=\"left\"><font face=\"Times New Roman\" color=\"blue\">&nbsp;".$row['Maths']."</font></td>";
  165. echo "</tr>";
  166. echo "<tr>";
  167. echo "<td width+\"222\" align=\"left\"><font face=\"Times New Roman\"><b>&nbsp;Social Science</b></font></td>";
  168. echo "<td width=\"15\" align=\"center\"><b>:</b></td>";
  169. echo "<td width=\"509\" align=\"left\"><font face=\"Times New Roman\" color=\"blue\">&nbsp;".$row['Social Science']."</font></td>";
  170. echo "</tr>";
  171. echo "<tr>";
  172. echo "<td width+\"222\" align=\"left\"><font face=\"Times New Roman\"><b>&nbsp;Sanskrit</b></font></td>";
  173. echo "<td width=\"15\" align=\"center\"><b>:</b></td>";
  174. echo "<td width=\"509\" align=\"left\"><font face=\"Times New Roman\" color=\"blue\">&nbsp;".$row['Sanskrit']."</font></td>";
  175. echo "</tr>";
  176. echo "<tr>";
  177. echo "<td width+\"222\" align=\"left\"><font face=\"Times New Roman\"><b>&nbsp;Total</b></font></td>";
  178. echo "<td width=\"15\" align=\"center\"><b>:</b></td>";
  179. echo "<td width=\"509\" align=\"left\"><font face=\"Times New Roman\" color=\"blue\">&nbsp;".$row['Total']."</font></td>";
  180. echo "</tr>";
  181. echo "<tr>";
  182. echo "<td width+\"222\" align=\"left\"><font face=\"Times New Roman\"><b>&nbsp;Percentage</b></font></td>";
  183. echo "<td width=\"15\" align=\"center\"><b>:</b></td>";
  184. echo "<td width=\"509\" align=\"left\"><font face=\"Times New Roman\" color=\"blue\">&nbsp;".$row['Per']."</font></td>";
  185. echo "</tr>";
  186. echo "</table>";
  187. echo "</div>";
  188. $row_count++;
  189. //end while
  190. }
  191. //end if
  192. }
  193.  
  194. echo "</body>";
  195. echo "</html>";
  196. if ($search == NULL or $search == '%') {
  197. } else {
  198. //clear memory
  199. mysql_free_result($result);
  200. }
  201. ?>
you can find this query on [link]www.kuchamancity.com/8thResult.php[/link]
plz enter 10000 to display result but when i enter Hemant it does not display any result.. because hemant query having 2 Data.......
plz help me........
Reply With Quote Quick reply to this message  
Join Date: Apr 2008
Posts: 45
Reputation: Daedal is an unknown quantity at this point 
Solved Threads: 11
Daedal's Avatar
Daedal Daedal is offline Offline
Light Poster

Re: Problem with PHP Search Code....

 
0
  #2
Apr 9th, 2008
Originally Posted by hemgoyal_1990 View Post
Hi All,
I Have Some Problem with my PHP Searching Code...
I Want to Create Hyperlink of Multipul Data for Ex. :- a user Found example if this query have maney data this show as hyperlink view....

i am using this code.......

  1. //Set the MySQL query
  2. if ($search == NULL or $search == '%'){
  3. } else {
  4. for ($i=0; $i<count($keywords); $i++) {
  5. $query = "SELECT * FROM table " .
  6. "WHERE Roll_No LIKE '%".$keywords[$i]."%'".
  7. " OR Name LIKE '%".$keywords[$i]."%'" .
  8. " ORDER BY Roll_No";
  9. }
you can find this query on [link]www.kuchamancity.com/8thResult.php[/link]
plz enter 10000 to display result but when i enter Hemant it does not display any result.. because hemant query having 2 Data.......
plz help me........
Here is one solution:

[code]
I Have Some Problem with my PHP Searching Code...
I Want to Create Hyperlink of Multipul Data for Ex. :- a user Found example if this query have maney data this show as hyperlink view....

i am using this code.......

  1. //Set the MySQL query
  2. if ($search == NULL or $search == '%'){
  3. } else {
  4. $where_addin = ''; //this will be the added WHERE statement
  5.  
  6. for ($i=0; $i<count($keywords); $i++) {
  7. if ($i == 0) { //no OR if its the first variable
  8. $where_addin .= "WHERE Roll_No LIKE '%".$keywords[$i]."%'"." OR Name LIKE '%".$keywords[$i]."%' ";
  9. } else {
  10. $where_addin .= "OR Roll_No LIKE '%".$keywords[$i]."%'"." OR Name LIKE '%".$keywords[$i]."%' ";
  11. }
  12.  
  13. $query = "SELECT * FROM table
  14. $where_addin
  15. ORDER BY Roll_No";
Reply With Quote Quick reply to this message  
Join Date: Aug 2007
Posts: 87
Reputation: hemgoyal_1990 is an unknown quantity at this point 
Solved Threads: 7
hemgoyal_1990's Avatar
hemgoyal_1990 hemgoyal_1990 is offline Offline
Junior Poster in Training

Re: Problem with PHP Search Code....

 
0
  #3
Apr 10th, 2008
Thanx frnd for Helping me but This Code does not work.... It;s Does Not Show Maney Data in a Hyperlink View.... Plz Give me Approx Code.
Reply With Quote Quick reply to this message  
Join Date: Aug 2007
Posts: 87
Reputation: hemgoyal_1990 is an unknown quantity at this point 
Solved Threads: 7
hemgoyal_1990's Avatar
hemgoyal_1990 hemgoyal_1990 is offline Offline
Junior Poster in Training

Re: Problem with PHP Search Code....

 
0
  #4
Apr 11th, 2008
Sorry Frnd But Your Given Code Does Not Eork Plz Help me.....
Someone Plz Help Me...
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Other Threads in the PHP Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC