views:

33

answers:

1
function menuName () 
{
    $this->viewData['page_title'] = "ContentManagement Systemt!";


    $this->db->where('visible', 1);
    $this->db->order_by("position", "ASC"); 
    $query = $this->db->get('subjects');
    $subjects = $query->result();
    foreach ($subjects as $subject)
    {
        echo $subject->menu_name ."<br />";

        $this->db->where('subject_id', $subject->id );
        $query = $this->db->get('pages');
        $pages = $query->result();

        foreach ($pages as $page)
        {
            echo $page->menu_name ."<br />";
        }        
    }

}

Why not working my query pls tell me anyone

A: 

You should always use foreach loops like the following:

$subjects = $query->result();
foreach ($subjects as $subject)

Your approach has a bad performance.

And maybe this already solves your problem - I couldn't test it right now . Your query variables are named equally and you're using $query in both of your foreach loops - this could maybe lead to some strange behaviour.

mseo
I have change as u told but not working yet pls help me
masud010
I have changed my variables $subjects and $pages
masud010
I have changed as u told me but till not working
masud010
One comment should be enough :) I can't see something wrong. Check your DB table. Or check your query. With echo $this->db->last_query(); you can see your last query. Is there a result if you use this query manually (for example in phpmyadmin)?
mseo
@mseo This is not accurate. There is no performance difference between using foreach($query->result().. and foreach($subjects... because PHP's foreach is implemented using iterators. I just did a performance test and found that the difference in time between the two fluctuates around zero...i.e. no difference.
treeface
mseo
Interesting link, but I don't see any information there on standard deviation of the run times, nor do I see that he performed the same tests on multiple computers and at multiple, random times. Given this, it's hard to draw too much definitively from the ~.8 second difference. I'm not saying it's definitely not faster to do it the way you say, but given the responses in the following thread, I'm inclined to wait for something more logically and empirically definitive: http://stackoverflow.com/questions/3908144/if-the-supplied-array-of-a-foreach-loop-is-a-function-call-is-there-a-performanc
treeface