07-24-2011, 01:14 PM
[eluser]blitzneo[/eluser]
I have a simple database, with 2 tables. One for brand, the other for model, having cars as examples.
In my model I have 2 functions, one to get all brands, and one to get all models from a brand. This way:
Both working perfectly, so my question is, how can I get in a view something like
brand-1:
- model1
- model2
- model3
brand-2:
- model1
- model2
etc. Sorry if it is a too easy question, but I am just trying to learn. I am using a foreach loop btw. This is the view.
Any help will be great =) Thanks for your time
I have a simple database, with 2 tables. One for brand, the other for model, having cars as examples.
In my model I have 2 functions, one to get all brands, and one to get all models from a brand. This way:
Code:
public function get_brands()
{
$data = array();
$query = $this->db->get('brand');
if ($query->num_rows > 0){
$data = $query->result();
}
$query->free_result();
return $data;
}
public function get_models($id)
{
$data = array();
$query = $this->db->where('brand_id', $id)->get('model');
if ($query->num_rows > 0){
$data = $query->result();
}
$query->free_result();
return $data;
}
Both working perfectly, so my question is, how can I get in a view something like
brand-1:
- model1
- model2
- model3
brand-2:
- model1
- model2
etc. Sorry if it is a too easy question, but I am just trying to learn. I am using a foreach loop btw. This is the view.
Code:
BRANDS:
<?php foreach($brands as $brand): ?>
<?php echo "Brand(id) : $brand->id" ?>
<br />
<?php echo "Brand(name) : $brand->name" ?>
<br />
<?php endforeach; ?>
MODELS:
<?php foreach($models as $model): ?>
<?php echo "Model(id) : $model->id" ?>
<br />
<?php echo "Model(name) : $model->name" ?>
<br />
<?php endforeach; ?>
Any help will be great =) Thanks for your time