|
|
PHP search script example
The script return stock information about a requested item in accordance with the eFind search system specifications.
Version: 0.1 (12.04.2007)
Query parameters:
The search query is transferred in the "search" parameter
Method: GET
Query encoding: Windows-1251
Reply encoding: Windows-1251
Database format:
partname - Item name (obligatory field)
mfg - Manufacturer
note - Description
pdf - Link to the documentation file
img - Link to the image
p1 - Retail price
p2 - Small quantities price
p3 - Large quantities price
stock - Stock availability information (amount of items or shipment date)
Download this search script and the database format it uses.
<?php
$host = ''; # MySQL address, example: mysql.mysite.com $user = ''; # Database user name, example: user_stock $pass = ''; # User password, example: MswZ2Qs $base = ''; # Database name, example: store $table = ''; # Name of the table containing the data, example: stock
# Check if there's a "search" parameter if(isset($_REQUEST['search']) && trim($_REQUEST['search']) != '') { # Database connection if($dbh = mysql_connect($host, $user, $pass)) { # Initialization of current database mysql_select_db($base, $dbh); # Get all items beginning with the searched string $sth = mysql_query( "SELECT * FROM `".$table."` ". "WHERE `partname` LIKE '".addslashes(trim($_REQUEST['search']))."%' ". "LIMIT 0,20", $dbh ); # In case of error, show an appropriate message and exit if(mysql_errno() > 0) { header("HTTP/1.1 500 Internal Server Error"); print "<h1>500 Internal Server Error</h1>". "Query Error"; exit; } ob_start(); print "<data>\n"; if(mysql_num_rows($sth) > 0) { # Loop through all received items while($row = mysql_fetch_array($sth, MYSQL_ASSOC)) { print "<line>\n"; # Item name print " <part>".$row['partname']."</part>\n"; # Manufacturer if($row['mfg'] != '') print " <mfg>".$row['mfg']."</mfg>\n"; # Description if($row['note'] != '') print " <note>".$row['note']."</note>\n";
# Link to the PDF documentation if($row['pdf'] != '') print " <pdf>".$row['pdf']."</pdf>\n";
# Link to the image if($row['img'] != '') print " <img>".$row['img']."</img>\n"; # Currency: USD print " <cur>USD</cur>\n"; # Retail price if($row['p1'] > 0) print " <p1>".$row['p1']."</p1>\n"; # Small quantities price if($row['p2'] > 0) print " <p2>".$row['p2']."</p2>\n"; # Wholesale price if($row['p3'] > 0) print " <p3>".$row['p3']."</p3>\n"; # Stock state print " <stock>".$row['stock']."</stock>\n"; # If the status is not a number (or shipment time, # availability at partner stocks and so on), we distinctly indicate, # that this item is not available in stock if(!preg_match("/^\d+$/", trim($row['stock']))) print " <instock>0</instock>"; print "</line>\n"; } mysql_free_result($sth); } print "</data>\n"; $content = ob_get_contents(); ob_clean(); header("Content-type: application/xml"); print '<?xml version="1.0" encoding="windows-1251" ?>'."\n". $content; mysql_close($dbh); } else # If database connection fails, show an error and exit { header("HTTP/1.1 500 Internal Server Error"); print "<h1>500 Internal Server Error</h1>". "Could not connect to database"; exit; } } else # If "search" parameter doesnt exists, show an error and exit { header("HTTP/1.1 500 Internal Server Error"); print "<h1>500 Internal Server Error</h1>". "Request is not set"; exit; }
?>
|