0
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 is my query not working? Please tell me.

Smi
  • 13,850
  • 9
  • 56
  • 64
masud010
  • 1
  • 2
  • whats not working? no results? error? unexpected results? – Ross Oct 11 '10 at 16:01
  • Yes, please provide more details. Tell us what errors are coming up, for example. Or perhaps how your table is structured. It's difficult to help when you just show us code. – treeface Oct 11 '10 at 16:27
  • When i run the query then just comes subject menu_name but not come page menu_name. I mean 1st loop is working but 2nd loop is not working – masud010 Oct 11 '10 at 16:37
  • Again..not entirely following what you're talking about. Please see this from our perspective: we have no way of knowing what's going on in your code *unless you provide us with more details*. You haven't even shown us what your tables look like, if there is an error, or what the result looks like. – treeface Oct 11 '10 at 19:32

2 Answers2

0

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
  • 3,881
  • 3
  • 22
  • 26
  • I have changed my variables $subjects and $pages – masud010 Oct 11 '10 at 15:42
  • 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 Oct 11 '10 at 16:40
  • @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 Oct 11 '10 at 17:38
  • @treeface Okay, the performance isn't "bad" but there definitely is a performance difference: http://webcache.googleusercontent.com/search?q=cache:1BGU3Wb2FyYJ:www.haughin.com/2008/02/13/optimizing-and-scaling-your-codeigniter-application/+haughin+performance&cd=1&hl=de&ct=clnk&gl=de The site's not working right now, so here is a cached version of it. Anyhow ... I think it's a better approach not to mess with variable scopes if you can easily avoid it. – mseo Oct 11 '10 at 21:04
  • 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 Oct 11 '10 at 22:50
0

Why dont you echo the query like so:

echo $this->db->last_query()

To see what is going on with the results

Maybe the problem is there.