首页 > 解决方案 > PHP搜索功能,如何给每个结果一个HREF LINK

问题描述

我在网上找到了下面的代码。但我唯一的问题是如何在里面填写页面名称<a href"--dynamic-pagename--">。根据结果​​的名称,它将为每个结果具有适当的名称。这怎么可能实现。

<?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']);
?>

感谢您的时间和回答。

标签: php

解决方案


您需要在表格产品中再添加一列 LINK。使用每个名称的所需链接填充它。

您将链接称为与以下名称相同的名称:

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

还要添加到您的 SQL 查询中:

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

推荐阅读