Skip to content
Advertisement

value from database not appearing into php

i have created a cart website using sql, html and php. the cart button is as below:

<form method="post" action="cart.php" class="form-inline">
<input type="hidden" value="' . $product['id'] . '" name="product_id">
<input type="hidden" value="add_to_cart" name="add_to_cart">
<button type="submit" name="id" value="12" class="btn btn-primary">Add to Cart</button>
</form>

this is how my cart looks like:

<div class="card">
        <h5 class="card-header">My Cart</h5>
            <div class="card-body">

                <?php
                    if(isset($_SESSION['shopping_cart']) && count($_SESSION['shopping_cart']) > 0)
                    {
                        $entertainment = $_SESSION['shopping_cart'];

                        echo '
                                <table class="table table-hover table-bordered">
                                <thead>
                                    <tr>
                                    <th scope="col">#</th>
                                    <th scope="col">Title</th>
                                    <th scope="col">Quantity</th>
                                    <th scope="col">Price</th>
                                    <th scope="col" width="100">Action</th>
                                    </tr>
                                </thead>';

                        $item_number = 1;
                        $total = 0;
                        foreach ($entertainment as $product) {
                        echo '
                                <tbody>
                                    <tr>
                                    <th scope="row">'. $item_number .'</th>
                                    <td>' . $product['title'] . '</td>
                                    <td>'.$product['quantity'].'</td>
                                    <td>$ '. $product['offer_price_corporate_artist']. '</td>
                                    <td>
                                        <a href="cart.php?id_to_remove=' . $item_number . '" class="btn btn-danger btn-sm">X</a>
                                    </td>
                                    </tr>
                                </tbody>
                           ';
                           $total += ($product['offer_price_corporate_artist'] * $product['quantity']);
                            $item_number++;
                        }

                        echo '
                                <tr>
                                    <th colspan="4" align="right">
                                        Total:
                                    </th>
                                    <td>
                                        $ '. $total .'
                                    </td>
                                </tr>
                                </table>';

                    }
                    else {
                        echo '<div class="alert alert-primary" role="alert">
                              Shopping cart is empty, visit <a href="index.php" class="alert-link">products</a> page to add product into shopping cart.
                            </div>';
                    }
                ?>

the library.php file is as follows:

<?php

// load database connection script

include("database_connection.php");

/*
 * Tutorial: PHP MySQL Shopping cart
 *
 * Page: Application library
 * */

class ShopingCart
{



    protected $db;

    function __construct()
    {
        $this->db = DB();
    }

    /**
     * get products list
     *
     * @return array
     */
    public function getProducts()
    {
        $query = "SELECT *  FROM `entertainment`";
        if (!$result = mysqli_query($this->db, $query)) {
            exit(mysqli_error($this->db));
        }
        $data = [];
        if (mysqli_num_rows($result) > 0) {
            while ($row = mysqli_fetch_assoc($result)) {
                $data[] = $row;
            }
        }

        return $data;
    }

    /**
        * get given product details
        *
        * @param [integer] $id
        * @return array
        */
       public function getProductDetails($id)
       {
           $id = mysqli_real_escape_string($this->db, $id);
           $query = "SELECT *  FROM `entertainment` WHERE `id` = '$id'";
           if (!$result = mysqli_query($this->db, $query)) {
               exit(mysqli_error($this->db));
           }
           $data = [];
           if (mysqli_num_rows($result) > 0) {
               while ($row = mysqli_fetch_assoc($result)) {
                   $data['id'] = $row['id'];
                   $data['title'] = $row['title'];
                   $data['price'] = $row['offer_price_corporate_artist'];
                   $data['quantity'] = 1;
               }
           }

           return $data;
       }

       /**
        * Add new product into the cart
        *
        * @param [integer] $id
        * @return void
        */
       public function addToCart($id)
       {
           $product = $this->getProductDetails($id);

           $isFound = false;
           $i = 0;

           if (!isset($_SESSION['shopping_cart']) || count($_SESSION['shopping_cart']) < 1)
           {
               $_SESSION['shopping_cart'] = array(0 => $product);
           } else {

               foreach ($_SESSION['shopping_cart'] as $item) {
                   $i++;
                   foreach ($item as $key => $value) {
                       if ($key == "id" && $value == $id) {
                           array_splice($_SESSION['shopping_cart'], $i - 1, 1, array([
                               'id' => $item['id'],
                               'title' => $item['title'],
                               'price' => $item['offer_price_corporate_artist'],
                               'quantity' => $item['quantity'] + 1,
                           ]));
                           $isFound = true;
                       }
                   }

               }
               if ($isFound == false) {
                   array_push($_SESSION['shopping_cart'], $product);
               }
           }

       }

       /**
        * remove existing product from the cart
        *
        * @param [integer] $id
        * @return void
        */
       public function removeProductFromCart($id)
       {
           unset($_SESSION['shopping_cart'][$id - 1]);
       }


}

?>

the thing is when i am adding items to the cart, i am able to display title and cart . but i am not able to display the price, whose field name is offer_price_corporate_artist in my database. can anyone tell me what i am missing.

Advertisement

Answer

Your Model for Shoppping Cart puts the offer_price_corporate_artist column into the returned arrays as a renames occurance called price

$data['price'] = $row['offer_price_corporate_artist'];

So in the other script replace

<td>$ '. $product['offer_price_corporate_artist']. '</td>

with

<td>$ '. $product['price']. '</td>

Small Warning

Unfortunately the getProducts() method does not rename the column as price so you have a bit of an anomily that is going to cause you a problem somewhere

5 People found this is helpful
Advertisement