jbw Posted March 18, 2014 Share Posted March 18, 2014 Hi, I hope you can help with the issue below. I'm trying to create a nested loop to output numbers as listed below but I can't get it to work correctly? ie. 1 12 123 1234 12345 $count =1;for ($row = 0; $row <=4; $row++){for ($num = 0; $num <=$row; $count++){echo "";}echo "<br>";} Regards, James. Quote Link to comment https://forums.phpfreaks.com/topic/287050-nested-loops/ Share on other sites More sharing options...
ginerjm Posted March 18, 2014 Share Posted March 18, 2014 What are the limits of this exercise? Interesting that you began some code, but never produced any output. Quote Link to comment https://forums.phpfreaks.com/topic/287050-nested-loops/#findComment-1472984 Share on other sites More sharing options...
jbw Posted March 18, 2014 Author Share Posted March 18, 2014 Hi , No limits, just to produce the outcome of >>> 1 12 123 1234 12345 here is a close working example>> for ($row = 1; $row <= 5; $row++) {for ($col = 1; $col <= $row; $col++) {echo '*'; }echo "\n"; } Thanks, Regards. Quote Link to comment https://forums.phpfreaks.com/topic/287050-nested-loops/#findComment-1472989 Share on other sites More sharing options...
jbw Posted March 18, 2014 Author Share Posted March 18, 2014 <?phpfor ($row = 1; $row <= 5; $row++) {for ($col = 1; $col <= $row; $col++) {echo '*'; }echo "<br>"; }?> Quote Link to comment https://forums.phpfreaks.com/topic/287050-nested-loops/#findComment-1472990 Share on other sites More sharing options...
jbw Posted March 18, 2014 Author Share Posted March 18, 2014 (edited) I just got it working!! <?phpfor ($row = 1; $row <= 5; $row++) {for ($col = 1; $col <= $row; $col++) {echo "$col"; }echo "<br>"; }?> Cheers. Edited March 18, 2014 by jbw Quote Link to comment https://forums.phpfreaks.com/topic/287050-nested-loops/#findComment-1472991 Share on other sites More sharing options...
ginerjm Posted March 18, 2014 Share Posted March 18, 2014 I fail to see how it is a "close working example" when it doesn't output any numbers..... for ($I=1;$I<6;$I++) { for ($num = 1; $num<=$I;$num++) echo $num; echo "<br>"; } This should be a closer example. Quote Link to comment https://forums.phpfreaks.com/topic/287050-nested-loops/#findComment-1472992 Share on other sites More sharing options...
jbw Posted March 18, 2014 Author Share Posted March 18, 2014 (edited) Perfect!!!!! thanks. I was wondering if you could help again in relation to the same output but this time modify the code to make the numbers smaller from 1 and biggest at 5. >>> 1 12 123 1234 12345 Regards. Edited March 18, 2014 by jbw Quote Link to comment https://forums.phpfreaks.com/topic/287050-nested-loops/#findComment-1472993 Share on other sites More sharing options...
ginerjm Posted March 18, 2014 Share Posted March 18, 2014 Add an array: $sz=array("10px","12px","14px","16px","18px"); Then in your $row loop add this : echo "<span style='font-size:${sz[$row]}>"; At the end of the $col loop, add this to the newline echo: "</span>"; Be sure you keep things enclosed in the proper loops. Quote Link to comment https://forums.phpfreaks.com/topic/287050-nested-loops/#findComment-1472996 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.