Shopping cart

0

Hello, I ask another question because I have other doubts, I do not know if it's okay.

Well, I propose what I have to do first. An object-oriented shopping cart (POO) that has basic operations such as adding product to cart to the aldo of each one, printing the objects added to a cart, see cart etc. I re-issued a code and it's almost over (I think) just that I miss some mistakes that I do not know how to solve when I want to add a cart and see the cart

codigo:  int, clave primaria
producto: varchar(30)
descripción: varchar(255)
precio: decimal (5,2)

  

Warning: Missing argument 2 for Product :: enter_product (), called   in C: \ xampp \ htdocs \ shopping.php on line 16 and defined in   C: \ xampp \ htdocs \ classes \ Product.php on line 62

     

Warning: Missing argument 3 for Product :: enter_product (), called   in C: \ xampp \ htdocs \ shopping.php on line 16 and defined in   C: \ xampp \ htdocs \ classes \ Product.php on line 62

     

Notice: Undefined variable: product in   C: \ xampp \ htdocs \ classes \ Product.php on line 64

     

Notice: Undefined variable: price in   C: \ xampp \ htdocs \ classes \ Product.php on line 65   Y   Notice: Undefined variable: Product in   C: \ xampp \ htdocs \ ver_carrito.php on line 25

     

Fatal error: Call to a member function imprime_carrito () on null in   C: \ xampp \ htdocs \ ver_carrito.php on line 25

here are my codes so I do not do so long the post

home

<?php 
session_start();
if (!isset($_SESSION['usuario'])){

}
            ?>
        <!DOCTYPE html>
    <html lang="en">
<head>
<title>Carrito de Compras</title>
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
<link rel="stylesheet" type="text/css" href="bootstrap/css/bootstrap.min.css">
<link href="http://fonts.googleapis.com/css?family=Asap:400,700" rel="stylesheet" type="text/css">
</head>
<body>
<div id="wrap">  <div id="main"> 
<div class="container">
<nav class="navbar navbar-inverse" role="navigation">
<div class="container-fluid">
    <div class="navbar-header">
        <button type="button" class="navbar-toggle" data-toggle="collapse" data-target="#acolapsar">
            <span class="sr-only">Toggle navigation</span>
            <span class="icon-bar"></span>
            <span class="icon-bar"></span> 
            <span class="icon-bar"></span>
            <span class="icon-bar"></span>
        </button>
        <a href="./" class="navbar-brand">ABM Productos</a>

    </div>

</div>
</nav>

<?php


require 'autoload.php';

$base = new BasedeDatosmysqli(SERVIDOR,USUARIO,PASSWORD,BASE);
$producto = new Producto($base);

$productoOK = $producto->getProductos();

if ($productoOK){
    echo '<table class="table table-striped">
            <tr class="active">
                <td>Código</td>
                <td>Producto</td>
                <td>Descripcion</td>
                <td>Precio</td>
                <td colspan="2">Operaciones</td>
            </tr>';
    for ($i=0;$i<count($productoOK);$i++){
        echo '<tr>
                <td>'.$productoOK[$i]['codigo'].'</td>
                <td>'.$productoOK[$i]['producto'].'</td>
                <td>'.$productoOK[$i]['descripcion'].'</td>
                <td>'.$productoOK[$i]['precio'].'</td>

                <td><a href="carrito.php?codigo='.$productoOK[$i]['codigo'].'><button type="button" class="btn btn-info">Añadir a carrito</button></a></td>
            </tr>';

    }
    echo '</table>';
}
else{
    echo 'Aún no hay datos ingresados';
}


        ?>
    </div>
</div> 
<div id="push"></div>
</div> 
<footer><div class="container">Este es el pie de página</div></footer>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<script src="bootstrap/js/bootstrap.min.js"></script>
</body>
</html>

producto.php

<?php
class Producto {
public $db;

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

public function __destruct() {
    $this->db = null;
}



public function getProductos(){
    //echo $this->db->error; 
    $respuesta = $this->db->enviarQuery("select * from productos");
    if (!$respuesta and $this->db->error!=''){
        echo $this->db->error; 
        return false;
    }
    else{
        if (!$respuesta){
            return false;
        }
        else {
            return $respuesta;
        }
    }
}

public function getProducto($codigo){
    //echo $this->db->error; 
    $respuesta = $this->db->enviarQuery("select * from productos where codigo = $codigo");
    if (!$respuesta and $this->db->error!=''){
        echo $this->db->error; 
        return false;
    }
    else{
        if (!$respuesta){
            return false;
        }
        else {
            return $respuesta;
        }
    }
}

function carrito () {
    $this->cantidad=0;
}


function introduce_producto($codigo,$producto,$precio){
    $this->codigo[$this->cantidad]=$codigo;
    $this->producto[$this->cantidad]=$producto;
    $this->precio[$this->cantidad]=$precio;

    $this->cantidad++;
}


function imprime_carrito(){
    $suma = 0;
    echo '<table border=1 cellpadding="3">
          <tr>
            <td><b>Nombre producto</b></td>
            <td><b>Precio</b></td>
            <td>&nbsp;</td>
          </tr>';
    for ($i=0;$i<$this->cantidad;$i++){
        if($this->codigo[$i]!=0){
            echo '<tr>';
            echo "<td>" . $this->nombre[$i] . "</td>";
            echo "<td>" . $this->precio[$i] . "</td>";
            echo "<td><a href='eliminar_producto.php?linea=$i'>Eliminar producto</td>";
            echo '</tr>';
            $suma += $this->precio[$i];
        }
    }
    //muestro el total
    echo "<tr><td><b>TOTAL:</b></td><td> <b>$suma</b></td><td>&nbsp;</td></tr>";
    //total más IVA
    echo "<tr><td><b>IVA (16%):</b></td><td> <b>" . $suma * 1.16 . "</b></td><td>&nbsp;</td></tr>";
    echo "</table>";
}


function elimina_producto($linea){
    $this->codigo[$linea]=0;
}
}




?>

cart.php

<?php
session_start();
if (!isset($_SESSION['usuario'])){

}

require 'autoload.php';

$base = new BasedeDatosmysqli(SERVIDOR,USUARIO,PASSWORD,BASE);
$producto = new Producto($base);


    $producto->introduce_producto('precio' . 'producto' . 'codigo');
?>

<html>
<head>
<title>Introduce Producto</title>
</head>

<body>

Producto introducido.
<br>
<br>
<a href="index.php">- Volver</a>
<br>
<br>
<a href="ver_carrito.php">- Ver carrito</a>

</body>
</html>

ver_carrito.php

<?php
    session_start();
if (!isset($_SESSION['usuario'])){

}

require 'autoload.php';

$base = new BasedeDatosmysqli(SERVIDOR,USUARIO,PASSWORD,BASE);
$producto = new Producto($base);

$Producto->imprime_carrito($_GET['codigo']);

?>


<html>
<head>
<title>Introduce Producto</title>
</head>

<body>




<br>
<br>
<a href="index.php">Volver</a>


</body>
</html>

codes

    
asked by Luchoo Asspero 23.11.2016 в 15:31
source

1 answer

2

Man the arguments are separated by "," (commas) not by points, look at the line where it says:

$producto->introduce_producto('precio' . 'producto' . 'codigo');

It's very logical, the php interpreter interprets something like:

'precio''producto''codigo' // en un solo argumento

Then it would be something like:

$producto->introduce_producto('precio', 'producto', 'codigo');

Then here:

$this->db->error;  // por que haces esto? llamar una propiedad dentro de una propiedad que no existe.

same here:

$this->db->enviarQuery("select * from productos");

Then here:

$Producto->imprime_carrito($_GET['codigo']); // le asignas un argumento al método 'imprime_carrito();' el cual no se le ingresa ningún argumento.

These are some of the mistakes I have discovered.

    
answered by 23.11.2016 в 22:40