Welcome Guest, Not a member yet? Register   Sign In
Nested queries (I think that is the term?)
#11

[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>
&lt;?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>";
        
    }
?&gt;

And here is my controller:

Code:
&lt;?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>
#12

[eluser]überfuzz[/eluser]
I don't like to spit out queries like that. I always do one query. If there is hierarchy I have a column called parent.

Parent = 0 --&gt; first level
Parent > 0 --&gt; pointing out parent, can be level two or infinite.

Now you've got a nice array to play with. Isn't that nice?
#13

[eluser]Alex Kendrick[/eluser]
Thanks for the clearly written example, antonagestam. I'm sure this will help me the next time I want to do something similar.
#14

[eluser]Thorpe Obazee[/eluser]
off-topic. @m4rw3r. You just lost post count:
Quote:Total Posts: 0
Joined 08-03-2006

EDIT: it seems to affect a lot of people.
#15

[eluser]überfuzz[/eluser]
[quote author="bargainph" date="1259824227"]off-topic. @m4rw3r. You just lost post count:
Quote:Total Posts: 0
Joined 08-03-2006

EDIT: it seems to affect a lot of people.[/quote]
Well either you're fresh out of counts or you just got a fresh start.
#16

[eluser]mattpointblank[/eluser]
When I do this, I join the parent table to the child one, so each row has a parent_id and/or parent_name column. I then order the query by parent_id, and in my view, do something like this:

Code:
$last_parent_id = '';
foreach($row as $r)
{
    if($r['parent_id'] != $last_parent_id) {
       echo '<h1>' . $r['parent_name'] . '</h1>';
    }

    echo $row['name_of_child_item'];

    $last_parent_id = $r['parent_id'];
}

It's not the cleanest of code, and gets trickier if you want to do things like put the subitems in a list, but it can be done.
#17

[eluser]antonagestam[/eluser]
[quote author="überfuzz" date="1259812074"]I don't like to spit out queries like that. I always do one query. If there is hierarchy I have a column called parent.

Parent = 0 --&gt; first level
Parent > 0 --&gt; pointing out parent, can be level two or infinite.

Now you've got a nice array to play with. Isn't that nice?[/quote]

Well that's not really what we're asking for here, I think. How would you do that if you want store, say, cities and areas. You want to store how many citizens the cities' got but on the areas you want to write a description. If you only use one table for that you'll end up with a lot of empty fields!
#18

[eluser]überfuzz[/eluser]
Well, ever heard about join?
#19

[eluser]antonagestam[/eluser]
[quote author="überfuzz" date="1259899041"]Well, ever heard about join?[/quote]

But join doesn't work if the tables has different columns, does it?




Theme © iAndrew 2016 - Forum software by © MyBB