Results 1 to 5 of 5

Thread: How to use a foreach with a while loop

  1. #1
    Join Date
    Sep 2007
    Location
    Maui
    Posts
    642
    Thanks
    284
    Thanked 15 Times in 15 Posts

    Default How to use a foreach with a while loop

    I thought this would be fairly easy but I am having trouble.

    I have a list of 200 member names. The members can belong to more than one category. There is a member table, a category table, and a membercat table that has 2 fields: member_id and cat_id.

    I would like to list the members grouped by category with a subheading every time the category changes.

    The SQL command I tried to join the 3 tables produced 66,220 results (!). So I am having trouble with that. Or do I do the SQL command on just 2 tables inside the while loop? I'd like to learn the best way to do it. I read the php manual but...

    I figure there will be a foreach loop for the categories and a while loop inside to list the members (?). Is it something like this? Thanks for any help.

    Code:
    $sql = "SELECT * FROM category";
    $resultcat = mysql_query($sql,$connection) or die("Couldn't execute $sql query. <br> mysql error: ".mysql_error());
    
    foreach ($cat_id as $category){
      while($member = mysql_fetch_assoc($result2)){ 
        echo $member['company'];
      }
    }

  2. #2
    Join Date
    Mar 2006
    Location
    Illinois, USA
    Posts
    12,164
    Thanks
    265
    Thanked 690 Times in 678 Posts

    Default

    Can you post some sample output of what you'd like to get from the database as a final result?
    I understand the structure of the DB but not the goal.

    The foreach(while()) setup above looks a little complex. The approach might not be wrong, but I think the easier way to go would be to use a while inside a while:
    Code:
    while ($result1 = mysql_fetch_assoc($query1)) {
    while ($result2 = mysql_fetch_assoc($query2)) {
    //do stuff here
    }
    }
    As is the foreach loop is operating based on an array that does not exist-- you could always run through the results first (in a while loop) and store them to an array THEN do foreach(while()), but I don't see an advantage and it would be more code.
    You can have more than one mysql query at the same time, as long as they are working within different variables.
    Daniel - Freelance Web Design | <?php?> | <html>| español | Deutsch | italiano | português | català | un peu de français | some knowledge of several other languages: I can sometimes help translate here on DD | Linguistics Forum

  3. The Following User Says Thank You to djr33 For This Useful Post:

    kuau (04-02-2010)

  4. #3
    Join Date
    Sep 2007
    Location
    Maui
    Posts
    642
    Thanks
    284
    Thanked 15 Times in 15 Posts

    Default

    Hi Daniel!

    The easiest way to show you what I am trying to do is show you what it is at the moment. When I list all the members at once, the list looks like this:

    http://www.mauiweddingclub.com/php/all-members.php

    What would be more useful is a list grouped by categories such as this:

    Photographers
    Aloha Images
    Joe's Photography
    Maui Wedding Memories

    Musicians
    Maui Underground
    Elite Productions

    Flowers
    Paia Flower Shop
    Nui's Flowers

    etc etc. I was able to do it in a situation where it was a one-to-one relationship between the member and the category, but as soon as the 3rd table was thrown in, I lost my grip. Thanks so much for lending your mastery once again. e
    Last edited by kuau; 04-02-2010 at 04:49 PM. Reason: formatting

  5. #4
    Join Date
    Sep 2007
    Location
    Maui
    Posts
    642
    Thanks
    284
    Thanked 15 Times in 15 Posts

    Default

    Dear Daniel:

    I got it to work using your 2 while loops. Thanks a million!!! e

    PS. I don't know how to change the status to "Resolved."
    Last edited by kuau; 04-02-2010 at 06:38 PM.

  6. #5
    Join Date
    Mar 2006
    Location
    Illinois, USA
    Posts
    12,164
    Thanks
    265
    Thanked 690 Times in 678 Posts

    Default

    You should be able to edit the original post and choose 'resolved' from the dropdown menu near the title. Then just save that post.


    Glad it worked out.


    By the way, your idea of a foreach loop is basically right except that in the case of mysql, a while loop is essentially a foreach loop.
    Think of it this way: "continue while my mysql query keeps returning new values", which is basically like saying "go through each value from the query" or "for each query value...."
    Daniel - Freelance Web Design | <?php?> | <html>| español | Deutsch | italiano | português | català | un peu de français | some knowledge of several other languages: I can sometimes help translate here on DD | Linguistics Forum

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •