user9771053
user9771053

Reputation:

PHP search function, how to give every result a HREF LINK

i found this code below online. But my only problem is how to fill in the page name inside <a href"--dynamic-pagename--">. That it will have a appropriate name for every result depending on what the name is of the result. How can this be achieved.

<?php
// create a new function
function search($text){

    // connection to the Ddatabase
include('config/database_connection.php');
    // let's filter the data that comes in
    $text = htmlspecialchars($text);
    // prepare the mysql query to select the users
    $get_name = $connect->prepare("SELECT name FROM products WHERE name LIKE concat('%', :name, '%')");
    // execute the query
    $get_name -> execute(array('name' => $text));
    // show the users on the page
    while($names = $get_name->fetch(PDO::FETCH_ASSOC)){


        // I AM TALKING ABOUT THE LINE BELOW
        echo '<a href="">'.$names['name'].'</a>';


    }
}
// call the search function with the data sent from Ajax
search($_GET['txt']);
?>

Thanks for your time and answer.

Upvotes: 0

Views: 235

Answers (1)

ikiK
ikiK

Reputation: 6532

You need to add in table products one more column LINK. Populate it with desired link for every name.

And you call your link same as a name like this:

 echo '<a href="'.$names['link'].'">'.$names['name'].'</a>';

Also add to your SQL query:

SELECT name, link FROM products WHERE name LIKE concat('%', :name, '%')

Upvotes: 1

Related Questions