【问题标题】:PHP: In shopping cart, Fatal error: Call to undefined function getCart()PHP:在购物车中,致命错误:调用未定义的函数 getCart()
【发布时间】:2012-06-06 13:04:45
【问题描述】:

最近我从 php 开始,而不是因为它给了我这个错误。 此代码仅用于在重新加载页面时显示文章和购物车总价,使用 PHPSESSID,如果他们对该功能有更好的想法或替代方案,不胜感激!

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

【问题讨论】:

  • 从错误信息中究竟有什么不清楚的地方?

标签: php function session shopping-cart


【解决方案1】:

首先创建DB_Functions实例

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

【讨论】:

  • DB_Functions 后面不需要'()' 吗?
  • 它是可选的,只有当你有参数要传递时才需要
  • 为我工作,但你的答案和@Gustav 一样:/
  • @SoldierCorp:因为那是显而易见的问题,是的,我的答案是一样的,他的答案也和我的一样,没有什么难弄清楚或互相复制的 :)
【解决方案2】:

你应该初始化对象,然后使用它的方法。像这样:

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

【讨论】:

  • 为我工作!但是,现在我的问题是不在 div 中打印 totalItems 和 totalPrince :/,即从 div 中打印 totalItems bot!
【解决方案3】:

你用两个 ss 拼写了 DB_Functions:

require("php/DB_Functionss.php");

【讨论】:

  • 他可能是这样命名文件的
【解决方案4】:

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

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

这是错误的:&lt;/b&gt;&lt;otal:

另外,您在这里关闭了&lt;/a&gt;,但您从未打开过。

固定:

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

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

$sessionID = session_id();

【讨论】:

    【解决方案5】:

    正如我所见,你不需要定义类,你可以做古斯塔夫提到的事情。 (你必须先初始化一个对象然后调用方法)

    我认为你最好使用类外的函数,或者使用静态方法

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

    【讨论】:

      【解决方案6】:

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

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

      在 jquery 中

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2015-06-23
        • 1970-01-01
        • 1970-01-01
        • 2016-02-20
        • 2014-07-21
        • 2016-07-02
        相关资源
        最近更新 更多