Showing Results - PHP / SQL

Hi.

I have coded a simple search bar which shows results based on my databse. Its based on Whiskie. Nice… So this is part one of a two part question, Part two to come later. Basically what I want to do is have the search results show the name , then have the name be a link to more details. The search results currently show a box with the name , last price paid and the date. Really appreciate the help.

Here is my code

<?php

$page='search';
include('header.php');
include ('navbar.php');
echo "<br>";
include ('connect.php');



if (isset ($_POST['search'])) { //the 'search' refers to the 'search' name=search on the index page and makes does something when the search is pushed.
     $search = $_POST['search'];
     $search = "%" . $search . "%"; // MySQL wildcard % either side of search to get partially matching results

                                   //  No wildcard if you want results to match fully
} else {

header ('location: index.php');

}



$stmt = $conn->prepare("SELECT * FROM test_db WHERE name LIKE :name ORDER BY name ASC"); // Use = instead of LIKE for full matching
$stmt->bindParam(':name', $search);  
$stmt->execute();
$count = $stmt->rowCount(); // Added to count no. of results returned


if ($count >= 1) { // Only displays results if $count is 1 or more

    echo "<div class='results_found'>";
    echo $count;
    echo " results found<br>";
    echo "</div>";

while ($row = $stmt->fetch(PDO::FETCH_ASSOC)) {

echo "<div class='results'>";
        echo "<div class='result_name'>";
                echo "<b>Whisky Name:</b><br>";
                echo $row['name'];
        echo "</div>";

    echo "<div class= 'results_body'>";
                
        echo "<div class= 'result_lowprice'>";
                echo "Price Paid: £";
                echo $row['price'];
        echo "</div>";

        echo "<div class= 'last_date'>";
                echo "Last date sold: ";
                echo $row['date'];
        echo "</div>";
    echo "</div>";
echo "</div>";

}

} else {
      echo " Sorry no records were found";
}

?>

Unless I’m missing something, you just need to replace this line

with something like

echo "<a href=\"http://www.example.com/\">" . $row['name'] . "</a>";

You can put inside an anchor element the following:

cho "<div class='results'>";
        echo "<div class='result_name'><a href='here_put_the_link_to_show_whatever_you_want' ">;
                echo "<b>Whisky Name:</b><br>";
                echo $row['name'];
        echo "</a></div>";

Got my answer thanks

if (isset($_GET['id'])) {
    $sql = "SELECT name FROM test_db WHERE lot_id = :id";
    $stmt = $conn->prepare($sql);
$stmt->execute( [ 'id' => $_GET['id'] ] );
$row = $stmt->fetch();
echo $row['name'];
}

OK, I’m not sure how that gives you your answer since that’s just giving you the name, and not a link, but I’m glad you figured it out. :shifty:

I figured that was the code for the destination of the “more information” link.

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.