Home Web development Loop within a loop – PHP – SitePoint Forums

Loop within a loop – PHP – SitePoint Forums

0

I have

foreach(range(1,$row['group1_receptacles'] + $row['group2_receptacles'] + $row['group3_receptacles']) as $x)
{
  echo "
  • "; echo ''; echo "
  • "; }

    the result…

    The first foreach loop 10 times (tfhags on the right)
    But I have 3 power trails with a power_strip_id of 4,
    so why does the image appear in all and not 4?


    Because for each iteration of the outer loop you cycle through all the power tracks of the inner loop and so it reaches 4 each time of the outer loop.

    Where does $power_strip_id to define oneself ? Should it be in your foreach() instead of $x?



    1 like

    It looks like a lot of unnecessary loops.
    For each of the 10 sockets, you complete all the electrical tracks.

    Why not rather loop the power tracks once, before this foreach, and come up with a number of power tracks for a given band, and they simply for 1 to this number, repeat breadcrumb element? Much less looping.

    Consider if you had 100 power streaks;
    In your current system, you loop 1000 times (10 takes * 100 tracks).
    If you did this ahead of time, you would loop 200 times (once overall, then once for each actual placement).



    1 like