| | |
search results question
Please support our PHP advertiser: PostgreSQL or MySQL? Compare and contrast the two most popular open source databases
![]() |
•
•
Join Date: Nov 2006
Posts: 35
Reputation:
Solved Threads: 1
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...
how do i approach this task?
php Syntax (Toggle Plain Text)
<!-- search.php --> <HTML> <HEAD><TITLE>Search Registry</TITLE></HEAD> <BODY> <form method="POST" action="results.php"> First Name:<input type="text" name="fname"><BR> Last Name:<input type="text" name="lname"><FONT COLOR="FF0000" SIZE="-1">(required)</FONT><BR> <TABLE><TR><TD> <SELECT NAME="event_day"> <OPTION VALUE="">select a day <OPTION VALUE="01">01 <OPTION VALUE="02">02 <OPTION VALUE="03">03 <OPTION VALUE="04">04 <OPTION VALUE="05">05 <OPTION VALUE="06">06 <OPTION VALUE="07">07 <OPTION VALUE="08">08 <OPTION VALUE="09">09 <OPTION VALUE="10">10 <OPTION VALUE="11">11 <OPTION VALUE="12">12 <OPTION VALUE="13">13 <OPTION VALUE="14">14 <OPTION VALUE="15">15 <OPTION VALUE="16">16 <OPTION VALUE="17">17 <OPTION VALUE="18">18 <OPTION VALUE="19">19 <OPTION VALUE="20">20 <OPTION VALUE="21">21 <OPTION VALUE="22">22 <OPTION VALUE="23">23 <OPTION VALUE="24">24 <OPTION VALUE="25">25 <OPTION VALUE="26">26 <OPTION VALUE="27">27 <OPTION VALUE="28">28 <OPTION VALUE="29">29 <OPTION VALUE="30">30 <OPTION VALUE="31">31 </SELECT> </TD> <TD> <SELECT NAME="event_month"> <OPTION VALUE="">select a month <OPTION VALUE="01">January <OPTION VALUE="02">February <OPTION VALUE="03">March <OPTION VALUE="04">April <OPTION VALUE="05">May <OPTION VALUE="06">June <OPTION VALUE="07">July <OPTION VALUE="08">August <OPTION VALUE="09">September <OPTION VALUE="10">October <OPTION VALUE="11">November <OPTION VALUE="12">December </SELECT> </TD> <TD> <SELECT NAME="event_year"> <OPTION VALUE="">select a year <OPTION VALUE="2002">2002 <OPTION VALUE="2003">2003 <OPTION VALUE="2004">2004 <OPTION VALUE="2005">2005 <OPTION VALUE="2006">2006 <OPTION VALUE="2007">2007 <OPTION VALUE="2008">2008 <OPTION VALUE="2009">2009 <OPTION VALUE="2010">2010 </SELECT> </TD> </TR> </TABLE><BR> <input type="SUBMIT" value="Search"> </form> <?php $fname = $_POST['fname']; $lname = $_POST['lname']; $event_day = $_POST['event_day']; $event_month = $_POST['event_month']; $event_year = $_POST['event_year']; ?> </BODY> </HTML>
php Syntax (Toggle Plain Text)
<!-- RESULTS.PHP --> <?php @ $db = mysql_connect("ya, blah, blah"); if(!$db) { echo "Error: Could not connect to the database. Please try again later."; exit; } trim($lname); if (!$lname) { echo "<FONT COLOR=FF0000>You have not filled the required fields. Please try again.</FONT>"; include "search.inc"; exit; } mysql_select_db("registry_DB, $db); $sql = mysql_query("SELECT brideLname, groomLname FROM my_search_table WHERE brideLname LIKE '%". $lname ."%' OR groomLname LIKE '%". $lname ."%'") or die(mysql_error(); $result = mysql_query($sql); $num_result = mysql_num_rows($result); echo "Number of matches: ". $num_result ."<br />"; if(!$result) { echo "Sorry, there were no matches for your query. Please try again."; } else { echo "<TABLE BORDER=1><TR><TH>Bride</TH><TH>Groom</TH><TH>Event Date</TH><TH> </TH></TR>"; for($i=0; $i < $num_result; $i++) { $row = mysql_fetch_array($result); 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 />"; } echo "</TABLE>"; } mysql_close($db); ?>
how do i approach this task?
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..
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!
Cpanel Email - Let users Register email accounts on your website upon registration
Ajax Chat - Fully browser based chat!
![]() |
Similar Threads
- search and results question. (PHP)
- Search Code Question (MS Access and FileMaker Pro)
- How to hyperlink mysql search results? (MySQL)
- Search Results Not Found (PHP)
- Desktop backgroud has been taken over, google search results being hijacked and more! (Viruses, Spyware and other Nasties)
- Search Results for poker online (Viruses, Spyware and other Nasties)
- Folders open Search Results (Windows NT / 2000 / XP)
Other Threads in the PHP Forum
- Previous Thread: Send a command
- Next Thread: PHP Tableing
| Thread Tools | Search this Thread |
Tag cloud for PHP
.htaccess access advanced ajax apache api array beginner binary broken cakephp check checkbox class cms code cookies cron curl database date datepart directory display download dynamic echo email error file files folder form forms function functions google head href htaccess html image include includingmysecondfileinthechain insert integration ip java javascript joomla limit link login loop mail menu mlm mod_rewrite multiple mysql oop parse password paypal pdf php problem query radio random recursion regex remote script search server sessions smarty sms soap source space sql stored structure syntax system table traffic tutorial update upload url validation validator variable video web xml youtube






