Pass several parameters to a view in CodeIgniter 2

0

Good day community, I hope you can help me with this little problem that I have ... I just started with this framework and I have the following problem. It is a site in which I intend to show products and categories of a database ... the code of the view, model and controller I leave it below. Problem: What I want to do is in the products view send the data of the categories, products and also page, so you can see in my code I had to separate a view of categories and other products to achieve it temporarily, but at the time of wanting to put the product page I stumble again with the problem: / Expected result: use a single view of products that have categories, products and pages.

<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Principal_ci extends CI_Controller {
    public function index()
    {
    $this->load->model('categorias_mdl');
    $result = $this->categorias_mdl->getCategorias();
    $cat = array('categorias' => $result);

    $this->load->model('productos_mdl');
    $result = $this->productos_mdl->getProductos();
    $prod = array('productos' => $result);

    $this->load->view('header');
    $this->load->view('categorias', $cat);
    $this->load->view('principal', $prod);
    $this->load->view('footer');
    }
}

class Productos_mdl extends CI_Model {

    public function getProductos($limite=9)
    {
        return $this->db->get('producto', $limite);
    }       

}
// Fragmento de la vista de como saco los datos actualmente
<?php foreach ($categorias->result() as $categoria) { ?>
<a href="#" class="list-group-item"><?= $categoria->nom_cat ?></a>
<?php } ?>
    
asked by J. Martinez 10.07.2017 в 20:38
source

2 answers

0

To pass different values of the controller to the view, an associative array could be created in the following way.

Controller

public function index()
{
  $this->load->model('categorias_mdl');
  $categorias = $this->categorias_mdl->getCategorias();
  /* Agregamos la clave categorias */
  $datos['categorias'] = $categorias;
  $this->load->model('productos_mdl');
  $productos = $this->productos_mdl->getProductos();
  /* Agregamos la clave productos*/
  $datos['productos'] = $productos;
  $this->load->view('header');
  /* Pasamos los valores a la vista productos*/
  $this->load->view('productos', $datos);
  $this->load->view('footer');
}

In the Vista we would accede of the following form.

foreach ($productos as $value) { ...} 

foreach ($categorias as $value) { ...} 
  

As a recommendation if it is a class and possibly use the   models in several methods would be much better to load them into the constructor   the class.

    
answered by 10.07.2017 в 20:51
0

If you want to send several data to the view, you have to create an associative array and send it as a parameter to your view.

Your code would be like this:

class Principal_ci extends CI_Controller {
    public function index()
    {
    $this->load->model('categorias_mdl');
    $result1 = $this->categorias_mdl->getCategorias();
    $data['categorias'] = $result1;

    $this->load->model('productos_mdl');
    $result2 = $this->productos_mdl->getProductos();
    $data['productos'] = $result2;

    $this->load->view('header');
    $this->load->view('productos', $data); //envias todos los datos a la misma vista.
    $this->load->view('footer');
    }
}

Once this is done, you can access the values that you send to your view with the key of the associative arrangement that was created, to use the values of products you use $ products, and for categories you use $ categories.

    
answered by 10.07.2017 в 20:51