PDA

View Full Version : searching more than one table


sftl99
08-16-2005, 11:03 PM
I can currently search very easily using the code below, but I have to specify which table to search in. So if I search for the name "Bob" I have to also select "First Name" to search by, but what I would like to do is just have the text search through every option, first name, last name, address, etc. Hopefully you'll see how I'm accomplishing my current task...
<?php
$SearchBy = $_POST['SearchBy'];
$searchtext = $_POST['searchtext'];

if( isset($_POST['SearchBy']) ){ $SearchBy = "WHERE $SearchBy"; }
else{ $SearchBy = ""; }
if( isset($_POST['searchtext']) ){ $searchtext = "LIKE '%$searchtext%'"; }
else{ $searchtext = ""; }

$query = "SELECT * FROM $database $SearchBy $searchtext";
?>
<form name="search" method="post" action="index.php">
<input type="text" name="searchtext">
<select name="SearchBy">
<option value="FirstName">First Name</option>
<option value="LastName">Last Name</option>
<option value="Address">Address</option>
</select>
<input type="submit" name="Submit" value="Search">
</form>
<?php
if( isset($_POST['searchtext']) ) {
echo "<p align=\"center\">$num_rows Contact"; if( $num_rows > 1 ){ echo "s"; } else{} echo " Found for &quot;$_POST[searchtext]&quot;</p><div align=\"center\" style=\"font-size:10px\"><a href=\"index.php\">Back</a></div>" ; }
else {echo ""; }
?>

dur0000
08-16-2005, 11:57 PM
Hi sftl99,

First of all, we should get something straight. What you meant for "table" in your question is "field" in a table, I think. Also the "database" word in your code seems to be mixed up with the "table" word. So the hierarchy goes from database > table > field.

When we come to your question, I have a nice piece of code which does exactly what you want
http://www.randomind.com/forum/viewtopic.php?t=4