bugzy Posted January 12, 2011 Share Posted January 12, 2011 Hello I've registered here last March and I'm just lurking around reading threads about php. Yesterday I've started to self studied php and today I've test some basic codes and I'm having difficulties on creating a table rows and columns. Here's my code. <?php echo "<table width=\"100\" border=\"1\">"; $text = 'php'; $mac_var = 1; $mac_plus = 4; while ($mac_var < $mac_plus) { echo "<tr>"; { while ($mac_var < $mac_plus) { echo "<td>". $text . "</td>"; $mac_var++; } } echo "</tr>"; $mac_var++; } echo "</table>"; ?> what I want is, it should have 3 columns and 3 rows. But I'm getting only 3 columns and 1 row only. I wonder what is the problem? Sorry for my ignorance as I'm on the 1st basic phase of using php codes. TIA! Link to comment https://forums.phpfreaks.com/topic/224213-newbie-creating-html-table-using-php-problem/ Share on other sites More sharing options...
litebearer Posted January 12, 2011 Share Posted January 12, 2011 your inner while is identical to your outer while; therefore it increments to the max value before you ever reach a 2nd row Link to comment https://forums.phpfreaks.com/topic/224213-newbie-creating-html-table-using-php-problem/#findComment-1158492 Share on other sites More sharing options...
AbraCadaver Posted January 12, 2011 Share Posted January 12, 2011 Because you increment the condition counter in both loops so it hits 4 in the inner loop and the outer loop only executes the first time. Plus you've got extra curly braces in there. Try: $text = 'php'; $max_rows = 3; $max_cols = 3; echo "<table>"; $rows = 1; while ($rows <= $max_rows) { echo "<tr>"; $cols = 1; // reset columns to 1 for each row while ($cols <= $max_cols) { echo "<td>". $text . "</td>"; $cols++; // column counter } echo "</tr>"; $rows++; //row counter } echo "</table>"; Link to comment https://forums.phpfreaks.com/topic/224213-newbie-creating-html-table-using-php-problem/#findComment-1158500 Share on other sites More sharing options...
bugzy Posted January 12, 2011 Author Share Posted January 12, 2011 Thanks you very much to litebearer and AbraCadaver Problem Solved! Great forum and I'm learning here. Thanks again! Link to comment https://forums.phpfreaks.com/topic/224213-newbie-creating-html-table-using-php-problem/#findComment-1158504 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.