473,326 Members | 2,168 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,326 software developers and data experts.

PHP Nested Loops of Arrays

blyxx86
256 100+
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...
Expand|Select|Wrap|Line Numbers
  1. while($row = good_assoc($result))
  2. {
  3.     echo "{$row['Service_id']}" . " | " . "{$row['ReferenceNumber']}" . " | " . "{$row['TypeName']}" . " | " . "{$row['CurTech']}" . " | " . "{$row['CurStatus']}<br />\n";
  4.  
  5.  
  6.     while($row2 = good_assoc($result2))
  7.     {    
  8.         if(in_array($row['Service_id'],$row2))
  9.         {
  10.             echo "<ul>\n";
  11.             if($row['Service_id']==$row2['Service_id'])
  12.             {
  13.                 echo "<li>{$row2['Service_id']} | {$row2['FirstName']} | {$row2['CreatedDate']} | {$row2['Note']}</li>\n";
  14.             }
  15.             echo "</ul>\n";
  16.  
  17.         }
  18.     }
  19.  
  20.     mysql_data_seek($result2,0);
  21. }
  22.  
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.
Dec 24 '08 #1
5 2782
pbmods
5,821 Expert 4TB
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:

Expand|Select|Wrap|Line Numbers
  1. $index = array();
  2. while( $row = good_assoc($result) )
  3. {
  4.   $index[$row['Service_id']] = array('data' => $row, 'notes' => array());
  5. }
  6.  
  7. while( $row = good_assoc($result2) )
  8. {
  9.   if( isset($index[$row2['Service_id']]) )
  10.   {
  11.     $index[$row2['Service_id']]['notes'][] = $row;
  12.   }
  13. }
  14.  
  15. foreach( $index as $id => $call )
  16. {
  17.     // $call['data'] is the data from $result.
  18.     // $call['notes'] is an array of corresponding data from $result2.
  19. }
  20.  
Dec 24 '08 #2
blyxx86
256 100+
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?

Expand|Select|Wrap|Line Numbers
  1. $foo = array('bar' => 'baz');
  2. echo "Hello {$foo['bar']}!"; // Hello baz!
  3.  
Thank you again phpmods for your help!!
Dec 29 '08 #3
blyxx86
256 100+
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.

Expand|Select|Wrap|Line Numbers
  1. print_r($index)
  2. /*
  3.     Array
  4. (
  5.     [0000000007] => Array
  6.         (
  7.             [data] => Array
  8.                 (
  9.                     [Service_id] => 0000000007
  10.                     [ReferenceNumber] => REFERENCE
  11.                     [PartName] => TEST PART1
  12.                     [CompanyName] => TEST CO
  13.                     [LocationCode] => FNE1001
  14.                     [Quantity] => 1
  15.                     [Serial] => SERIAL001
  16.                     [Asset] => ASSET001
  17.                     [TypeName] => Abuse
  18.                     [CurStatus] => Open
  19.                     [CurTech] => David
  20.                 )
  21.  
  22.             [notes] => Array
  23.                 (
  24.                     [0] => Array
  25.                         (
  26.                             [Service_id] => 0000000007
  27.                             [Note] => Unit is broken?  This is a test call.
  28.                             [CreatedDate] => 2008-12-19 15:18:43
  29.                             [FirstName] => Kyle
  30.                         )
  31.  
  32.                     [1] => Array
  33.                         (
  34.                             [Service_id] => 0000000007
  35.                             [Note] => Test note Number 2
  36.                             [CreatedDate] => 2008-12-19 15:20:16
  37.                             [FirstName] => Kyle
  38.                         )
  39.  
  40.                 )
  41.  
  42.         )
  43. )
  44. */
  45.  
Dec 29 '08 #4
Dormilich
8,658 Expert Mod 8TB
example from above
Expand|Select|Wrap|Line Numbers
  1. $index["0000000007"]["data"]["TypeName"] = "Abuse";
  2. // or
  3. $index["0000000007"]["data"] // that's the data-array
Dec 29 '08 #5
blyxx86
256 100+
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.

Expand|Select|Wrap|Line Numbers
  1. $index = array();
  2. while($row = good_assoc($result))
  3. {
  4.     $index[$row['Service_id']] = array('data'=>$row, 'notes' => array());
  5. }
  6.  
  7. while($row=good_assoc($result2))
  8. {
  9.     if(isset($index[$row['Service_id']]))
  10.     {
  11.         $index[$row['Service_id']]['notes'][]=$row;
  12.     }
  13. }
  14. foreach ($index as $id => $call)
  15. {
  16.     echo "{$call['data']['Service_id']} | {$call['data']['ReferenceNumber']} | {$call['data']['TypeName']} | {$call['data']['CurTech']} | {$call['data']['CurStatus']}<br />\n";
  17.     if(isset($call['notes'][0])) // Need help here
  18.     {
  19.         echo "<ul>\n";
  20.         foreach($call['notes'] as $call)
  21.         {
  22.             echo "<li>{$call['FirstName']} | {$call['CreatedDate']} | {$call['Note']}</li>\n";
  23.         }
  24.         echo "</ul>\n";
  25.     }
  26. }
  27.  
Dec 29 '08 #6

Sign in to post your reply or Sign up for a free account.

Similar topics

25
by: chad | last post by:
I am writing a program to do some reliability calculations that require several nested for-loops. However, I believe that as the models become more complex, the number of required for-loops will...
46
by: Neptune | last post by:
Hello. I am working my way through Zhang's "Teach yourself C in 24 hrs (2e)" (Sam's series), and for nested loops, he writes (p116) "It's often necessary to create a loop even when you are...
10
by: Pavan | last post by:
Hi i have two nested loops as shown below: 1. for(i=0;i<=1000;i++) { for(i=0;i<=100;i++) { .....; .....; }
77
by: Peter Olcott | last post by:
http://www.tommti-systems.de/go.html?http://www.tommti-systems.de/main-Dateien/reviews/languages/benchmarks.html The above link shows that C# is 450% slower on something as simple as a nested loop....
9
by: Gregory Petrosyan | last post by:
I often make helper functions nested, like this: def f(): def helper(): ... ... is it a good practice or not? What about performance of such constructs?
5
by: =?Utf-8?B?QUEyZTcyRQ==?= | last post by:
Could someone give me a simple example of nested scope in C#, please? I've searched Google for this but have not come up with anything that makes it clear. I am looking at the ECMA guide and...
3
by: Luna Moon | last post by:
My friend has three nested loops, each has 10000, 1000, 100 iterations, respectively. What should be the most efficient way of layout out the three nested loops? for (i=0; i<10000; i++) for...
4
by: toddlahman | last post by:
I am using two while loops that are nested. The first loop (post name) returns the full column of results, but the second (post modified) only returns the first row of the column. Is there another...
13
by: Fredrik Lundh | last post by:
Patrol Sun wrote: so why exactly are you trying to nest 20 or 100 for-in loops? </F>
8
by: Nathan Sokalski | last post by:
I have several nested For loops, as follows: For a As Integer = 0 To 255 For b As Integer = 0 To 255 For c As Integer = 0 To 255 If <Boolean ExpressionThen <My CodeElse Exit For Next If Not...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.