Jump to content

my search page


Hooch

Recommended Posts

Holy cow..am I moving along here!! I really appreciate the help you guys have given me.I did a google on site searches. I found one and have it very close to what I need. It will search one row of a database. I know how to make it show results fromas many rows I want, but I can't figure out how to make it search multiple rows. Here's my code...

<link rel="stylesheet" type="text/css" href="includes/tia.css" /><?php// Connect to DBinclude "includes/z_db.php";if(isset($_GET['search'])){$search = $_GET['search'];}$keywords = explode(" ", $search);$query = "SELECT id,username,email,biography, tours FROM users " ."WHERE biography LIKE '%".$keywords['0']."%'";for ($i=1; $i<count($keywords); $i++) {$query = $query." AND biography LIKE '%".$keywords[$i]."%'";}$result = mysql_query($query) or die(mysql_error());?><center><a href="index.php">Back</a><table width="50%" style="border:1px solid #000000;"><?phpwhile($row = mysql_fetch_array($result)){echo "<tr>";echo "<td style='border-bottom: 1px solid #000000;'>";echo "<b><span class=\"red-medium-16\">".$row['username']."</span></b><br>";echo "<b><span class=\"black-reg-10\">Tours:</b> ".$row['tours']."<br>";echo "<b>Biography:</b> ".$row['biography']."<br>";echo "</span></td>";echo "</tr>";}?></td></tr></table></center>

I assume this is what needs to be tweaked. Right now it's searching the Biography row.

for ($i=1; $i<count($keywords); $i++) {$query = $query." AND biography LIKE '%".$keywords[$i]."%'";}

Many many thanks..Hooch

Link to comment
Share on other sites

I think you're confusing your terminology, biology is a 'field', not a row. A row is a single set of all fields. If you have a table called users, some fields might be id, password, name, etc. A row of the table would be one record, so it would be something like:'user123', 'cleverpassword', 'Test User' etcAnyway, it looks like you need to build a SQL search query. That's always a fun time. You will probably need to build a query using plenty of ANDs and ORs. The way you have it now, you have the keywords separated by "AND", so that means that all keywords need to appear in the field in order for the row to be included in the search. If you have "OR" instead, that would indicate that you can have any keyword and still have the search succeed. Or, you can go one step further and have radio buttons where the user could pick "all keywords" or "any keyword", and then you switch between AND or OR.Anyway, if you want to search in more than one field (say, 'biography' and 'tours'), it would look something like this:

$query = "SELECT id,username,email,biography, tours FROM users " ."WHERE (biography LIKE '%".$keywords['0']."%' OR tours LIKE '%".$keywords['0']."%')";for ($i=1; $i<count($keywords); $i++) {$query = $query." AND (biography LIKE '%".$keywords[$i]."%' OR tours LIKE '%".$keywords[$i]."%')";}

If you want to do the any/all thing, you could add something like this:

<input type="radio" name="any" value="any">Any word  <input type="radio" name="any" value="all">All words...$any = $_POST['any'];if ($any == "any")  $bool = "OR";else  $bool = "AND";$query = "SELECT id,username,email,biography, tours FROM users " ."WHERE (biography LIKE '%".$keywords['0']."%' OR tours LIKE '%".$keywords['0']."%')";for ($i=1; $i<count($keywords); $i++) {$query = $query." {$bool} (biography LIKE '%".$keywords[$i]."%' OR tours LIKE '%".$keywords[$i]."%')";}

Link to comment
Share on other sites

awsome! Sorry for my confusion on the terminology. Now I have about the search..I have about 5 fields to search. Would this

"WHERE (biography LIKE '%".$keywords['0']."%' OR tours LIKE '%".$keywords['0']."%')";

change to this

"WHERE (biography LIKE '%".$keywords['0']."%' OR studio LIKE '%".$keywords['0']."%' OR medium LIKE '%".$keywords['0']."%'OR tours LIKE '%".$keywords['0']."%')";

Link to comment
Share on other sites

hmm..I must be missing something.

$query = "SELECT id,username,studio,tours,medium,biography FROM users " ."WHERE (username LIKE '%".$keywords['0']."%' studio LIKE '%".$keywords['0']."%' OR tours LIKE '%".$keywords['0']."%' OR medium LIKE '%".$keywords['0']."%'OR biography LIKE '%".$keywords['0']."%')";for ($i=1; $i<count($keywords); $i++) {$query = $query." AND (username LIKE '%".$keywords[$i]."%' studio LIKE '%".$keywords[$i]."%' OR tours LIKE '%".$keywords[$i]."%' OR medium LIKE '%".$keywords[$i]."%' OR biography LIKE '%".$keywords[$i]."%')";}

I just saw I missed an OR on each section...ooopssee

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...