Hello all, I'm having some issues with a script I've been working on these past few days. First, the script works as needed, there are a few things Im not sure how to do yet (newbie) and if you guys could help, I would appreciate that very much.
- The script displays listings in a MySQL database
- What I can't seem to figure out is how get my limit of 10 results per page, and have those pages reachable via linked numbers to each page.
- And alas, sometimes when a user wants to search for more than one term, it will come back with duplicate results. I tried to fix that, but no luck.
HELP!
<?php
// Search Script 04.2006
$hostname_logon = "localhost" ;
$database_logon = "***" ;
$username_logon = "******" ;
$password_logon = "******" ;
// Open Database Connection
$connections = mysql_connect($hostname_logon, $username_logon, $password_logon) or die ( "Unabale to connect to the database" );
// Select Database
mysql_select_db($database_logon) or die ( "Unable to select database!" );
// How Many Results Per Page
$limit = 10;
// Grab Search Variable
$var = @$_GET['q'] ;
// Trim Whitespace from the Stored Variable
$trimmed = trim($var);
// Separate Key-phrases into Keywords
$trimmed_array = explode(" ",$trimmed);
// Check for an empty string and display a message
if ($trimmed == "") {
include("searchfield.html");
$resultmsg = "<p><b>Search ERROR: </b><p>Please try again</p><br><br>" ;
}
// Check for a Search Parameter
if (!isset($var)){
$resultmsg = "<p><b>Search ERROR: </b><p>Search will not execute! We don't seem to have a search parameter! </p>" ;
}
// Build SQL Query for each keyword entered
foreach ($trimmed_array as $trimm){
// MOD Here to specify our table and field names for the SQL query
$query = "SELECT * FROM Products WHERE ProductID LIKE \"%$trimm%\"
OR ProductKeywords LIKE \"%$trimm%\"
OR ProductIcon LIKE \"%$trimm%\"
OR ProductColor LIKE \"%$trimm%\"
OR ProductDescription LIKE \"%$trimm%\"
ORDER BY ProductID DESC" ;
// Execute the query to get number of rows that contain search kewords
$numresults=mysql_query ($query);
$row_num_links_main =mysql_num_rows ($numresults);
// Determine if 's' has been passed to script, if not use 0.
// 's' is a variable that gets set as we navigate the search result pages.
if (empty($s)) {
$s=0;
}
// Get results
$query .= " LIMIT $s,$limit" ;
$numresults = mysql_query ($query) or die ( "Couldn't execute query" );
$row= mysql_fetch_array ($numresults);
// Store record id of every item that contains the keyword in the array we need to do this to avoid display of duplicate search result.
do{
// Primary Key
$adid_array[] = $row[ 'ProductID' ];
}while( $row= mysql_fetch_array($numresults));
} // End foreach
if($row_num_links_main == 0 && $row_set_num == 0){
include("searchfield.html");
$resultmsg = "<p><b>Current Product Listing for: </b>" . $trimmed ."</p><p>Sorry, we could not find anything to match your search criteria, please try again.</p>" ;
}
// Delete duplicate record id's from the array. Using array_unique function
$tmparr = array_unique($adid_array);
$i=0;
foreach ($tmparr as $v) {
$newarr[$i] = $v;
$i++;
}
// Now display results returned. First display the search form on the top of the page
?>
<?php
// Display what the person searched for
if( isset ($resultmsg)){
echo $resultmsg;
exit();
}else{
echo "<p><b>Current Product Listing for: </b>" . $var;
}
foreach($newarr as $value){
// Specify our table and field names for the SQL query
$query_value = "SELECT * FROM Products WHERE ProductID = '$value'";
$num_value=mysql_query ($query_value);
$row_linkcat= mysql_fetch_array ($num_value);
$row_num_links= mysql_num_rows ($num_value);
// Try to make the keywods bold. Use preg_replace function?
$titlehigh = preg_replace ( "'($var)'si" , "\\1" , $row_linkcat[ 'ProductID' ] );
$linkhigh = preg_replace ( "'($var)'si" , "\\1" , $row_linkcat[ 'ProductKeywords' ] );
$linkicon = preg_replace ( "'($var)'si" , "\\1" , $row_linkcat[ 'ProductIcon' ] );
$linkcolor = preg_replace ( "'($var)'si" , "\\1" , $row_linkcat[ 'ProductColor' ] );
$linkdesc = preg_replace ( "'($var)'si" , "\\1" , $row_linkcat[ 'ProductDescription' ] );
foreach($trimmed_array as $trimm){
if($trimm != 'b' ){
//Highlight try?
$titlehigh = preg_replace( "'($trimm)'si" , "\\1" , $titlehigh);
$linkhigh = preg_replace( "'($trimm)'si" , "\\1" , $linkhigh);
$linkicon = preg_replace( "'($trimm)'si" , "\\1" , $linkicon);
$linkcolor = preg_replace( "'($trimm)'si" , "\\1" , $linkcolor);
$linkdesc = preg_replace( "'($trimm)'si" , "\\1" , $linkdesc);
}
//end highlight
?>
<table border="0" cellpadding="6" cellspacing="0" style="border-collapse: collapse" bordercolor="#99AEB3" width="100%" id="AutoNumber1">
<tr>
<td width="100">
<p align="center"><?php echo $linkicon; ?></td>
<td align="left" valign="middle"><?php echo $titlehigh; ?></td>
<td>
<table border="1" cellpadding="6" cellspacing="0" style="border-collapse: collapse" bordercolor="#C0C0C0" width="100%" id="AutoNumber2">
<tr>
<td width="100%"><?php echo $linkhigh; ?></td>
</tr>
<tr>
<td width="100%"><?php echo $linkdesc; ?></td>
</tr>
</table>
</td>
<td width="50" align="right" valign="middle"><?php echo $linkcolor; ?></td>
</tr>
</table>
<?php
} //end foreach $trimmed_array
if($row_num_links_main > $limit){
// check to see if last page
$slimit =$s+$limit;
if (!($slimit >= $row_num_links_main) && $row_num_links_main!=1) {
}
}
} //end foreach $newarr
?>