foreach nesting - 2 layers

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • JonB
    New Member
    • Mar 2008
    • 2

    foreach nesting - 2 layers

    The examples I'm generally finding on the web of how to do nested loops are confusing and extremely scarce. Here's what I'm trying to do...

    I have an array named $data that I believe qualifies as a multidimensiona l array:

    $data=array( ID, Title, Description, UpdateID, Update, UpdateDate)

    This data is from a query of a MySQL database that linked 2 tables so in reality I have:

    $data[0] = array(1, Job1, Clean up the room, 1, Waiting for sunshine, 2008-03-23)
    $data[1] = array(1, Job1, Clean up the room, 2, Still waiting, 2008-03-24)
    $data[2] = array(1, Job1, Clean up the room, 3, Looking better, 2008-03-25)
    $data[3] = array(2, Job2, Wash the car, 1, Kid has the car, 2008-03-21)
    $data[4] = array(2, Job2, Wash the car, 2, Car is home, 2008-03-24)
    $data[5] = array(2, Job2, Wash the car, 3, Car washed, 2008-03-25)

    So what I want to do is handle each of these data sets by doing a foreach on ID and then a foreach on each Update ID so that it ends up like:

    Item ID: 1 Title: Clean the room
    Update ID: 1 Update: Waiting for sunshine Updated: 2008-03-23
    Update ID: 2 Update: Still waiting Updated: 2008-03-24
    Update ID: 3 Update: Looking better Updated: 2008-03-25
    Item ID: 2 Title: Wash the car
    Update ID: 1 Update: Kid has the car Updated: 2008-03-21
    Update ID: 2 Update: Car is home Updated: 2008-03-24
    Update ID: 3 Update: Car washed Updated: 2008-03-25

    Can someone please comment on this syntax?

    $query = "SELECT ID, Title, Description, UpdateID, Update, UpdateDate FROM ...";

    $result = mysql_query($qu ery) or die('Error, query failed');

    While($data = mysql_fetch_ass oc($result))
    {

    foreach ($data as $ID=> $UpdateID){

    echo 'Item ID:'.$ID.' Title: '.$Title;

    foreach ($UpdateID as $UpdateID){

    echo 'Update ID: '.$UpdateID.' Update: '.$Update.' Updated: '.$UpdateDate;
    }
    }
    }

    Any assistance is appreciated.
  • hsriat
    Recognized Expert Top Contributor
    • Jan 2008
    • 1653

    #2
    foreach ($UpdateID as $UpdateID) won't work.
    Use something like foreach ($UpdateID as $_UpdateID)

    Comment

    • JonB
      New Member
      • Mar 2008
      • 2

      #3
      So aside from changing the $value in the inner foreach loop all looks okay?

      Comment

      • hsriat
        Recognized Expert Top Contributor
        • Jan 2008
        • 1653

        #4
        A multidimensiona l array is built like this:
        [php]$data = array(
        array("a","b"," c"), //$data[0]
        array("d","e"," f"), //$data[1]
        array("g","h"," i"), //$data[2]
        array("j","k"," l"), ); //$data[3][/php]

        If you will echo $data[0][2], it will display c

        Also include the strings in quotes.
        eg. "Clean up the room"

        And in future, when you post some code here, do include that in
        [php] [/php] tags or
        [code] [/code]
        If it were there, I would have found the problem easily.

        Regards,
        Harpreet

        Comment

        Working...