php - 用 php 表单更改 href 链接

标签 php html search hyperlink tags

我正在制作一个带有搜索栏的网站。一旦“搜索”并显示结果,我想让搜索栏具有交互性。所以我希望 href 根据正在使用的 Id 进行更改。例如:有人搜索“Pinecones”,如果它在数据库中,它就会有一个 ID,在这个例子中它是 #4。一旦他们搜索了它,它就会显示为一个链接。但我希望链接使用“/#IDNumber.php”

这是我使用的代码:

<?php
$output = '';
//collect
if(isset($_POST['search'])) {
$searchq = $_POST['search'];
$searchq = preg_replace("#[^0-9a-z]#i","",$searchq);

$query = mysql_query("SELECT * FROM `findgame` WHERE name LIKE '%$searchq%'       OR keywords LIKE '%$searchq%' LIMIT 1") or die("Search unavailable.");
$count = mysql_num_rows($query);
if($count == 0){
    $output = 'Results not found.';

}else{
    while($row = mysql_fetch_array($query)) {
        $name = $row['name'];
        $kwords = $row['keywords'];
        $id = $row['id'];

        $output .= '<div style="position:absolute;margin:110px    20px;padding:25px;">'.$id.' '.$name.'</div>';
        }

}

}

?>

<a href="/<?php$id?>.php">    <?php print("$output");?></a>

有什么帮助让它发挥作用吗?

最佳答案

您需要打印变量。

$id = 123;

<?php $id ?>      =>
<?php echo $id ?> => 123
<?= $id ?>        => 123

所以最终的结果应该是这样的:

<a href="/<?= $id ?>.php">
    <?php print($output); ?>
</a>

注意:您不需要 $output 周围的 "。这不会有坏处,但不是必需的。

关于php - 用 php 表单更改 href 链接,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30010135/

相关文章:

python - 如何在 python 中编写正则表达式来识别字符串中的日期

php - Laravel 找不到 css 文件:GET http://localhost:8000/cssFile net::ERR_ABORTED

Java 中 "PBKDF2WithHmacSHA256"的 PHP 替代方案

php 内容类型

html - 中心 div 元素

javascript - 通过下拉选择器angularjs更改动画图像

javascript - Html5 Canvas 游戏移动x、y

php - 无法在 Safari 中填充下拉列表

search - wireshark 在 TCP 流中搜索字符串

search - 弹性(搜索): Get docs with max and min timestamp values