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 iOS绕地区网络检测
  • ¥15 python验证码滑块图像识别
  • ¥15 根据背景及设计要求撰写设计报告
  • ¥15 QT6颜色选择对话框显示不完整
  • ¥20 能提供一下思路或者代码吗
  • ¥15 用twincat控制!
  • ¥15 请问一下这个运行结果是怎么来的
  • ¥15 单通道放大电路的工作原理
  • ¥30 YOLO检测微调结果p为1
  • ¥15 DS18B20内部ADC模数转换器