Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Foreach loop wont show card php

So I am working on an ecommerce website and I am now working on products page, specifically to show them from the database. Everytime I add the foreach loop, the card just entirely dissapears and I am very lost as to why it would do that.

Here is the code of the class:

<?php
    class DatabaseQueries {
        private $servername = "localhost";
        private $username   = "root";
        private $password   = "";
        private $database   = "adh";
        public  $conn;

        // Database Connection
        public function __construct()
        {
            $this->conn = new mysqli($this->servername, $this->username, $this->password, $this->database);
            if(mysqli_connect_error()) {
             trigger_error("Failed to connect to MySQL: " . mysqli_connect_error());
            }else{
            return $this->conn;
            }
        }

        public function getRecords($table) {
            $query = "SELECT * FROM $table";
            $result = $this->conn->query($query);
            if($result){
                if($result->num_rows > 0){
                    $data = array();
                    while($row = $result->fetch_assoc()) {
                        $date[] = $row;
                    }
                    return $data;
                }else{
                    echo "No records have been found.";
                }
            }else{
                echo "Error in:" . $query . "<br>" . $this->conn->error;
            }
        }
    }

And here is the code of the shop page:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

<?php
    include './classes/databaseQueries.php';
    $obj = new DatabaseQueries();
?>

<div class="container">
    <div class="row">
        <div class="col-3">
            <div class="card border-0" style="width: 18rem;">
                <img src="./img/png1.png" class="card-img-top" alt="...">
                <div class="card-body">
                    <p class="card-text">Some quick example text to build on the card title and make up the bulk of the
                        card's content.</p>
                </div>
            </div>
            <?php
                $products = $obj->getRecords('products');
                foreach($products as $product){
            ?>
            <div class="card border-0" style="width: 18rem;">
                <img src="./<?php echo $product['img_address'] ?>" class="card-img-top" alt="...">
                <div class="card-body">
                    <p class="card-text"><?php echo $product['name'] ?></p>
                    <p class="card-text"><?php echo $product['price'] ?></p>
                </div>
            </div>
            <?php } ?>
        </div>
    </div>
</div>

Also, a link to the repository if you would like to fork it and see for yourself (This is on the branche products):
https://github.com/tcudjoe/apresdesheures/tree/products

I tried to see if the problem was because of the function getRecords() but when var_dumping it seemed to give me the right information.

I even added a card without any php code inside, and this seems to work fine.

I checked if the column names were wrong, and they were all matching.

I checked if there was something wrong with the connection of the database, and all seemed to be well.

>Solution :

you have a typo here :

$data = array();
                    while($row = $result->fetch_assoc()) {
                        $date[] = $row;
                    }
                    return $data;

You put the row in a $datE array instead of $datA

I think that if you var_dump($products) before your loop, you will have an empty data array

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading