talksr Posted December 5, 2007 Share Posted December 5, 2007 What sort of code can I make which will print odd numbers from 2 to 99 in a table on a webpage? ??? Quote Link to comment Share on other sites More sharing options...
pocobueno1388 Posted December 5, 2007 Share Posted December 5, 2007 This will display all odd numbers between 2 and 99 <?php for ($i=3; $i<100; $i+=2){ echo $i.'<br>'; } ?> I'm not sure how you want the table, so if you need help with that, explain it a little. Quote Link to comment Share on other sites More sharing options...
cooldude832 Posted December 5, 2007 Share Posted December 5, 2007 from 2 to 99 lol 2 is even prime and not odd Quote Link to comment Share on other sites More sharing options...
talksr Posted December 9, 2007 Author Share Posted December 9, 2007 from 2 to 99 lol 2 is even prime and not odd I have just realised that! Sorry about the mistake. Basically, I would like php to create the numbers and insert them into a table in a web page. Quote Link to comment Share on other sites More sharing options...
pocobueno1388 Posted December 9, 2007 Share Posted December 9, 2007 Basically, I would like php to create the numbers and insert them into a table in a web page. Well, I gave you the code to generate the numbers...now all you have to do is generate the HTML you want. If you don't know how to do that, then give a description of how you want the table to look. Quote Link to comment Share on other sites More sharing options...
talksr Posted December 9, 2007 Author Share Posted December 9, 2007 I know how to create a table, but not how to get the numbers into the table. I have no preference as to how the table looks, just want to try some things out really. Any help would be great. Quote Link to comment Share on other sites More sharing options...
pocobueno1388 Posted December 9, 2007 Share Posted December 9, 2007 How about this? <?php echo '<table border=1>'; for ($i=3; $i<100; $i+=2){ echo '<tr>'; echo '<td>'.$i.'</td>'; echo '</tr>'; } echo '</table>'; ?> Quote Link to comment Share on other sites More sharing options...
talksr Posted December 9, 2007 Author Share Posted December 9, 2007 Thanks, looks good. If I wanted to add columns what could I insert into the php code, so say I wanted to use 3 or 5 columns to make it easier to fit on the page? Quote Link to comment Share on other sites More sharing options...
pocobueno1388 Posted December 9, 2007 Share Posted December 9, 2007 <?php echo '<table border=1>'; $cols = 1; for ($i=3; $i<100; $i+=2){ echo '<td>'.$i.'</td>'; if ($cols == 5){ echo '<tr>'; $cols = 0; } $cols++; } echo '</table>'; ?> Quote Link to comment Share on other sites More sharing options...
talksr Posted December 9, 2007 Author Share Posted December 9, 2007 Thanks for all of your help, thats super. Is there any way you can explain to me what is happening on these lines: for ($i=3; $i<100; $i+=2){ and also if ($cols == 5){ echo '<tr>'; $cols = 0; } Quote Link to comment Share on other sites More sharing options...
revraz Posted December 9, 2007 Share Posted December 9, 2007 Start $i at 3, count up to 100, increment $i +2 because you want odd only. Set cols to 1 and print $i in a row, count up to 5 and when you hit 5 start a new row. Quote Link to comment Share on other sites More sharing options...
pocobueno1388 Posted December 9, 2007 Share Posted December 9, 2007 This line for ($i=3; $i<100; $i+=2){ Is telling $i to start at 3, increase by 2 each time the loop runs, and to run the loop 99 times (since you want all odd numbers up to 99. if ($cols == 5){ echo '<tr>'; $cols = 0; } I set up a counter named $cols, this counts how many times the loop has run...so if it has run 5 times, then $cols equals 5. So that code checks if the loop has run 5 times, and if it has, to create a new row (since you want 5 numbers per line). Then it starts the counter over for the next row. Hopefully those make sense Quote Link to comment Share on other sites More sharing options...
talksr Posted December 9, 2007 Author Share Posted December 9, 2007 Thanks pocobueno1388,and everyone else for your help Quote Link to comment Share on other sites More sharing options...
pocobueno1388 Posted December 9, 2007 Share Posted December 9, 2007 No problem Don't forget to press "topic solved". Quote Link to comment Share on other sites More sharing options...
talksr Posted December 9, 2007 Author Share Posted December 9, 2007 No probs thanks again 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.