Skip to content
Advertisement

2 foreach in 1 table is not work correctly

I’m try to loop 2 query inside one table . both query had 10 records . so i put my first foreach (or looping) before TR tag ..

here is example of the table that i want to create :

JavaScript
JavaScript

I don’t know how i can make my table like that with data from my database..

so i tried and put the first Foreach out the TR tag and close it after /TR tg. so as we can see there is 4 TD /TD inside TR /TR . so i put the second foreach in the middle between 4 TD /TD .

just like this :

JavaScript

and the result was not perfect and i guess my foreachs are in the wrong place .

and here u can see the result of my try :

JavaScript

only one row (yellow background) is correct and the others are not . i dont know why . maybe my foreach is not correct or i put it in the wrong place .

and here what i want to achieve but i dont know how :

JavaScript

can someone tell me how and i’ll be so thankful.. and sorry if my English is not really good because its not my mother tongue

Advertisement

Answer

Yep, your logic is missing something because the second loop is running within the first loop, so you’re creating a lot of TDs within the same TR.

In order to achieve what you want, I suggest merging the two arrays, then looping a single array.

JavaScript

Alternatively, if you’re 100% sure that the keys between the two arrays are the same you can also do the following, shorter version without merging the two arrays:

JavaScript
User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement