SoldierCorp
SoldierCorp

Reputation: 7700

PHP: In shopping cart, Fatal error: Call to undefined function getCart()

Recently I start with php and not because it gives me this error. This code is onyl for show articles and total price of shopping cart when reload page, using PHPSESSID, if they have a better idea or an alternative for that function, they are appreciated!

INDEX.PHP

<?php require("php/DB_Functionss.php"); ?>
<!doctype html>
<html lang="es">
<head>
    ........
</head>
<body>
    <div id="infoShopCart">
       <a href="contShopping"><img src="img/shopping_cart.png" width="30px"/>
       Artículos: <b id="cantArticles">0</b>
       Total: $ <b id="totalPriceArticles">0.00</b></a>
       <?php getCart(); ?>
    </div>
.....
....
</body>
</html>

AND DB_Functions.php

session_start();
$sessionID = $_COOKIE['PHPSESSID'];

    class DB_Functions {

        private $db;
        //put your code here
        //constructor
        function __construct() {
            require_once 'DB_Connect.php';
            //connecting to database
            $this->db = new DB_Connect();
            $this->db->connect();
        }
        //destructor
        function __destruct() {

        }
      public function getCart(){
            $query ="SELECT * FROM carrito ORDER BY id_pelicula";
            $result = mysql_query($query) or die(mysql_error());

            $no_of_rows = mysql_num_rows($result);
            if($no_of_rows > 0) {
                while ($row = mysql_fetch_assoc($result)) {
                    $totalItems     = $totalItems + 1;
                    $movieTotalPrice    = $movieTotalPrice + $row['precio_pelicula'];
                }
                echo ("Artículos: <b id='cantArticles'>"+$totalItems+"</b><otal: $ <b id='totalPriceArticles'>"+$totalPrice+"</b></a>");
            } else {
                return false;
            }
        }

Upvotes: 0

Views: 1058

Answers (6)

Arun
Arun

Reputation: 1

You can encounter that problem with the help of some CSS or Jquery addons.. I supposes.. and also increase the width more.. Yours is 38px only I suppose or else you can use

$('div').getWidth()+some number;

in jquery

Upvotes: 0

Ocean
Ocean

Reputation: 2942

As I see, you don't need define class, you can do something that Gustav mentioned. (you must first initiate an object and then call the method)

I think it is better that you use out of class function, or using static method, instead.

session_start();
$sessionID = $_COOKIE['PHPSESSID'];

    class DB_Functions {

        private $db;
        //put your code here
        //constructor
        static function initDb() {
            require_once 'DB_Connect.php';
            //connecting to database
            $this->db = new DB_Connect();
            $this->db->connect();
        }
      public static function getCart(){
            $query ="SELECT * FROM carrito ORDER BY id_pelicula";
            $result = mysql_query($query) or die(mysql_error());

            $no_of_rows = mysql_num_rows($result);
            if($no_of_rows > 0) {
                while ($row = mysql_fetch_assoc($result)) {
                    $totalItems     = $totalItems + 1;
                    $movieTotalPrice    = $movieTotalPrice + $row['precio_pelicula'];
                }
                echo ("Artículos: <b id='cantArticles'>"+$totalItems+"</b><otal: $ <b id='totalPriceArticles'>"+$totalPrice+"</b></a>");
            } else {
                return false;
            }
        }

or using without declaring them in class

session_start();
$sessionID = $_COOKIE['PHPSESSID'];

        //put your code here
        function initDb() {
            require_once 'DB_Connect.php';
            //connecting to database
            $db = new DB_Connect();
            $db->connect();
        }
            function getCart(){
            $query ="SELECT * FROM carrito ORDER BY id_pelicula";
            $result = mysql_query($query) or die(mysql_error());

            $no_of_rows = mysql_num_rows($result);
            if($no_of_rows > 0) {
                while ($row = mysql_fetch_assoc($result)) {
                    $totalItems     = $totalItems + 1;
                    $movieTotalPrice    = $movieTotalPrice + $row['precio_pelicula'];
                }
                echo ("Artículos: <b id='cantArticles'>"+$totalItems+"</b><otal: $ <b id='totalPriceArticles'>"+$totalPrice+"</b></a>");
            } else {
                return false;
            }
        }

Upvotes: 0

MrCode
MrCode

Reputation: 64526

Apart from not instantiating the object as other answers have said, you have problems here:

echo ("Artículos: <b id='cantArticles'>"+$totalItems+"</b><otal: $ <b id='totalPriceArticles'>"+$totalPrice+"</b></a>");

This is wrong: </b><otal:

Also you are closing an </a> here but you never opened one to begin with.

Fixed:

echo ("Artículos: <b id='cantArticles'>"+$totalItems+"</b>Total: $ <b id='totalPriceArticles'>"+$totalPrice+"</b>");

Additionally, there is a better way to obtain the current session id instead of trying to read the raw cookie:

$sessionID = session_id();

Upvotes: 0

skashi
skashi

Reputation: 335

You have spelt DB_Functions with two ss's:

require("php/DB_Functionss.php");

Upvotes: 0

Sarfraz
Sarfraz

Reputation: 382696

Create an instance of DB_Functions first:

$dbf = new DB_Functions;
$dbf->getCart();

Upvotes: 1

Gustav
Gustav

Reputation: 1341

You should initialize the object and then use its method. Like so:

<div id="infoShopCart">
   <a href="contShopping"><img src="img/shopping_cart.png" width="30px"/>
   Artículos: <b id="cantArticles">0</b>
   Total: $ <b id="totalPriceArticles">0.00</b></a>
   <?php
       $db_object = new DB_Functions();
       $db_object->getCart();
   ?>
</div>

Upvotes: 0

Related Questions