php - 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']);
?>
感谢您的时间和回答。
解决方案
您需要在表格产品中再添加一列 LINK。使用每个名称的所需链接填充它。
您将链接称为与以下名称相同的名称:
echo '<a href="'.$names['link'].'">'.$names['name'].'</a>';
还要添加到您的 SQL 查询中:
SELECT name, link FROM products WHERE name LIKE concat('%', :name, '%')
推荐阅读
- google-cloud-shell - GCP:如何确保在 Cloud Shell Editor 中设置了 gcp 项目
- r - 如何使用 get_elev_point 同时访问多个高程点
- mongodb - 具有重复键错误的 mongorestore 非常慢
- python - Django Whitenoise 导致收集静态错误
- asp.net-core - “dotnet new angular”应用程序中的脚手架身份出现错误
- android - 在 API 30 中为 resolveActivity 写入清单的意图操作
- react-native - 如何在本机反应中使用嵌套导航堆栈
- kubernetes - 如何使用 index.html 中的自定义文本在 nginx 上创建部署?
- json - 我如何能够比较两组 JSON 数据并检查一组是否在另一组中
- javascript - Async/Await:推理与实现