dongmi3203 2019-04-04 19:31
浏览 96
已采纳

将结果从搜索数据库链接到另一个页面

For a school project, I have to make a webshop with PHP and use a database to search for your products, I have the code to display the results, however, I want to make a link when you click on one of the search results, you go to that product's page.

I've tried looking online but I couldn't seem to find it anywhere, that's why I'm posting this question.

$sql = "SELECT ProductID, ProductTags, ProductName FROM producttabel";
$result = $mysqli->query($sql);

if ($result->num_rows > 0) {
    // output data of each row
    echo '<div class="allepccskop">';
    echo "Onze producten: " . "<br>";
    echo '</div>';

while($row = $result->fetch_assoc()) {
    echo '<div class="allepccs">';
    echo $row["ProductName"]. "<br>";
    echo '</div>';
    }
} else {
    echo "0 results";
}
  • 写回答

1条回答 默认 最新

  • dongtang6718 2019-04-04 22:17
    关注

    I want to make a link when you click on one of the search results, you go to that product's page.

    To make a link to a product page, you need to use html a tag, just wrap it around your product name like this:

    echo '<a href="index.php?id=' . $row["ProductID"] . '">' . $row["ProductName"]. "</a><br>"; 
    

    The href attribute contains your php file name (e.g. index.php) and a GET parameter id to send the product id to the php page.

    Full example:

    Assuming you want to extend your code to display a single product when a product id (ProductID) is provided or all products otherwise.

    This is a simple example how you could extend your code, look at the comments:

    <?php
    
    // take id from the request, otherwise set default to null
    $productId = isset($_GET['id']) ? intval($_GET['id']) : null;
    
    // when we have an id in the url, then we display a product page
    if (!is_null($productId)) {
    
        $sql = 'SELECT ProductID, ProductTags, ProductName FROM producttabel WHERE ProductID = ' . $productId;
        $result = $mysqli->query($sql);
    
        if ($result && $result->num_rows == 1) {
            $row = $result->fetch_assoc();
            $result->free(); // free result set
    
            // output data of each row
            echo '<div class="allepccskop">';
            echo "Product page: #" . $productId . "<br>";
            echo '</div>';
    
            echo '<div class="allepccs">';
            echo $row["ProductName"]. "<br>";
            echo '</div>';
    
        } else {
            echo "Product not found";
        }
    
    // otherwise we show All products page
    } else {
    
        // your code
    
        $sql = 'SELECT ProductID, ProductTags, ProductName FROM producttabel';
        $result = $mysqli->query($sql);
    
        if ($result && $result->num_rows > 0) {
            // output data of each row
            echo '<div class="allepccskop">';
            echo "Onze producten: " . "<br>";
            echo '</div>';
    
            while ($row = $result->fetch_assoc()) {
                echo '<div class="allepccs">';
                echo '<a href="index.php?id=' . $row["ProductID"] . '">' . $row["ProductName"]. "</a><br>"; 
                echo '</div>';
            }
            $result->free(); // free result set
        } else {
            echo "0 results";
        }
    
    }
    
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 我想使用一些网络协议或者部分协议也行,主要想实现类似于traceroute的一定步长内的路由拓扑功能
  • ¥30 深度学习,前后端连接
  • ¥15 孟德尔随机化结果不一致
  • ¥15 apm2.8飞控罗盘bad health,加速度计校准失败
  • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
  • ¥15 谁有desed数据集呀
  • ¥20 手写数字识别运行c仿真时,程序报错错误代码sim211-100
  • ¥15 关于#hadoop#的问题
  • ¥15 (标签-Python|关键词-socket)
  • ¥15 keil里为什么main.c定义的函数在it.c调用不了