Welcome Guest, Not a member yet? Register   Sign In
Showing data in a loop, view, controller, model
#1

[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
#2

[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;
#3

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

[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!
#5

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

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

[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>';
    }
  }
}
#8

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




Theme © iAndrew 2016 - Forum software by © MyBB