Jump to content

Unable to Display My Products from the DB in the DOM


Recommended Posts


After following along and successfully completing Ben's tutorial of the Online Shopping Cart, I figured I would try and make my own. 

The first major problem I am having is being able to display my products from my database in my index.php page. The goal is to actually see the images of each product displayed in index.php (like in the tutorial). This would then enable me to add my products to the cart.php page. 

I've checked the database connection and it does exist. My guess would be that I am using the wrong syntax to display my products in the "fetch_all_products()" method. 




class Products 
    private $Conn;
    private $db_table = "toys"; 

    function __construct() {
        // here we're making sure that the connection from $conn in "init.php" is transferred 
        // into our own private $conn property for usage in this object
        global $Conn; 
        $this->Conn = $Conn;

        // fetches and displays all products from db
        public function fetch_all_products($id = NULL)
            if ($id = NULL)
                $data = "<li>"; 
                if ($result = $this->conn->query("SELECT * FROM " . $this->db_table . " ORDER BY name"))
                    if ($result->num_rows > 0)
                        while ($row = $result->fetch_array())
                            $data .= array(
                            "id" => $row["product_id"],
                            "name" => $row["name"],
                            "price" => $row["price"],
                            "image" => $row["image"]
                        $data .= "</li>"; 
                        return $data; 
                    return "<h1>Oops... Something went wrong!</h1>"; 

*** INIT.PHP ***

// db connection
$host = "localhost"; 
$user = "root";
$pass = "root";
$db = "cart"; 

$Conn = new mysqli($host, $user, $pass, $db); 

// error reporting 
ini_set("display_errors", 1); 

// constants
define("shop_name", "Tremendous Toys"); 
define("main_path", "localhost/web_apps/shopping_cart/");
define("image_path", "localhost/web_apps/shopping_cart/images/");  

// includes 

// object integrations
$Shopping_Cart = new Shopping_Cart(); 
$Products = new Products();

// session initialisation

*** INDEX.PHP ***
  <?php include("init.php") ?>

<link rel="stylesheet" href="visual/style.css" type="text/css">

    <div id="whitespace">
        <h1><?php echo shop_name ?></h1>





Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

  • Create New...