zartzar Posted February 12, 2009 Share Posted February 12, 2009 Im supposed to create an X by X table using a for loop where X is a random number. Ive been racking my brain all day. I ask you with tears in my eyes, whats wrong! My logic at least seems correct to me.. $r(10,30); for ($a=1; $a<=$r; $a++) {print"<table><tr>"; for ($b=1; $b<=$r; $b++) {print "<td>x</td>";} print "</tr>"; Quote Link to comment Share on other sites More sharing options...
gizmola Posted February 12, 2009 Share Posted February 12, 2009 Use variable names that actually mean something. I can't understand your code because you've omitted important details like where you get the random value. Off the top of my head, the first issue is that you have the statement inside the loop. You only want one table, so you should emit that table tag first. Then you need 2 loops Outer ( 1 - $x Will print the tag) Inner (1 - $x will print x) After the inner for loop, you print the end tag. Quote Link to comment Share on other sites More sharing options...
zartzar Posted February 12, 2009 Author Share Posted February 12, 2009 Use variable names that actually mean something. I can't understand your code because you've omitted important details like where you get the random value. Off the top of my head, the first issue is that you have the <table> statement inside the loop. You only want one table, so you should emit that table tag first. Then you need 2 loops Outer ( 1 - $x Will print the <tr> tag) Inner (1 - $x will print <td>x</td>) After the inner for loop, you print the end </tr> tag. Sorry, im a noob. Anyway, the random value is just that, a random value, it just determines how many rows and collums the table has. a and b are just counters. Or did i misunderstand you? Quote Link to comment Share on other sites More sharing options...
gizmola Posted February 12, 2009 Share Posted February 12, 2009 You have this line of code: $r(10,30); That doesn't mean anything out of context. If you had this: $r = rand(10, 30); Then it might make sense. Anyways, I've given you the solution to your problems and identified what is going wrong for you. Do you understand the answer I provided? Quote Link to comment Share on other sites More sharing options...
gizmola Posted February 12, 2009 Share Posted February 12, 2009 Oh yeah, I guess I should say that your code is basically correct, but you are making a table every loop which is of course the main problem. Only make the table once. Otherwise, your code does what I described. Quote Link to comment Share on other sites More sharing options...
zartzar Posted February 13, 2009 Author Share Posted February 13, 2009 Oh yeah, I guess I should say that your code is basically correct, but you are making a table every loop which is of course the main problem. Only make the table once. Otherwise, your code does what I described. Thanks dude! Got it! Besides the table i didnt close the first loop properly. Works beautifully now Quote Link to comment 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.