简体   繁体   中英

displaying products from database using php

This is a php code for displaying 3 products (from database). It displays products in a raw, but their is a fault that the first product that should appears in the very next raw is missing.

<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>

Assign $x to zero. $x=0;

Complete code:-

<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>

You just need little modification to your code:

     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>

You can change reset of x according to your needs ... May be reset $x=0. I also added closing tr tag, because otherwise it will remain open although now days browsers will fix that missing tag for you.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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