0

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

索引.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>

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

6 回答 6

2

创建first的实例DB_Functions

$dbf = new DB_Functions;
$dbf->getCart();
于 2012-06-06T13:07:55.277 回答
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>
于 2012-06-06T13:07:35.667 回答
0

您已经用两个 ss 拼写了 DB_Functions:

要求(“php/DB_Functionss.php”);

于 2012-06-06T13:11:45.353 回答
0

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

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

在jQuery中

于 2012-06-06T13:16:06.427 回答
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();
于 2012-06-06T13:18:57.153 回答
0

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

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

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;
            }
        }
于 2012-06-06T13:27:50.177 回答