Query with WHERE statement in CodeIgniter does not work

2

I am trying to make simple queries to a MySQL database from CodeIgniter. For that I have a view called "Menu", here I will fill it with 3 categories: Meals (2), Drinks (1) and Desserts (3). In the database I have 2 tables, producto and categoria which I present below:

tabla producto

id, nombre, descripcion,precio,imagen,negocio_id,categoria_id

tabla categoria
id. nombre --Aquí va el nombre(Comidas, bebidas o postres)

In the MenuModel model I have these functions:

    public function getAllComidas($id=2)
{
    $this->db->select('*');
    $this->db->from(TABLE_PRODUCTO);
    $this->db->where('categoria_id',$id);
    $query = $this->db->get();
    return $query->result_array();

}

public function getAllBebidas($id=1)
{
    $this->db->select('*');
    $this->db->from(TABLE_PRODUCTO);
    $this->db->where('categoria_id',$id);
    $query = $this->db->get();
    return $query->result_array();

}

public function getAllPostres($id=3)
{
    $this->db->select('*');
    $this->db->from(TABLE_PRODUCTO);
    $this->db->where('categoria_id',$id);
    $query = $this->db->get();
    return $query->result_array();

}

In the controller I have the following:

 public function index()
 {  


$data['menu'] = $this->MenuModel->getAllComidas();
    $this->load->view('estaticos/header');          
    $this->load->view('dinamicos/menu/menus', $data);
    $this->load->view('estaticos/menu_izquierdo');
    $this->load->view('estaticos/footer');  
    $this->load->view('estaticos/menu_derecho');
}



public function getBebidas()
 {

$data['menuBebidas'] = $this->MenuModel->getAllBebidas();

    $this->load->view('estaticos/header');          
    $this->load->view('dinamicos/menu/menus', $data);
    $this->load->view('estaticos/menu_izquierdo');
    $this->load->view('estaticos/footer');  
    $this->load->view('estaticos/menu_derecho');

}



public function getPostres()
{

    $data['menuPostres'] = $this->MenuModel->getAllPostres();
    $this->load->view('estaticos/header');          
    $this->load->view('dinamicos/menu/menus', $data);
    $this->load->view('estaticos/menu_izquierdo');
    $this->load->view('estaticos/footer');  
    $this->load->view('estaticos/menu_derecho');


}

In the view I use this to show the data by calling only the food function:

 <?php
                foreach ($menu as $menu) {
                  echo '

            <div class="box-body">
              <ul class="products-list product-list-in-box">
                <li class="item">
                  <div class="product-img">


                    <img  src="'.base_url('assets/dist/img/default-50x50.gif').'" alt="Product Image">
                  </div>

                  <div class="product-info">

                    <a href="javascript::;" class="product-title">'.$menu['nombre'].'<span class="label label-warning pull-right">Editar</span></a>

                    <span class="product-description">'
                     .$menu['descripcion'].'
                    </span>
                     <span class="label label-danger pull-right">eliminar</span></a>
                     <a onclick="if(confirma() == false) return false" href="'.base_url().'MenusController/borrar_item/'.$menu['id'].'">Eliminar</a>
                  </div>

                </li><!-- /.item -->




              </ul>
            </div><!-- /.box-body -->';
                       }
              ?>

And if it works, it shows me the meals:

But if I go back to using the same code fragment and only changing the name of the function for example to:

foreach ($menu as $menuBebidas)

It shows me a repeated item and not all drinks or desserts depending on the ID:

I would appreciate it if you would tell me what I am wrong with. Thank you very much

    
asked by x4mp73r 22.03.2016 в 20:07
source

1 answer

1

Your problem is that in the index you are calling the data only from the function getallMeals ()

public function index(){
  $data['menu'] = $this->MenuModel->getAllComidas();
  $this->load->view('estaticos/header');          
  $this->load->view('dinamicos/menu/menus', $data);
  $this->load->view('estaticos/menu_izquierdo');
  $this->load->view('estaticos/footer');  
  $this->load->view('estaticos/menu_derecho');
}

For each menu you should call each function if you want to use them, for example:

public function index(){
  $data['menu'] = $this->MenuModel->getAllComidas();
  $data['menuBebidas'] = $this->MenuModel->getAllBebidas();
  $data['menuPostres'] = $this->MenuModel->getAllPostres();
  $this->load->view('estaticos/header');          
  $this->load->view('dinamicos/menu/menus', $data);
  $this->load->view('estaticos/menu_izquierdo');
  $this->load->view('estaticos/footer');  
  $this->load->view('estaticos/menu_derecho');
}

and then in the view you have to call them in a foreach for each menu:

foreach ($menu as $menu){
  //presentar datos
}

foreach ($menuBebidas as $menu){
  //presentar datos
}

foreach ($menuPostres as $menu){
  //presentar datos
}

Do not forget that codeigniter sends each element of the $ data array as a new variable, that is, to go through each menu you have to use those variables in foreach .

    
answered by 22.03.2016 / 23:30
source