Ok here is what i am trying to do. When a user enters a search and hits submit the script is suppose query the db and search table named products and look for the cell named keywords, and then display results in the following way based on those results:
line 1 / Cell named item_code
line 2 / Cell named description
line 3 / cell named link ( links to items spec sheet that is stored in db Shown as a link)
What is happening is search is returning results however if there is more than one results the link is spreading to the following search result, for an example results look like this :
12-177
Bakers Pride White Bread
View Details For This Item <----Should show hyperlink only here
:13-218
BUTTER KRUST White Pullman 27oz
View Details For This Item <--------------|
| However link is spreading across these three fields not just where it reads
| "view details for this item" and then repeats all the way down the search results.
:13-150 <-------------------------------------|
Holsum Country Style 23oz <-------------|
View Details For This Item
Here is the code i am using i know the problems lies in the displaying of the results but am at a loss any ideas. I am using php5 and MySql server ver 5
require_once ('errorscript.php');
require_once ('mysql connection file'); // Connect to the database.
// Set up our error check and result check array
$error = array();
$results = array();
// First check if a form was submitted.
// Since this is a search we will use $_GET
if (isset($_GET['search'])) {
$searchTerms = trim($_GET['search']);
$searchTerms = strip_tags($searchTerms); // remove any html/javascript.
if (strlen($searchTerms) < 2) {
$error[] = "Search terms must be longer than 2 characters.";
}else {
$searchTermDB = mysql_real_escape_string($searchTerms); // prevent sql injection.
}
// If there are no errors, lets get the search going.
if (count($error) < 1) {
$searchSQL = "SELECT item_code, description, link, keywords FROM products WHERE ";
// grab the search types.
$types = array();
$types[] = isset($_GET['keywords'])?"`keywords` LIKE '%{$searchTermDB}%'":'';
$types = array_filter($types, "removeEmpty"); // removes any item that was empty (not checked)
if (count($types) < 1)
$types[] = "`keywords` LIKE '%{$searchTermDB}%'"; // use the body as a default search if none are checked
$andOr = isset($_GET['matchall'])?'AND':'OR';
$searchSQL .= implode(" {$andOr} ", $types) . " ORDER BY `keywords`"; // order by title.
$searchResult = mysql_query($searchSQL) or trigger_error("There was an error.<br/>" . mysql_error() . "<br />SQL Was: {$searchSQL}");
if (mysql_num_rows($searchResult) < 1) {
$error[] = "The search term provided {$searchTerms} yielded no results.";
}else {
$results = array(); // the result array
$i = 1;
while ($row = mysql_fetch_assoc($searchResult)) {
$results[] = " :{$row['item_code']}<br /> {$row['description']}<br /> {$row['link']}<br /><br />";
}
}
}
}
function removeEmpty($var) {
return (!empty($var));
}
<body>
<?php echo (count($error) > 0)?"The following had errors:<br /><span id=\"error\">" . implode("<br />", $error) . "</span><br /><br />":""; ?>
<form method="GET" action="<?php echo $_SERVER['PHP_SELF'];?>" name="searchForm">
What are you looking for: <input name="search" type="text" value="<?php echo isset($searchTerms)?htmlspecialchars($searchTerms):''; ?>" size="50" /><input type="submit" name="submit" value="Search" />
</form>
<?php echo (count($results) > 0)?"Your search: {$searchTerms} returned:<br /><br />" . implode("", $results):""; ?>