help needed with search bar...

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
boo_lolly
Forum Contributor
Posts: 154
Joined: Tue Nov 14, 2006 5:04 pm

help needed with search bar...

Post by boo_lolly »

i've got a search bar that i'm working on. i'm having a little trouble getting it to do what i want... first, i need it to be kind of liberal... not extremely precise... i also have optional search attributes... for instance... i have 3 drop down menus... one for day of the month. another for month. and another for year. if these values are set, add them into the sql query. if not, ignore them. here's what i got....

Code: Select all

//search.php
<form method="POST" action="results.php?action=simplesearch">

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>
<form method="POST" action="results.php?action=view_all">
<input type="SUBMIT" value="View All">
</form>
<?php
	$fname = $_POST['fname'];
	$lname = $_POST['lname'];
	$event_day = $_POST['event_day'];
	$event_month = $_POST['event_month'];
	$event_year = $_POST['event_year'];
?>
don't give me cr@p for not using loops for my drop-downs... haha i do have working loops for them on other pages and i have every intention of changing them in the near future, but for now.. you get the idea =).

and here are the results

Code: Select all

<!-- RESULTS.PHP -->

<?php

	@ $db = mysql_connect("yah", "blah", "blah");
	if(!$db){
		echo "Error: Could not connect to the database. Please try again later.";
		exit;
	}

	mysql_select_db("registry_DB", $db);
	
	if($action == "simplesearch"){
   		$sql = mysql_query("SELECT brideFname, brideLname, groomFname, groomLname, event_month, event_day, event_year, uID FROM my_search_table WHERE brideLname LIKE '%". $lname ."%' OR groomLname LIKE '%". $lname ."%'") or die(mysql_error());
		trim($lname);
		if (!$lname){
			echo "<FONT COLOR=FF0000>You have not filled the required fields. Please try again.</FONT>";
			include "search.inc";
			exit;
		}
	}elseif($action == "view_all"){
		$sql = mysql_query("SELECT * FROM my_search_table") or die(mysql_error());
	}

	$result = $sql;
	$num_result = mysql_num_rows($result);

	if($num_result < 1){
		echo "<FONT COLOR=red>Sorry, there were no matches for your query. Please try again.</FONT>";
		include "search.inc";
		mysql_close($db);
		exit;
	}else{
		echo "Number of matches: ". $num_result ."<br />";
		echo "<TABLE BORDER=1><TR><TH>Bride</TH><TH>Groom</TH><TH>Event Date</TH><TH>Registry</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><A HREF=registry.php?regID=". $row['uID'] .">View</A></TD></TR><br />";
			$_POST['regID'];
		}
		echo "</TABLE>";
	}
	mysql_close($db);
?>
keep in mind these were probably the first php pages i've ever built. or sincerely close to it. anyway, the results are coming back pretty weird. i really don't know how to describe it... i'm pretty sure the problem is with my SQL query.... any help?
User avatar
boo_lolly
Forum Contributor
Posts: 154
Joined: Tue Nov 14, 2006 5:04 pm

Post by boo_lolly »

i think i may have to write all these variables to an array if they're set, and run the array through my SQL query..
User avatar
Ollie Saunders
DevNet Master
Posts: 3179
Joined: Tue May 24, 2005 6:01 pm
Location: UK

Post by Ollie Saunders »

What specifically is causing you difficulty?
Post Reply