PHP Nested Loops of Arrays

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • blyxx86
    Contributor
    • Nov 2006
    • 258

    PHP Nested Loops of Arrays

    Happy Christmas Eve everyone,

    I've been looking around for the past few hours and my searches return useful information, but not with the same effect in mind as what I am trying to achieve.

    Essentially I am trying to create some form that is pulled from a MySQL database using two queries that I pull into their result sets and then close the connection and then display information in the first query on the top, and then the related information of the second query listed underneath it in an unordered list (or table element).

    For example:
    00100 | ABC | Call | Joe | Open
    • NOTE 1
    • NOTE 2

    00101 | DEF | Call | Joe | Closed

    What I have now is...
    Code:
    while($row = good_assoc($result))
    {
    	echo "{$row['Service_id']}" . " | " . "{$row['ReferenceNumber']}" . " | " . "{$row['TypeName']}" . " | " . "{$row['CurTech']}" . " | " . "{$row['CurStatus']}<br />\n";
    
    	
    	while($row2 = good_assoc($result2))
    	{	
    		if(in_array($row['Service_id'],$row2))
    		{
    			echo "<ul>\n";
    			if($row['Service_id']==$row2['Service_id'])
    			{
    				echo "<li>{$row2['Service_id']} | {$row2['FirstName']} | {$row2['CreatedDate']} | {$row2['Note']}</li>\n";
    			}
    			echo "</ul>\n";
    			
    		}
    	}
    	
    	mysql_data_seek($result2,0);
    }
    This works, except it produces an unordered list for every note beneath it's related record, or an empty unordered list if the related record has no notes.

    Can someone help me out with this problem? The current method works, but it's not right.
  • pbmods
    Recognized Expert Expert
    • Apr 2007
    • 5821

    #2
    Heya, blyxx86.

    The way I'd approach this is to set up my array first and then worry about echoing it out.

    I'd do something like this:

    [code=php]
    $index = array();
    while( $row = good_assoc($res ult) )
    {
    $index[$row['Service_id']] = array('data' => $row, 'notes' => array());
    }

    while( $row = good_assoc($res ult2) )
    {
    if( isset($index[$row2['Service_id']]) )
    {
    $index[$row2['Service_id']]['notes'][] = $row;
    }
    }

    foreach( $index as $id => $call )
    {
    // $call['data'] is the data from $result.
    // $call['notes'] is an array of corresponding data from $result2.
    }
    [/code]

    Comment

    • blyxx86
      Contributor
      • Nov 2006
      • 258

      #3
      Sorry it took so long to reply. Happy Holidays by the way!

      I'm still new to arrays. I'm starting to understand them better and I've started to delve into class structures as well.

      I'm confused though. I suppose I could store them all to an array FIRST and then mess with the formatting later. However, I am confused with all the brackets you have setup here. Specifically line 4 in your code. I see you setup the $index as an array... I'm confused about the right side I guess. Could you break it down for me what that function is doing. I see you are defining an empty array for the notes (if any) but does 'data' store all the information from $row, or just one piece of data?

      Code:
      $foo = array('bar' => 'baz');
      echo "Hello {$foo['bar']}!"; // Hello baz!
      Thank you again phpmods for your help!!

      Comment

      • blyxx86
        Contributor
        • Nov 2006
        • 258

        #4
        Ok...

        I have been able to toy with this for a little while and have figured out most of it.

        I had to make some minor changes to the code you posted. $row instead of $row2 when storing the notes.

        However, I am having problems accessing the array within the array.

        Code:
        print_r($index)
        /*
        	Array
        (
            [0000000007] => Array
                (
                    [data] => Array
                        (
                            [Service_id] => 0000000007
                            [ReferenceNumber] => REFERENCE
                            [PartName] => TEST PART1
                            [CompanyName] => TEST CO
                            [LocationCode] => FNE1001
                            [Quantity] => 1
                            [Serial] => SERIAL001
                            [Asset] => ASSET001
                            [TypeName] => Abuse
                            [CurStatus] => Open
                            [CurTech] => David
                        )
         
                    [notes] => Array
                        (
                            [0] => Array
                                (
                                    [Service_id] => 0000000007
                                    [Note] => Unit is broken?  This is a test call.
                                    [CreatedDate] => 2008-12-19 15:18:43
                                    [FirstName] => Kyle
                                )
         
                            [1] => Array
                                (
                                    [Service_id] => 0000000007
                                    [Note] => Test note Number 2
                                    [CreatedDate] => 2008-12-19 15:20:16
                                    [FirstName] => Kyle
                                )
         
                        )
         
                )
        )
        */
        Last edited by blyxx86; Dec 29 '08, 05:41 PM. Reason: Forgot ending code tag.

        Comment

        • Dormilich
          Recognized Expert Expert
          • Aug 2008
          • 8694

          #5
          example from above
          Code:
          $index["0000000007"]["data"]["TypeName"] = "Abuse";
          // or
          $index["0000000007"]["data"] // that's the data-array

          Comment

          • blyxx86
            Contributor
            • Nov 2006
            • 258

            #6
            I was able to get it working using the following, but I'm still a little unsure of the arrays within arrays. If someone could help me out with understanding them better, that would be greatly appreciated. I commented out a line that I need to perform the function, but cannot think of a way to do it other than the way I did.

            Code:
            $index = array();
            while($row = good_assoc($result))
            {
            	$index[$row['Service_id']] = array('data'=>$row, 'notes' => array());
            }
            
            while($row=good_assoc($result2))
            {
            	if(isset($index[$row['Service_id']]))
            	{
            		$index[$row['Service_id']]['notes'][]=$row;
            	}
            }
            foreach ($index as $id => $call)
            {
            	echo "{$call['data']['Service_id']} | {$call['data']['ReferenceNumber']} | {$call['data']['TypeName']} | {$call['data']['CurTech']} | {$call['data']['CurStatus']}<br />\n";
            	if(isset($call['notes'][0])) // Need help here
            	{
            		echo "<ul>\n";
            		foreach($call['notes'] as $call)
            		{
            			echo "<li>{$call['FirstName']} | {$call['CreatedDate']} | {$call['Note']}</li>\n";
            		}
            		echo "</ul>\n";
            	}
            }

            Comment

            Working...