search results question

Reply

Join Date: Nov 2006
Posts: 35
Reputation: boo_lolly is an unknown quantity at this point 
Solved Threads: 1
boo_lolly boo_lolly is offline Offline
Light Poster

search results question

 
0
  #1
Nov 16th, 2006
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...
  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>
  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?
Reply With Quote Quick reply to this message  
Join Date: Sep 2005
Posts: 1,082
Reputation: digital-ether is just really nice digital-ether is just really nice digital-ether is just really nice digital-ether is just really nice 
Solved Threads: 66
Moderator
digital-ether's Avatar
digital-ether digital-ether is offline Offline
Veteran Poster

Re: search results question

 
0
  #2
Nov 19th, 2006
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..
www.fijiwebdesign.com - web design and development and fun
Cpanel Email - Let users Register email accounts on your website upon registration
Ajax Chat - Fully browser based chat!
Reply With Quote Quick reply to this message  
Reply

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



Similar Threads
Other Threads in the PHP Forum
Thread Tools Search this Thread



Tag cloud for PHP
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC