Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

search engines

Status
Not open for further replies.

thumbelina

Programmer
Jun 4, 2001
58
CA
I'm not really sure where to start on this one, but I need to create several search engines, one for each of 2 databases. The problem is that within each database I need to let them search by one of several different fields. For example one database maintains a small library, so they need to search by title, year, author, or subject (i have 5 subject fields) So I need to create one form with each of these different options and from there create a list of results. The database is in MYSQL. Any help would be appreacted, Thanks.

Thumbelina
 
Sure.

Here's a simple way to do one:

[tt]
<?php
// assume $fieldname is a radio button from a form
$sql = &quot;SELECT $fieldname FROM library WHERE $fieldname LIKE '%$query%'&quot;;
// assume $query is the word/words they searched for
$dbresult = mysql_query(&quot;database&quot;,$sql) or die(&quot;failed query&quot;);
$intnumrows = mysql_num_rows($dbresult);
for ($i=0; $i<$intnumrows; $i++) {
$cl = mysql_fetch_object($dbresult);
print $cl->$fieldname.&quot;<br>&quot;;
}
?>
[/tt]

That should work for a rudementary beginning search engine.

Here is one that I wrote about 6 months ago. It is really bad coding, but I think it will work. You can play around with it if you like.

[tt]
<?php

include(&quot;lib.inc.php&quot;); // include the library
if ($query == '') {
common_header($cfgBoardTitle,$cfgBoardStyleSheet);
print &quot;You did not enter a query, go back and enter a query.&quot;;
std_button(&quot;index.php&quot;,&quot;Back&quot;);
common_footer();
} else {

connect();
common_header($cfgBoardTitle,$cfgBoardStyleSheet);
common_header2();
start_table();
mysql_select_db($cfgBoardDatabase);
if ($method == &quot;text&quot;) {
$search = &quot;SELECT * FROM messages WHERE text LIKE '%$query%'&quot;;
} else {
$search = &quot;SELECT * FROM threads WHERE username='$query'&quot;;
}
$result = mysql_query($search);
$found = 0;
?>

<tr>
<td class=title width=6%>Poster</td><td class=title>Post</td></tr>

<?php
for ($i=0; $i<=mysql_num_rows($result)-1; $i++) {
$threadid = mysql_result($result, $i,&quot;threadid&quot;);
$forumid = mysql_result($result, $i,&quot;forumid&quot;);
$title = mysql_result($result, $i,&quot;title&quot;);
$user = mysql_result($result, $i, &quot;username&quot;);
$email = mysql_result($result,$i,&quot;email&quot;);

if ($title == '') {
// do nothing, don't print it
} else {
echo &quot;<tr><td class=search align=center><a href=\&quot;mailto:$email\&quot;>$user</a></td><td class=search><a href=\&quot;viewpost.php?threadid=$threadid&forumid=$forumid\&quot;>$title</a></td></tr>&quot;;
}
$found++;
}

if ($found == 0) {

print &quot;<td class=search align=center colspan=2>Nothing matched your query: <b>$query</b></td></tr>&quot;;
} else {
echo &quot;<tr><td colspan=2 class=searchresults>There &quot;, $found > 1 ? &quot;were&quot; : &quot;was&quot;, &quot; $found &quot;, $found > 1 ? &quot;matches&quot; : &quot;match&quot; ,&quot; for your query: <b>$query</b>.</td></tr>&quot;;
}
end_table();
mysql_free_result($result);

std_button(&quot;index.php&quot;,&quot;Back&quot;);
common_footer();

}
?>

[/tt]

Hope this helps.

-Vic vic cherubini
krs-one@cnunited.com
====
Knows: Perl, HTML, JavScript, C/C++, PHP, Flash
====
 
Hey thumbelina,

Here some stuff that I did a while back. I don't know if you want the search engine to search all the fields you mentioned, but you can take a look here.
Code:
function print_library_list($search) {

if($search != &quot;&quot;){
  $query = &quot;SELECT l.id l.title, l.year, &quot; .    
           &quot;l.author, l.subject, l.subject1&quot; .
           &quot;FROM library l &quot; .
     	   &quot;ORDER BY l.title&quot;;
	
  $s = mysql_query($query);
			
$searchres = &quot;&quot;;
while ($t = mysql_fetch_object($s)) {
 if (stristr($t->title, $search)) { $searchres .= $t->id . &quot;, &quot;; }
 if (stristr($t->year, $search)) { $searchres .= $t->id . &quot;, &quot;; }
 if (stristr($t->author, $search)) { $searchres .= $t->id . &quot;, &quot;; }
 if (stristr($t->subject, $search)) { $searchres .= $t->id . &quot;, &quot;; }

etc... 
} 

//take off the last &quot;,&quot;
$searchres = substr($searchres, 0, strlen($searchres)-2);

/* Now you have a string containing the id's of the records with the information you searched on. So do a SELECT on that string */

if ($searchres == &quot;&quot;) { return &quot; &quot;;} // you got nothing
		
$query = &quot;SELECT l.id, l.name, whatever you want &quot; .
         &quot;FROM library &quot; .
	 &quot;WHERE l.id IN ($searchres) &quot; .
         &quot;OREDER BY l.name&quot;;

$qid = mysql_query($query);

return $qid;

} else {

Do something if they didn't enter anything.... maybe show all the entries. :)
}
And there ya go, $qid will have the records that you want to display now.

-Josh


 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top