Hello and welcome to our community! Is this your first visit?
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 2 of 2
  1. #1
    New Coder
    Join Date
    Aug 2011
    Thanked 0 Times in 0 Posts

    PHP hyperlink in mysql search result title

    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:

    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 />";
    function removeEmpty($var) {
       return (!empty($var));
       <title>My Simple Search Form</title>
       <style type="text/css">
          #error {
             color: red;
          <?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!" />
          <?php echo (count($results) > 0)?"<h6>Games found in the UniVerse for '{$searchTerms}' are:</h6><br /><br />" . implode("", $results):""; ?>
    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!

  2. #2
    Regular Coder Redcoder's Avatar
    Join Date
    May 2012
    Thanked 49 Times in 48 Posts
    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:

    PHP Code:

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

    $id $r['id'];

    echo <<<CODE

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

    This is the newest game in the COD trilogy. The franchise has hit it perfect this time. 

    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 , that is if you didn't know about it.

  3. Users who have thanked Redcoder for this post:

    camzenxbt (10-15-2012)


Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts