user1572621
user1572621

Reputation: 1

How Do You Add A Link To A Search Results

I have written this code to enable a search of my database and to then display the results. (code copied below)

I need some help as I need to know how to then create a link from the search results so they can click on the result and it automatically opens the details on a new webpage.

I am very new to PHP so any help anyone can give is appreciated.

Thanks

<?php
error_reporting(E_ALL);
ini_set('display_errors', '1');
$search_output = "";
if(isset($_POST['searchquery']) && $_POST['searchquery'] != ""){
     $searchquery = preg_replace('#[^a-z 0-9?!]#i', '', $_POST['searchquery']);
     if($_POST['filter1'] == "Whole Site"){
         $sqlCommand = "(SELECT id, title AS title FROM cars WHERE title LIKE '%$searchquery%' OR description LIKE '%$searchquery%') UNION (SELECT id, title AS title FROM motorcycles WHERE title LIKE '%$searchquery%' OR description LIKE '%$searchquery%')";
     } else if($_POST['filter1'] == "cars"){
         $sqlCommand = "SELECT id, title AS title FROM cars WHERE title LIKE '%$searchquery%' OR description LIKE '%$searchquery%'";
     } else if($_POST['filter1'] == "motorcycles"){
         $sqlCommand = "SELECT id, title AS title FROM motorcycles WHERE title LIKE '%$searchquery%' OR description LIKE '%$searchquery%'";
     }
        include_once("testconn2.php");
        $query = mysql_query($sqlCommand) or die(mysql_error());
     $count = mysql_num_rows($query);
     if($count > 1){
         $search_output .= "<hr />$count results for <strong>$searchquery</strong><hr />$sqlCommand<hr />";
         while($row = mysql_fetch_array($query)){
                 $id = $row["id"];
            $title = $row["title"];
            $search_output .= '<a href="item.php"' .$_SERVER['PHP_SELF'] . '"?c=$catId&p=' . $id . '">' .$title . '</a><br>';
                } // close while
     } else {
         $search_output = "<hr />0 results for <strong>$searchquery</strong><hr />$sqlCommand";
     }
}
?>
<html>
<head>
</head>
<body>
<h2>Search the Database </h2>   <h4><a href="Sign in.php">Sign in</a> <a href="UserRegistration.php">Register</a></h4>
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
Search For: 
  <input name="searchquery" type="text" size="44" maxlength="88"> 
Within: 
<select name="filter1">
<option value="Whole Site">Whole Site</option>
<option value="Cars">Cars</option>
<option value="Motorcycles">Motorcycles</option>
</select>
<input name="myBtn" type="submit">
<br />
</form>
<div>
<?php echo $search_output; ?>
</div>
</body>
</html>

Upvotes: 0

Views: 4849

Answers (4)

coyotebush
coyotebush

Reputation: 653

$search_output .= '<a href="item.php"' .$_SERVER['PHP_SELF'] . '"?c=$catId&p=' . $id . '">' .$title . '</a><br>';

With $_SERVER['PHP_SELF'] included outside the double quotes, that looks like it would produce something like

<a href="item.php"search.php"?c=1&p=1">Title</a><br>

More likely you just want (oh, and encode your ampersands)

$search_output .= '<a href="item.php?$catId&amp;p=$id">$title</a><br>';

Upvotes: 0

Jorge Zapata
Jorge Zapata

Reputation: 2336

<a href="item.php"' .$_SERVER['PHP_SELF'] . '"?c=$catId&p=' . $id . '" taget="_blank">' .$title . '</a><br>';

I that part just use the "_blank" target and use $_GET variable in your linked page to show your result.

Upvotes: 0

Brock B.
Brock B.

Reputation: 377

It looks like you already have a link to the search results here, correct?

 $search_output .= '<a href="item.php"' .$_SERVER['PHP_SELF'] . '"?c=$catId&p=' . $id . '">' .$title . '</a><br>';

You might want to remove the $_SERVER['PHP_SELF'] or move it before the item.php, as it will append that to your file name, which I am sure you don't want.

Now all you should have to do is create your item.php page, and use $_GET['id'] to get the ID of the item you are looking for.

Once you get the item you are looking for, you can then do a query on your database.

You should also look into using PDO or mysqli rather than regular mysql commands, as they are not what's recommended to be used nowadays.

For reference to get, you can try w3schools or PHP's manual.

Hope this helps!

Upvotes: 2

Jocelyn
Jocelyn

Reputation: 11393

Replace the form method with "get" instead of "post", then when the form is submitted it will generate the link you are looking for.

<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="get">

The link will look like:

http://yourwebsite/yourfile.php?searchquery=sometext&fillter1=somevalue1&myBtn=submit

Upvotes: 0

Related Questions