VB icon

DB Items Search

Email
Submitted on: 1/1/2015 1:34:00 AM
By: PHP Code Exchange (from psc cd)  
Level: Intermediate
User Rating: By 4 Users
Compatibility: PHP 3.0, PHP 4.0
Views: 1932
 
     A quick and dirty search script for pulling catalog items from a MySQL database. Supports the use of 'And' or 'Or' or 'Not' between keywords. by Kevin Clevenger.
 
code:
Can't Copy and Paste this?
Click here for a copy-and-paste friendly version of this code!
				
//**************************************
// Name: DB Items Search
// Description:A quick and dirty search script for pulling catalog items from a MySQL database. Supports the use of 'And' or 'Or' or 'Not' between keywords. by Kevin Clevenger.
// By: PHP Code Exchange (from psc cd)
//**************************************

<?
/*
 Kevin Clevenger, 1999-05-23
 This is just a quick and dirty script for pulling catalog items
 from a MySQL database. This script supports the use of 'And' or 'Or' 
 or 'Not' between keywords. 
 Config.inc consists of
 $dbname = "db_name";
 $dbserver= "localhost";
 $dbuser = "uid";
 $dbpass = "password"; 
 If you make improvements to this script please mail a copy to 
 ksc@wanetwork.net
*/
<table border=0 cellpadding=4 align=center width=90%>
<tr><td colspan=2 align=center><font size="6">Search</font></td></tr> 
<? 
 if ($search) {
echo "<tr><td colspan=2 align=center>Search for: $search</td></tr><tr colspan=2><td></td></tr>";
include("config.inc");
$arrSearch = explode(" ", $search);
for ($i=0; $i<count($arrSearch); $i++) {
 if (strToUpper($arrSearch[$i])=='AND' or strToUpper($arrSearch[$i])=='OR' or strToUpper($arrSearch[$i])=='NOT') {
if (strToUpper($arrSearch[$i])=='NOT') {
 $i++;
 $strWhere = $strWhere." and descr not like '%".$arrSearch[$i]."%'";
	} else {
 $strWhere = $strWhere." ".$arrSearch[$i]." ";
	}
 } else {
$strWhere = $strWhere."descr like '%".$arrSearch[$i]."%'";
 }
}
$cn=mysql_connect($dbserver, $dbuser, $dbpass);
mysql_select_db($dbname,$cn);
$sql="SELECT * FROM items WHERE ".$strWhere." ORDER BY name";
$rsCat_query=mysql_query($sql, $cn);
if (!(mysql_errno()==0)) {
 echo "<tr><td colspan=2 align=center><big>There was a problem with the query syntax</big></td></tr>";
 echo "<tr><td colspan=2 align=center><a href=search.php3>Back</a></td></tr></table>";
 exit;
}
if (mysql_num_rows($rsCat_query)==0) {
 echo "<tr><td colspan=2 align=center><big>No items were found matching the criteria</big></td></tr>";
 echo "<tr><td colspan=2 align=center><a href=search.php3>Back</a></td></tr></table>";
 exit;
}
while($rsCat = mysql_fetch_array($rsCat_query)) {
?>
<tr><td>Iterate through your fields here</td></tr>
<tr><td>Field_1 Value:</td><td><? echo $rsCat["field1_name"] ?></td></tr>
<tr><td>Field_2 Value:</td><td><? echo $rsCat["field2_name"] ?></td></tr>
<tr><td>Field_3 Value:</td><td><? echo $rsCat["field3_name"] ?></td></tr>
<? 
 }
} else {
?>
<form action=search.php3 method=post>
<tr><td align=center colspan=2>  <input type=text name=search id=search size=25>
 <input type=submit name=submit value=Submit></td></tr>
<tr><td colspan=2 align=center>Enter the criteria you wish to search for. The search is not case sensitive.</td></tr>
<tr><td colspan=2 align=center>You may use 'And' or 'Or' or 'Not' between keywords.</td></tr>
<tr><td align=right width=100>Example 1:</td><td>this and that</td></tr>
<tr><td align=right>Example 2:</td><td>this not that</td></tr>
<tr><td align=right>Example 3:</td><td>this and that not those</td></tr>
<tr><td align=right>Example 4:</td><td>this and that or those</td></tr>
</form>
<? } ?>
</table>


Other 13 submission(s) by this author

 


Report Bad Submission
Use this form to tell us if this entry should be deleted (i.e contains no code, is a virus, etc.).
This submission should be removed because:

Your Vote

What do you think of this code (in the Intermediate category)?
(The code with your highest vote will win this month's coding contest!)
Excellent  Good  Average  Below Average  Poor (See voting log ...)
 

Other User Comments


 There are no comments on this submission.
 

Add Your Feedback
Your feedback will be posted below and an email sent to the author. Please remember that the author was kind enough to share this with you, so any criticisms must be stated politely, or they will be deleted. (For feedback not related to this particular code, please click here instead.)
 

To post feedback, first please login.