【问题标题】:Add link to php value for search添加链接到 php 值以进行搜索
【发布时间】:2014-06-07 15:30:28
【问题描述】:

我创建了一个搜索来填充输入文本的列表。

一切正常,但是我想将输出的名称变成一个链接,该链接转到包含该产品所有详细信息的 id 页面。

例如,当输入测试时,会显示“测试 1”。我希望 1 或 Test 可以点击到 product.php?id=1。

    <?php

$text = $_REQUEST['text'];

$text = preg_replace('#[^a-z0-9]#i', '', $text);

$query = "SELECT * FROM products WHERE product_name LIKE '%" . $text . "%'";
$action = mysql_query($query);
$result = mysql_num_rows($action);



while ($res = mysql_fetch_array($action)) {
        $output = $res['product_name'].' '.$res['id'].'<br />'; //this is the line to add the link to.
        echo $output;
        }
?>

【问题讨论】:

    标签: php sql search hyperlink


    【解决方案1】:
    while ($res = mysql_fetch_array($action)) {
        $output = "<a href=\"product.php?id={$res['id']}\">{$res['product_name']}</a>";
        echo $output;
    }
    

    while ($res = mysql_fetch_array($action)) {
        $output = sprintf('<a href="product.php?id=%u">%s</a>', $res['id'], $res['product_name']);
        echo $output;
    }
    

    【讨论】:

      猜你喜欢
      • 2015-12-23
      • 1970-01-01
      • 2012-10-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-11-22
      • 2021-12-22
      • 2017-08-17
      相关资源
      最近更新 更多