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. 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. 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. 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>"; }?> 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 I just got it working!! <?phpfor ($row = 1; $row <= 5; $row++) {for ($col = 1; $col <= $row; $col++) {echo "$col"; }echo "<br>"; }?> Cheers. 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. 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 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. 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. Link to comment https://forums.phpfreaks.com/topic/287050-nested-loops/#findComment-1472996 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.