[eluser]antonagestam[/eluser]
Allright, this example is with vehicles. The vehicles can have different types (cars,boats etc.) and these types are stored in one table. The vehicles themselves are stored in another database where I give them a label (ferrari, audi, etc.).
This is the model and the most interesting part:
Code:
<?php
class Vehicles extends Model
{
public function Get_all()
{
// get the vehicle types
$query = $this->db->get('vehicles_types');
$result = $query->result_array();
// loop through the types e.g. the parents
foreach( $result as $key => $row )
{
// add the "examples" e.g. the children to the result array
$query = $this->db->get_where('vehicles',array('type'=>$row['id']));
$row['examples'] = $query->result_array();
$result[$key] = $row;
}
return $result;
}
}
This is the view:
Code:
<h1>Vehicles</h1>
<?php
//print_r($vehicles_types);exit;
foreach( $vehicles_types as $vehicle_type )
{
echo "<h2>" . $vehicle_type['label'] . "</h2>";
echo "<ul>";
foreach( $vehicle_type['examples'] as $example )
{
echo "<li>" . $example['label'] . "</li>";
}
echo "</ul>";
}
?>
And here is my controller:
Code:
<?php
class Nested extends Controller
{
public function Index()
{
$this->load->model('vehicles');
$data = array(
'vehicles_types' => $this->vehicles->get_all(),
);
$this->load->view('vehicles',$data);
}
}
And this gives me this output:
Code:
<h1>Vehicles</h1>
<h2>cars</h2>
<ul>
<li>Ferrari</li>
<li>Audi</li>
</ul>
<h2>boats</h2>
<ul>
<li>Yachts</li>
<li>Oil tanks</li>
</ul>