CodeIgniter Forums
Multiple Queries in view - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20)
+--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forumdisplay.php?fid=23)
+--- Thread: Multiple Queries in view (/showthread.php?tid=42763)



Multiple Queries in view - El Forum - 06-18-2011

[eluser]RichM[/eluser]
I have a pretty common scenario of a table that lists categories and a table that lists items. Of course, many items can be associated with one category.

controllers/equipment.php
Code:
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Equipment extends CI_Controller
{

    function index()
     {
      $this->load->model('Equipment_Model');
      $data['main_content'] = 'equipment_list';
      $data['title'] = "Equipment Administration";
      $data['equipment'] = $this->Equipment_Model->list_equipment();
      $this->load->view('template', $data);
     }    
    
}
I'm using a template view to load header, content, and footer...
views/template.php
Code:
<?php
$this->load->view('includes/header');
$this->load->view($main_content);
$this->load->view('includes/footer');
?>

model/equipment_model.php
Code:
<?php

class Equipment_Model extends CI_Model
{
  function __construct()
    {
        parent::__construct();
    }

  function list_equipment()
    {
      $this->load->database();
      $this->db->order_by("cid", "asc");
      $query = $this->db->get('ecat');
      foreach ($query->result() as $row)
      {
        echo "<h2>".$row->cname."</h2>";
        $query = $this->db->get_where('equipment', array('ecid' => $row->cid));
        foreach ($query->result() as $row2)
        {
          echo "<div>".$row2->eqid."</div>";
        }    
      }
    }
}
?&gt;

the view equipment_list.php actually has no code in it other than php tags. The output for each section works fine, but the information outputs before the inclusion of the header.php file so it's out of sequence. As I was going through this, being a complete noob to codeigniter, I started with outputting just the category names by passing the equipment_list function to the view and having the foreach() within the view - worked perfectly.

Sorry about that lengthy post - so the real question is - How to do this effectively and get it in the right place. I've looked through the user guide but can't find an example of running multiple queries as shown. Any help is greatly appreciated.


Multiple Queries in view - El Forum - 06-18-2011

[eluser]RichM[/eluser]
And yes, I do realize why it's outputting above the header and footer. Maybe I'll modify the question to - how would I pass the information to the equipment_list view?


Multiple Queries in view - El Forum - 06-18-2011

[eluser]Nick_MyShuitings[/eluser]
Do you see how in your call to the template you are passing a variable called $data? Which if your model had RETURNED the data instead of echoing it, you would then be able to access the variable $equipment (from $data['equipment']) in your view file.


Multiple Queries in view - El Forum - 06-18-2011

[eluser]RichM[/eluser]
In the first version of just outputting the categories, that's what I had. Passed the $category variable array to the view and output foreach ($category as $row) etc.etc. into the equipment_list view and it worked just fine.

With the layout of
Cat1
-Equip1-1
-Equip1-2

Cat 2
-Equip2-1
-Equip2-2

I don't know how to separate them out to be able to pass them to the view. It almost seems like I would have to call a function within the view which I've heard is bad practice. The function would be called based on what $row->catid would be.


Multiple Queries in view - El Forum - 06-18-2011

[eluser]Nick_MyShuitings[/eluser]
You could modify your model so that it outputs that structure natively. Or your could do some more nasty foreach in your view.

Ie your model could be instead of calling list_equipment you could call get_category_equipment_list which would loop through all the categories and call list_equipment for each one building the larger array. The ways to do this are endless.


Multiple Queries in view - El Forum - 06-18-2011

[eluser]RichM[/eluser]
This is my first go round with CI, so I would rather figure out how to stick to the conventions.

As I expand this to what I want I'll be pulling more information in this same manner. I appreciate your feedback and will give it a shot.


Multiple Queries in view - El Forum - 06-18-2011

[eluser]RichM[/eluser]
Ended up passing both queries to the view and comparing the category id of each one. Probably not the most elegant and definitely not efficient, but it's working. Until I figure out another way - I'll keep it as is.

view
Code:
foreach ($cat->result() as $cats)
  {
    echo "<h2>".$cats->cname."</h2>";
      foreach ($equipment->result() as $equip)
        {
          if($equip->ecid == $cats->cid)
            {
              echo $equip->eqid." - ".$equip->make." - ".$equip->model."<br />";  
            }  
        }
  }



Multiple Queries in view - El Forum - 06-18-2011

[eluser]jmadsen[/eluser]
have a look at this, rich:

http://ellislab.com/forums/viewthread/191669/