mayman212 Posted November 2, 2011 Share Posted November 2, 2011 <table> <tr> <td align="center"> <?PHP $b = 1; for ($a=1; $a<=16; $a++) { echo str_repeat($a, $b); $b++; echo "<br>"; } $b = 17; for ($a=1; $a<=26; $a++) { echo str_repeat($a, $b); $b--; echo "<br>"; } ?> </td> </tr> </table> I am trying to make something like this: 1 23 456 7891011 1213141516 17181920 212223 2425 26 But i have managed what I've shown above. Anyone have any help on this? Link to comment https://forums.phpfreaks.com/topic/250284-for-loop/ Share on other sites More sharing options...
mayman212 Posted November 2, 2011 Author Share Posted November 2, 2011 <table> <tr> <td align="center"> <?PHP $b = 1; for ($a=1; $a<=16; $a++) { echo str_repeat($a, $b); $b++; echo "<br>"; } $b = 17; for ($a=17; $a<=26; $a++) { echo str_repeat($a, $b); $b++; echo "<br>"; } ?> </td> </tr> </table> I am trying to make something like this: 1 23 456 7891011 1213141516 17181920 212223 2425 26 But i have managed what I've shown above. Anyone have any help on this? (slightly updated) Link to comment https://forums.phpfreaks.com/topic/250284-for-loop/#findComment-1284216 Share on other sites More sharing options...
mayman212 Posted November 2, 2011 Author Share Posted November 2, 2011 <table> <tr> <td align="center"> <?PHP $b = 1; for ($a=1; $a<=1; $a++) { echo $b++; echo "<br>"; } $b = 2; for ($a=1; $a<=2; $a++) { echo $b++; } echo "<br>"; $b = 4; for ($a=1; $a<=3; $a++) { echo $b++; } echo "<br>"; $b = 7; for ($a=1; $a<=5; $a++) { echo $b++; } echo "<br>"; $b = 12; for ($a=1; $a<=5; $a++) { echo $b++; } echo "<br>"; $b = 17; for ($a=1; $a<=4; $a++) { echo $b++; } echo "<br>"; $b = 21; for ($a=1; $a<=3; $a++) { echo $b++; } echo "<br>"; $b = 24; for ($a=1; $a<=2; $a++) { echo $b++; } echo "<br>"; $b = 26; for ($a=1; $a<=1; $a++) { echo $b++; } ?> </td> </tr> </table> Okay, I figured it out, well kind of, but I need to reduce the amount of line have. Any ideas? Link to comment https://forums.phpfreaks.com/topic/250284-for-loop/#findComment-1284221 Share on other sites More sharing options...
AyKay47 Posted November 2, 2011 Share Posted November 2, 2011 you can condense that into one for loop with if conditionals.. for($a=1; $a <= 26; $a++){ if($a == 1) echo $a ."<br />".; if($a == 2 || $ == 3) echo $a; if($a == 4) echo $a ."<br />".; } won't get an award on its looks... but will use one for loop instead of 9 Link to comment https://forums.phpfreaks.com/topic/250284-for-loop/#findComment-1284226 Share on other sites More sharing options...
mayman212 Posted November 2, 2011 Author Share Posted November 2, 2011 nope that didnt work mate> Link to comment https://forums.phpfreaks.com/topic/250284-for-loop/#findComment-1284228 Share on other sites More sharing options...
AyKay47 Posted November 2, 2011 Share Posted November 2, 2011 for($a = 1; $a <= 26; $a++){ if($a == 1) echo $a ."<br />"; if($a == 2 || $ == 3) echo $a; if($a == 4) echo $a ."<br />"; } concatenation error.. it will work Link to comment https://forums.phpfreaks.com/topic/250284-for-loop/#findComment-1284229 Share on other sites More sharing options...
mayman212 Posted November 2, 2011 Author Share Posted November 2, 2011 ( ! ) Parse error: syntax error, unexpected T_IS_EQUAL, expecting T_VARIABLE or '$' in C:\wamp\www\p1\m2.php on line 10 that is what's showing. Link to comment https://forums.phpfreaks.com/topic/250284-for-loop/#findComment-1284232 Share on other sites More sharing options...
mayman212 Posted November 2, 2011 Author Share Posted November 2, 2011 anyone have any more clues? Link to comment https://forums.phpfreaks.com/topic/250284-for-loop/#findComment-1284244 Share on other sites More sharing options...
AyKay47 Posted November 2, 2011 Share Posted November 2, 2011 where is line ten Link to comment https://forums.phpfreaks.com/topic/250284-for-loop/#findComment-1284257 Share on other sites More sharing options...
mayman212 Posted November 2, 2011 Author Share Posted November 2, 2011 Sorry? Link to comment https://forums.phpfreaks.com/topic/250284-for-loop/#findComment-1284258 Share on other sites More sharing options...
AyKay47 Posted November 2, 2011 Share Posted November 2, 2011 your error is on line 10.. show us line 10 Link to comment https://forums.phpfreaks.com/topic/250284-for-loop/#findComment-1284260 Share on other sites More sharing options...
mayman212 Posted November 2, 2011 Author Share Posted November 2, 2011 <table> <tr> <td align="center"> <?PHP $b = 1; for ($a=1; $a<=1; $a++) { echo $b++; echo "<br>"; } $b = 2; for ($a=1; $a<=2; $a++) { echo $b++; } echo "<br>"; $b = 4; for ($a=1; $a<=3; $a++) { echo $b++; } echo "<br>"; $b = 7; for ($a=1; $a<=5; $a++) { echo $b++; } echo "<br>"; $b = 12; for ($a=1; $a<=5; $a++) { echo $b++; } echo "<br>"; $b = 17; for ($a=1; $a<=4; $a++) { echo $b++; } echo "<br>"; $b = 21; for ($a=1; $a<=3; $a++) { echo $b++; } echo "<br>"; $b = 24; for ($a=1; $a<=2; $a++) { echo $b++; } echo "<br>"; $b = 26; for ($a=1; $a<=1; $a++) { echo $b++; } ?> </td> </tr> </table> This is still the code I am using, the one you gave earlier came up with the unexpected error. Link to comment https://forums.phpfreaks.com/topic/250284-for-loop/#findComment-1284263 Share on other sites More sharing options...
mayman212 Posted November 2, 2011 Author Share Posted November 2, 2011 Anyone? Link to comment https://forums.phpfreaks.com/topic/250284-for-loop/#findComment-1284273 Share on other sites More sharing options...
AyKay47 Posted November 2, 2011 Share Posted November 2, 2011 using the code i provided.. show me line 10. Link to comment https://forums.phpfreaks.com/topic/250284-for-loop/#findComment-1284286 Share on other sites More sharing options...
mayman212 Posted November 2, 2011 Author Share Posted November 2, 2011 <table> <tr> <td align="center"> <?PHP $b = 1; for($a = 1; $a <= 26; $a++){ if($a == 1) echo $a ."<br />"; if($a == 2 || $ == 3) echo $a; if($a == 4) echo $a ."<br />"; } ?> </td> </tr> </table> Link to comment https://forums.phpfreaks.com/topic/250284-for-loop/#findComment-1284288 Share on other sites More sharing options...
Far Cry Posted November 2, 2011 Share Posted November 2, 2011 "$" is not a valid variable. Link to comment https://forums.phpfreaks.com/topic/250284-for-loop/#findComment-1284304 Share on other sites More sharing options...
Drummin Posted November 2, 2011 Share Posted November 2, 2011 Maybe a little primitive for the coding gurus, but it duplicates the example you gave. <table> <tr> <td align="center"> <?PHP $a=1; $b=1; $d=16; $c=1;//line limit while ($b<=$c && $a<=$d){ while ($b<=$c && $a<=$d){ echo $a; $a++; $b++; } echo "<br>"; IF ($c<3){ $c++; } ELSE{ $c=5; } $b=1; } //Start Down $b=4; $c=1; $e=26; while ($c<=$b && $a<=$e){ while ($c<=$b && $a<=$e){ echo $a; $a++; $c++; } echo "<br>"; $b--; $c=1; } ?> </td> </tr> </table> Link to comment https://forums.phpfreaks.com/topic/250284-for-loop/#findComment-1284356 Share on other sites More sharing options...
mayman212 Posted November 3, 2011 Author Share Posted November 3, 2011 Thanks for that. But has anyone got something with less lines? Link to comment https://forums.phpfreaks.com/topic/250284-for-loop/#findComment-1284657 Share on other sites More sharing options...
mayman212 Posted November 4, 2011 Author Share Posted November 4, 2011 I need something with a little less lines, the code I have right now is a little too long. Anyone have any suggestions? Link to comment https://forums.phpfreaks.com/topic/250284-for-loop/#findComment-1284869 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.