Jump to content

retrieving results of queries within nested loops with OOP


silphium

Recommended Posts

Ok, that's an ugly subject. I don't know what else to call this. I'm doing a fairly simple app for an online business student contest. All of the contest data is in MySQL: entries, prizes categories, etc. On one page I need to display past winners for each of four prize categories. A category can have more than one winner. I first run a query to obtain the name of each prize category. No problem displaying these:

 

foreach ($prizes as $pr)
{
echo '<pre>', $pr->name ,'</pre>';
}

 

I then do a foreach loop to iterate through each category, and run a query ($projects) to find the winning entries for each category within a given year. Again, no problem -- all the queries are being run correctly.

 

But I can't seem to retrieve and display the results of these $projects queries. If I do this:

 

foreach ($prizes as $pr)
{
echo '<pre>', $pr->name ,'</pre>';

    foreach($projects as $p)
   {
       echo '<pre>', $p->project_title ,'</pre>';
   }
}

All I get is the same title for all prize categories -- from the last query. This seems like an issue of how to correctly iterate through the $projects queries and assign the results of each with the correct prize category. I used to do this sort of thing a lot when I did procedural PHP with PHP 4. But this is OOP in PHP 5, and I'm using a framework (Kohana). I know these are objects, maybe even iterator objects, and not the kinds of arrays I used to deal with. But I can't get my head around how to work with them in a situation like this. I'm sure that there's a simple answer. Can anyone prod me in the right direction? Thanks.

 

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.