Page 1 of 1

search results question

Posted: Thu Nov 16, 2006 10:08 am
by boo_lolly
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...

Code: Select all

<!-- 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>

Code: Select all

<!-- 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>&nbsp;</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?

Search

Posted: Thu Nov 16, 2006 10:30 am
by timclaason
If I'm understanding you correctly, you want an option or button to view all records from a table.

I usually use a query string for stuff like this (assuming your php.ini allows this, you shouldn't have a problem).

I'd probably change your <FORM> line to be something like this:

Code: Select all

<form method="POST" action="results.php?action=simplesearch">
Then on PHP post, do something like:

Code: Select all

if($action == "simplesearch")
    $sql = mysql_query("SELECT brideLname, groomLname FROM my_search_table WHERE brideLname LIKE '%". $lname ."%' OR groomLname LIKE '%". $lname ."%'") or die(mysql_error(); 
elseif($action == "fullsearch")
    $sql = mysql_query("SELECT brideLname, groomLname FROM my_search_table") or die(mysql_error();
Then on the "Search All" link or button or whatever, have the link be results.php?action=fullsearch

Hope this helps

Posted: Thu Nov 16, 2006 11:11 am
by boo_lolly
are you sure this is gonna work? for some reason i think i may have to use the $_GET[] array here. anyway, i'll test it out as soon as i get my database running, which i am working on right now. get back to ya soon.

Search Results

Posted: Thu Nov 16, 2006 11:29 am
by timclaason
It depends on how you're php.ini is setup, and what version of PHP you're using.

In newer versions of PHP, I believe GET[] variables by default are cast as variables in themselves (ie $_GET['action'] = $action).

Posted: Thu Nov 16, 2006 11:30 am
by boo_lolly
the tables don't exist yet, but the database does... i get the following error. it seems to be combining my database name AND my table name....

Table 'registry_DB.my_search_table' doesn't exist

my database name is 'registry_DB'. my table name is 'my_search_table'. what's the deal here?

Re: Search Results

Posted: Thu Nov 16, 2006 11:35 am
by Burrito
timclaason wrote:In newer versions of PHP, I believe GET[] variables by default are cast as variables in themselves (ie $_GET['action'] = $action).
this is only the case when register globals is turned on and in 'newer' versions of php it is OFF by default (which it should be :) )

Posted: Thu Nov 16, 2006 11:40 am
by RobertGonzalez
And for the record, register_globals should NEVER be turned on.

Posted: Thu Nov 16, 2006 11:42 am
by boo_lolly
righteous. thanks for the head's up. but that still doesn't help me with my table issue.

Posted: Thu Nov 16, 2006 11:51 am
by RobertGonzalez
Use a checkbox, check if it is checked (this would mean show all). Default your SQL to be SELECT * FROM table, then if the checkbox is not checked, AND the user did not leave empty fields, append the SQL to include the WHERE portion. Voila!