944,221 Members | Top Members by Rank

Ad:
  • PHP Discussion Thread
  • Unsolved
  • Views: 1385
  • PHP RSS
Nov 16th, 2006
0

search results question

Expand Post »
i'm working on a simple search and results project. i don't have the database setup yet, but i'm working on that as we speak. i'd like for this to work the FIRST time i run it after the database is running. however, i'd like to add something to my search/results project and i need a little direction... i want to add an option on the search.php page to allow users to view ALL the contents of the SQL table. the search only needs to cycle through the contents of ONE table in the database. here's the catch, i've designed my results.php to bring up an error message if the user didn't input any information into one of the search fields. i need this "VIEW ALL" button to over-ride that. or at least have the same effect. here's what i have so far...
php Syntax (Toggle Plain Text)
  1. <!-- search.php -->
  2. <HTML>
  3. <HEAD><TITLE>Search Registry</TITLE></HEAD>
  4.  
  5. <BODY>
  6.  
  7. <form method="POST" action="results.php">
  8.  
  9. First Name:<input type="text" name="fname"><BR>
  10.  
  11. Last Name:<input type="text" name="lname"><FONT COLOR="FF0000" SIZE="-1">(required)</FONT><BR>
  12.  
  13. <TABLE><TR><TD>
  14. <SELECT NAME="event_day">
  15. <OPTION VALUE="">select a day
  16. <OPTION VALUE="01">01
  17. <OPTION VALUE="02">02
  18. <OPTION VALUE="03">03
  19. <OPTION VALUE="04">04
  20. <OPTION VALUE="05">05
  21. <OPTION VALUE="06">06
  22. <OPTION VALUE="07">07
  23. <OPTION VALUE="08">08
  24. <OPTION VALUE="09">09
  25. <OPTION VALUE="10">10
  26. <OPTION VALUE="11">11
  27. <OPTION VALUE="12">12
  28. <OPTION VALUE="13">13
  29. <OPTION VALUE="14">14
  30. <OPTION VALUE="15">15
  31. <OPTION VALUE="16">16
  32. <OPTION VALUE="17">17
  33. <OPTION VALUE="18">18
  34. <OPTION VALUE="19">19
  35. <OPTION VALUE="20">20
  36. <OPTION VALUE="21">21
  37. <OPTION VALUE="22">22
  38. <OPTION VALUE="23">23
  39. <OPTION VALUE="24">24
  40. <OPTION VALUE="25">25
  41. <OPTION VALUE="26">26
  42. <OPTION VALUE="27">27
  43. <OPTION VALUE="28">28
  44. <OPTION VALUE="29">29
  45. <OPTION VALUE="30">30
  46. <OPTION VALUE="31">31
  47. </SELECT>
  48. </TD>
  49. <TD>
  50. <SELECT NAME="event_month">
  51. <OPTION VALUE="">select a month
  52. <OPTION VALUE="01">January
  53. <OPTION VALUE="02">February
  54. <OPTION VALUE="03">March
  55. <OPTION VALUE="04">April
  56. <OPTION VALUE="05">May
  57. <OPTION VALUE="06">June
  58. <OPTION VALUE="07">July
  59. <OPTION VALUE="08">August
  60. <OPTION VALUE="09">September
  61. <OPTION VALUE="10">October
  62. <OPTION VALUE="11">November
  63. <OPTION VALUE="12">December
  64. </SELECT>
  65. </TD>
  66. <TD>
  67. <SELECT NAME="event_year">
  68. <OPTION VALUE="">select a year
  69. <OPTION VALUE="2002">2002
  70. <OPTION VALUE="2003">2003
  71. <OPTION VALUE="2004">2004
  72. <OPTION VALUE="2005">2005
  73. <OPTION VALUE="2006">2006
  74. <OPTION VALUE="2007">2007
  75. <OPTION VALUE="2008">2008
  76. <OPTION VALUE="2009">2009
  77. <OPTION VALUE="2010">2010
  78. </SELECT>
  79. </TD>
  80. </TR>
  81. </TABLE><BR>
  82.  
  83. <input type="SUBMIT" value="Search">
  84. </form>
  85.  
  86. <?php
  87.  
  88. $fname = $_POST['fname'];
  89. $lname = $_POST['lname'];
  90. $event_day = $_POST['event_day'];
  91. $event_month = $_POST['event_month'];
  92. $event_year = $_POST['event_year'];
  93. ?>
  94.  
  95. </BODY>
  96. </HTML>
php Syntax (Toggle Plain Text)
  1. <!-- RESULTS.PHP -->
  2.  
  3. <?php
  4.  
  5. @ $db = mysql_connect("ya, blah, blah");
  6.  
  7. if(!$db)
  8. {
  9. echo "Error: Could not connect to the database. Please try again later.";
  10. exit;
  11. }
  12.  
  13. trim($lname);
  14. if (!$lname)
  15. {
  16. echo "<FONT COLOR=FF0000>You have not filled the required fields. Please try again.</FONT>";
  17. include "search.inc";
  18. exit;
  19. }
  20.  
  21. mysql_select_db("registry_DB, $db);
  22.  
  23. $sql = mysql_query("SELECT brideLname, groomLname FROM my_search_table WHERE brideLname LIKE '%". $lname ."%' OR groomLname LIKE '%". $lname ."%'") or die(mysql_error();
  24. $result = mysql_query($sql);
  25. $num_result = mysql_num_rows($result);
  26.  
  27.  
  28. echo "Number of matches: ". $num_result ."<br />";
  29.  
  30. if(!$result)
  31. {
  32. echo "Sorry, there were no matches for your query. Please try again.";
  33. }
  34. else
  35. {
  36. echo "<TABLE BORDER=1><TR><TH>Bride</TH><TH>Groom</TH><TH>Event Date</TH><TH>&nbsp;</TH></TR>";
  37.  
  38. for($i=0; $i < $num_result; $i++)
  39. {
  40. $row = mysql_fetch_array($result);
  41. echo "<TR><TD>". $row['brideFname'] ." ". $row['brideLname'] ."</TD><TD>". $row['groomFname'] ." ". $row['groomLname'] ."</TD><TD>". $row['event_month'] ."/". $row['event_day'] ."/". $row['event_year'] ."</TD><TD>". $row['uID'] ."</TD></TR><br />";
  42. }
  43.  
  44. echo "</TABLE>";
  45. }
  46. mysql_close($db);
  47. ?>
  48.  

how do i approach this task?
Similar Threads
Reputation Points: 10
Solved Threads: 1
Light Poster
boo_lolly is offline Offline
35 posts
since Nov 2006
Nov 19th, 2006
0

Re: search results question

For example:

If you create a submit form field for the "view all" function. Then say name it "view_all".

<input type="submit" name="view_all" value="View All" />

Then when someone submits the form with the "view all" button instead of the regular "search" button, you'll get an HTTP_VAR with the index "view_all" with the value "View All".

Eg: If it was an HTTP POST then you would get

$_POST['view_all']; // with the value 'View All'

So in your code you can branch the validation block:


[PHP]if (!$lname && !isset($_POST['view_all']))
{
echo "<FONT COLOR=FF0000>You have not filled the required fields. Please try again.</FONT>";
include "search.inc";
exit;
} elseif (isset($_POST['view_all']) {

// select everything (best if you add a limit and pagination)
$sql = mysql_query("SELECT * FROM my_search_table LIMIT [pagination limit]") or die(mysql_error();

} else {

// your regular query
$sql = mysql_query("SELECT brideLname, groomLname FROM my_search_table WHERE brideLname LIKE '%". $lname ."%' OR groomLname LIKE '%". $lname ."%'") or die(mysql_error();

}


[/PHP]

Notice I've put the SQL statements in the validation blocks. Its the only thing that should depend on the validation.

hope that helps a bit..
Moderator
Reputation Points: 457
Solved Threads: 101
Nearly a Posting Virtuoso
digital-ether is offline Offline
1,250 posts
since Sep 2005

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in PHP Forum Timeline: Send a command
Next Thread in PHP Forum Timeline: PHP Tableing





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC