使用php显示数据库中的产品

时间:2016-10-18 16:17:13

标签: php

这是一个显示3个产品的PHP代码(来自数据库)。它以原始产品显示产品,但它们是错误的,应该出现在下一个原料中的第一个产品缺失。

<html>
    <head>
    </head>
    <body>
        <?php
        $connect = mysqli_connect("localhost", "root", "", "shopping") or
        die("Please, check your server connection.");
        $query = "SELECT item_code, item_name, description, imagename, price FROM
            products";
        $results = mysqli_query($connect, $query) or die(mysql_error());
        echo "<table border=\"0\">";
        $x = 1;
        echo "<tr>";
        while ($row = mysqli_fetch_array($results, MYSQLI_ASSOC)) {
            if ($x <= 3) {
                $x = $x + 1;
                extract($row);
                echo "<td style=\"padding-right:15px;\">";
                echo "<a href=itemdetails.php?itemcode=$item_code>";
                echo '<img src=' . $imagename . ' style="max-width:220px;max-height:240px;
            width:auto;height:auto;"></img><br/>';
                echo $item_name . '<br/>';
                echo "</a>";
                echo '$' . $price . '<br/>';
                echo "</td>";
            }
            else {
                $x = 1;
                echo "</tr><tr>";
            }
        }
        echo "</table>";
        ?>
    </body>
</html>

2 个答案:

答案 0 :(得分:0)

$x分配为零。 $x=0;

完整代码: -

<html>
    <head>
    </head>
    <body>
        <?php
        $connect = mysqli_connect("localhost", "root", "", "shopping") or
        die("Please, check your server connection.");
        $query = "SELECT item_code, item_name, description, imagename, price FROM products";
        $results = mysqli_query($connect, $query) or die(mysqli_error());
        echo "<table border=\"0\">";
        $x=0;
        echo "<tr>";
        while ($row = mysqli_fetch_array($results, MYSQLI_ASSOC)) {
            if ($x <= 3)
            {
                $x = $x + 1;
                extract($row);
                echo "<td style=\"padding-right:15px;\">";
                echo "<a href=itemdetails.php?itemcode=$item_code>";
                echo '<img src=' . $imagename . ' style="max-width:220px;max-height:240px; width:auto;height:auto;"></img><br/>';
                echo $item_name .'<br/>';
                echo "</a>";
                echo '$'.$price .'<br/>';
                echo "</td>";
            }
            else
            {
                $x=1;
                echo "</tr><tr>";
            }
        }
        echo "</table>";
        ?>
    </body>
</html>

答案 1 :(得分:0)

您只需对代码进行一些修改:

     while ($row = mysqli_fetch_array($results, MYSQLI_ASSOC)) {
            if ($x > 3) {
                  $x = 1;
                  echo "</tr><tr>";
            }

            $x = $x + 1;
            extract($row);
            echo "<td style=\"padding-right:15px;\">";
            echo "<a href=itemdetails.php?itemcode=$item_code>";
            echo '<img src=' . $imagename . ' style="max-width:220px;max-height:240px;
        width:auto;height:auto;"></img><br/>';
            echo $item_name . '<br/>';
            echo "</a>";
            echo '$' . $price . '<br/>';
            echo "</td>";
    }
    echo "</tr></table>";
    ?>
</body>

您可以根据需要更改x的重置...可以重置$ x = 0。我还添加了关闭tr标签,因为否则它会保持打开状态,尽管现在几天浏览器会为你修复丢失的标签。