CodeIgniter Forums
Showing data in a loop, view, controller, model - 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: Showing data in a loop, view, controller, model (/showthread.php?tid=43812)



Showing data in a loop, view, controller, model - El Forum - 07-24-2011

[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:

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 />
&lt;?php echo "Brand(name) : $brand->name" ?&gt;
<br />
&lt;?php endforeach; ?&gt;

MODELS:
&lt;?php foreach($models as $model): ?&gt;
&lt;?php echo "Model(id) : $model->id" ?&gt;
<br />
&lt;?php echo "Model(name) : $model->name" ?&gt;
<br />
&lt;?php endforeach; ?&gt;

Any help will be great =) Thanks for your time


Showing data in a loop, view, controller, model - El Forum - 07-24-2011

[eluser]SitesByJoe[/eluser]
You need to do something like this:

Code:
&lt;?php
foreach ($brands->result() as $brand)
{
    echo $brand->name;
    foreach ($models->result() as $model)
    {
        if ($model->brand_id == $brand->id)
        {
            echo $model->name;
        }
    }
}
?&gt;



Showing data in a loop, view, controller, model - El Forum - 07-24-2011

[eluser]SitesByJoe[/eluser]
And you don't need the result() portion of my snippet.


Showing data in a loop, view, controller, model - El Forum - 07-24-2011

[eluser]blitzneo[/eluser]
Code:
&lt;?php
foreach ($brands as $brand)
{
    echo $brand->name;
    foreach ($models as $model)
    {
        if ($model->brand_id == $brand->id)
        {
            echo $model->name;
        }
    }
}
?&gt;
Thanks for your reply. I tried this code but it says, for the 2nd foreach loop:
"Invalid argument supplied for foreach()"

It cannot be merged like you commented or am I missing something ? Thanks!


Showing data in a loop, view, controller, model - El Forum - 07-24-2011

[eluser]shailendra[/eluser]
Please check this link
http://ellislab.com/forums/viewthread/193763/#916973


Showing data in a loop, view, controller, model - El Forum - 07-25-2011

[eluser]blitzneo[/eluser]
Thanks a lot, I am having a look at it =)


Showing data in a loop, view, controller, model - El Forum - 07-25-2011

[eluser]SitesByJoe[/eluser]
Try this code:

Code:
$brands = $this->db->get('brands');
$models = $this->db->get('models');

foreach ($brands->result() as $brand)
{
  echo '<b>' . $brand->name . '</b><br>';
  foreach ($models->result() as $model)
  {
    if ($brand->id == $model->brand_id)
    {
      echo $model->name . '<br>';
    }
  }
}



Showing data in a loop, view, controller, model - El Forum - 07-26-2011

[eluser]blitzneo[/eluser]
Very interesting post =)
I will be using Joe's suggestion for now. I will check performance in a future 'upgrade' of the webapp.
Thanks guys.