PHP:在购物车中,致命错误:调用未定义函数getCart()

时间:2012-06-06 13:04:45

标签: php function session shopping-cart

最近我开始使用php而不是因为它给了我这个错误。 当使用PHPSESSID重新加载页面时,此代码是显示文章和购物车总价的onyl,如果他们有更好的想法或该功能的替代品,他们表示赞赏!

的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;
            }
        }

6 个答案:

答案 0 :(得分:2)

首先创建DB_Functions实例

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

答案 1 :(得分:0)

您应初始化对象,然后使用其方法。像这样:

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

答案 2 :(得分:0)

你用两个ss拼写了DB_Functions:

  

需要( “PHP / DB_Functionss.php”);

答案 3 :(得分:0)

你可以在一些CSS或Jquery插件的帮助下遇到这个问题..我想..并且还增加了更多的宽度.. 你的只是我想的38px,否则你可以使用

$('div').getWidth()+some number;
在jquery中

答案 4 :(得分:0)

除了没有像其他答案所说的那样实例化对象外,你在这里遇到了问题:

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

这是错误的:</b><otal:

此外,您在此处关闭了</a>,但您从未打开过一个。

修正:

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

此外,还有一种更好的方法来获取当前会话ID,而不是尝试读取原始cookie:

$sessionID = session_id();

答案 5 :(得分:0)

如我所见,你不需要定义类,你可以做一些Gustav提到的事情。 (您必须先启动一个对象,然后调用该方法)

我认为您最好使用课外功能,或使用静态方法

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

或在课堂上使用而不声明它们

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