php poo carrito

0

Hi, the following error message appears trying to make a shopping cart (which, by the way, if you have a way to help me by throwing out tips, I would appreciate it):

  

Notice: Undefined property: driverproducts :: $ code in   C: \ xampp \ htdocs \ driver.php on line 18

     

Fatal error: Call to a member function list () on null in   C: \ xampp \ htdocs \ driver.php on line 18

my codes:

connection

<?php

class conexion{

     //Atributos

    private $server;
    private $usuario;
    private $password;
    private $bd;


    //Metodo

    public function __construct(){
          $this->server = "localhost";
          $this->usuario = "root";
          $this->password = "";
          $this->bd = "carro_productos";

          $con = mysql_errno($this->server, $this->usuario, $this->password);
          if ($con) 
            mysql_select_db($this->bs, $con);


    }

   public function consultaSimple($sql){
    $consulta = mysql_query($sql);
   }    

      public function consultaRetorno($sql){

      }


   } 




?>

products

<?php

       include_once("conexion.php");
       include_once("carrito.php");
       include_once("controlador.php");
       include_once("enrutador.php");
       include_once("index.php");
       include_once("inicio.php");





       class productos{

        private $codigo;
        private $producto;
        private $descripcion;
        private $precio;

        private $con;

       }

?>

controller

<?php


       include_once("productos.php");





       class controladorproductos{
         private $producto;

         public function __construct(){
           $this->productos = new productos();   
         }

         public function index(){
            $resultado = $this->codigo->listar();
            return $resultado;
         }
         public function eliminar($codigo){
            $this->producto->set("codigo", $codigo);
            $this->producto->eliminar();
         }
         public function ver($codigo){
            $this->producto->set("codigo", $codigo);
            $this->producto->ver();
         }

       }
?>

router

<?php
include_once("conexion.php");
       include_once("carrito.php");
       include_once("controlador.php");
       include_once("enrutador.php");
       include_once("index.php");
       include_once("inicio.php");


class enrutador{
   public function vista($vista){
      switch ($vista):
         case 'añadir':
            include_once($vista . '.php');
            break;

        endswitch;

   }

  public function validarGET($var){
if(empty($var)){
   include_once('inicio.php');
}else{
   return true;
}
  } 


    }


?>

index

<?php
        include_once("conexion.php");
       include_once("carrito.php");
       include_once("controlador.php");
       include_once("enrutador.php");
       include_once("index.php");
       include_once("inicio.php");



?>

<!DOCTYPE html>
<html>
<head>
   <meta charset="UTF-8"/>
   <title>Carrito</title>
</head>
<body>
     <h1>
        Carrito
     </h1>

     <section>
     <?php
      $enrutador = new enrutador();
      if($enrutador->validarGET($_GET['añadir'])){
        $enrutador->vista($_GET['añadir']);
      }

      ?>      
     </section>
</body>
</html>

home

<?php 
       include_once("conexion.php");
       include_once("carrito.php");

       include_once("enrutador.php");
       include_once("index.php");
       include_once("inicio.php");


       include_once("controlador.php");

       $controlador = new controladorproductos();
       $resultado = $controlador->index();
?>
<h3>pagina</h3>
<table>
    <thead>
        <th>codigo</th>
        <th>producto</th>
        <th>descripción</th>
        <th>precio</th>
        <th>acción</th>
    </thead>
    <tbody>
    <?php while ($row = mysql_fetch_array($resultado)): ?>

        <tr>
            <td><?php echo $row['codigo']; ?></td>
            <td><?php echo $row['producto']; ?></td>
            <td><?php echo $row['descripción']; ?></td>
            <td><?php echo $row['precio']; ?></td>
        </tr>
    <?php endwhile; ?>
    </tbody>
</table>
    
asked by Luchoo Asspero 23.11.2016 в 09:44
source

2 answers

0

In addition to clarifying the visibility. Your controller does not have the property code. Code is a product property, so when you make $ result = $ this-> code-> list (); PHP is looking for the property code in your controller and not in the product class. It should be something like $ this- > product-> code.

On your controller you are defining private $ product; Notice that the s is missing at the end. (private $ products;)

To get the codes you should use a loop in that case:

foreach($this->products as $product) {
  echo $product->codigo;
}

But look what code is private. Either you do it public or use a getter

public function getCodigo() {
   return $this->codigo;
}
    
answered by 23.11.2016 в 10:11
0

the problem:

     class controladorproductos{
     private $producto;

     public function __construct(){
       $this->productos = new productos();   
     }

     public function index(){
        $resultado = $this->codigo->listar(); #<---------error
        return $resultado;
     }
     public function eliminar($codigo){
        $this->producto->set("codigo", $codigo);
        $this->producto->eliminar();
     }
     public function ver($codigo){
        $this->producto->set("codigo", $codigo);
        $this->producto->ver();
     }

   }

in the line that marks you are trying to access a code attribute in your controller object products which is not defined

  

$ this- > code

the interpreter tries to find that attribute in the object and since it is not defined in the class controllerproductos throws the error

  

Notice: Undefined property: driverproducts :: $ code in C: \ xampp \ htdocs \ driver.php on line 18

Now, besides that, you try to call the list () method

  

$ this- > code-> list ()

As a code is not defined (previous error) then what you are doing is something like: $this->null->listar() causing the second error.

  

Fatal error: Call to a member function list () on null in C: \ xampp \ htdocs \ driver.php on line 18

Now let's improve your driver class:

class Producto
{
    public $codigo;
    public $producto;
    public $descripcion;
    public $precio;

    private $con;

    public static function listar(filtros){
        //haces el código para crear una consulta con los filtros pasados
    }

    public static function remover(filtros){
        //haces la consulta para eliminar con los filtros
    }

    public static function obtener(filtros){

    }
}


class ControladorProductos //usa CamelCase para el nombre de las clases 
{

     public function __construct(){
       $this->productos = new productos();   
     }

     public function index($codigo=""){
        // usa métodos estáticos para acciones que no tengan que ver con un solo registro
        // listar es un ejemplo de esto usar lo siguiente:
        return Producto::listar(["codigo"=>$codigo])
     }
     public function eliminar($codigo){
        // para remover no necesitas hacer nada mas con el objeto asi que no vale
        // la pena almacenarlo en una variable
        return Producto::remover(['codigo'=>$codigo])
     }
     public function ver($codigo){
        return Producto::obtener(['codigo'=>$codigo])
     }

}

If you need a guide how to do, you can be guided by some mvc framework like yii2 or laravel. but for better I recommend you a book:

  

POO and MVC in PHP - Eugenia Bahit

If you are learning php, it is ideal for you. if you want to make a framework by hand it is ideal for you.

    
answered by 23.11.2016 в 10:28