[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>";
}
}
}
}
?>
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.