...

View Full Version : PHP hyperlink in mysql search result title



camzenxbt
10-13-2012, 12:40 PM
Hi there,

I am currently attempting to edit a simple search form script. What I am wanting is for, when a search result is displayed, the title of the search result to be hyperlinked to the page.

I am not very good with PHP, thats why I am creating a simple 'free flash games' website to improve my skills. I want the user to be able to search for a game, and the title of the game to be hyperlinked - the title of the game will be hyperlinked by getting the url from the table, but I am not sure how to do this.

Heres my code:


<?php

include 'dbc.php';

// 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) < 3) {
$error[] = "Search terms must be longer than 3 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 sid, sbody, stitle, sdescription FROM search WHERE ";
// grab the search types.
$types = array();
$types[] = isset($_GET['body'])?"`sbody` LIKE '%{$searchTermDB}%'":'';
$types[] = isset($_GET['url'])?"`surl` LIKE '%{$searchTermDB}%'":'';
$types[] = isset($_GET['title'])?"`stitle` LIKE '%{$searchTermDB}%'":'';
$types[] = isset($_GET['desc'])?"`sdescription` LIKE '%{$searchTermDB}%'":'';
$types = array_filter($types, "removeEmpty"); // removes any item that was empty (not checked)

if (count($types) < 1)
$types[] = "`sbody` 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 `stitle`"; // 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[] = "{$i}: {$row['stitle']}<br />{$row['sdescription']}<br />{$row['sbody']}<br /><br />";
$i++;
}
}
}
}

function removeEmpty($var) {
return (!empty($var));
}


?>
<html>
<title>My Simple Search Form</title>
<style type="text/css">
#error {
color: red;
}
</style>
<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">
Search For: <input type="text" name="search" value="<?php echo isset($searchTerms)?htmlspecialchars($searchTerms):''; ?>" /><input type="submit" name="submit" value="Search!" />
</form>
<?php echo (count($results) > 0)?"<h6>Games found in the UniVerse for '{$searchTerms}' are:</h6><br /><br />" . implode("", $results):""; ?>
</body>
</html>

Is there a way I can add an additional snippet of code which will hyperlink the title in the search results?

Thanks for your help! :D

Redcoder
10-14-2012, 01:00 PM
I've not read your script...but if you want to hyperlink to that page, simply make the title of the result to be a link to that page.

How - Here's one way:

Maybe the pages' contents are dynamic because they all use one script whose HTML content changes. So maybe the url goes something like yourwebsite.com/index.php?id=2

Where 2 is the id of the game or whatever product it is.

So essentially, you also get the ID of the game/product from the database as you fetch other things. This makes the results easily dynamic.

Then do something like:




while($r = mysqli_fetch_assoc(mysqli_query($link,"SELECT * FROM games WHERE game_name LIKE '%search_term%'")){

$id = $r['id'];


echo <<<CODE

<div>
<p><a href='mywebsite.com/index.php?id={$id}'The Title of The search results</a>
</p>

<p>
This is the newest game in the COD trilogy. The franchise has hit it perfect this time.
</p>
</div>
CODE;
}



Using the above or whatever variation you choose, makes the results dynamic and therefore the title hyperlink relevant to the page.

The <<< i used in the code is the heredoc (http://php.net/manual/en/language.types.string.php) , that is if you didn't know about it.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum