Jump to content

My codes an abortion :(


zartzar

Recommended Posts

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>";

Link to comment
https://forums.phpfreaks.com/topic/144957-my-codes-an-abortion/
Share on other sites

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

)

 

After the inner for loop, you print the end

tag.

 

 

x
Link to comment
https://forums.phpfreaks.com/topic/144957-my-codes-an-abortion/#findComment-760658
Share on other sites

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?

Link to comment
https://forums.phpfreaks.com/topic/144957-my-codes-an-abortion/#findComment-760662
Share on other sites

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?

Link to comment
https://forums.phpfreaks.com/topic/144957-my-codes-an-abortion/#findComment-760703
Share on other sites

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 :)

Link to comment
https://forums.phpfreaks.com/topic/144957-my-codes-an-abortion/#findComment-761144
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.