我正在制作一个带有搜索栏的网站。一旦“搜索”并显示结果,我想让搜索栏具有交互性。所以我希望 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/