繁体   English   中英

用php表单更改href链接

[英]Change href link with php form

我正在建立一个带搜索栏的网站。 我希望搜索栏在“搜索”并显示结果后进行交互。 所以我希望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 。它不会受到伤害,但没有必要。

我不清楚问题是什么,例如我不知道你是否将$ id和$ output设置为任何值。 我不知道您发布的代码的上下文是否在PHP字符串中,或​​者它是否在模板中。

但是,如果它只是一个语法问题,那么以下可能会更好:

作为PHP字符串:

$out = '<a href="/' . $id . '">' . $output . '</a>';

替代:

$out = "<a href=\"/$id.php\">$output</a>";

在HTML中:

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

甚至:

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

我希望你的问题都是。

注意打印和回声可以互换使用,人们更喜欢在这种情况下打印,尽管回声稍快一些IIRC。 这些都不是函数,所以不包括像print('blah')这样的括号是合适的。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM