cainchild Posted September 29, 2010 Share Posted September 29, 2010 Sigh. I am learning PHP, started today. So, pursuant to becoming more comfortable with this as a language I started on one a little echo tutorial I found somewhere and expanded it to this code. My issue is that it's only going through $main_loop one time, thus rendering the table a single time, rather than the expected 3 time. Somewhere my while{} is falling apart. It still runs and does its thing, but only the one time. The comments are for my own sake. <?php $main_loop = 0; $column_loop = 0; $row_loop = 0; $column_loop = 0; /* do it all this way until you get comfortable with this var system */ $table_start = "<table BORDER = 1> "; $table_end = "</table> "; $table_row_start = "<tr>"; $table_row_end = "</tr>"; $table_d_start = "<td>"; $table_d_end = "</td>"; echo "<html><head></head><body>"; /* Begins writing the table(s)*/ while($main_loop != 4) /* writes entire table 4 times */ { /* ------------------------------------------while main loop-------------------------------*/ echo $table_start; /* code block to render a table */ while($row_loop != /* specifies teh number of rows -1 */ { /*-------------------while row loop-----------------------*/ echo $table_row_start; while($column_loop != /* specifies the number of <td> per row -1 */ {/*----------------while column loop ---------------------------*/ if ($row_loop == 0) /* part one of checking to see if we are at row 0 col 0 */ { if($column_loop == 0) /* part two of checking to see if row 0 col */ { echo $table_d_start; echo " "; /* if row == 0 and col == 0 leave a blank box */ echo $table_d_end; $column_loop++; } else { echo $table_d_start; if($row_loop == 0) { $row_num = $row_loop + 1; /* if the row is 0 set it to one to prevent 0's everywhere */ } else { $row_num = $row_loop; } if($column_loop == 0) { $col_num = $column_loop + 1; } else { $col_num = $column_loop; } echo $row_num * $col_num; /* multiply row x col */ echo $table_d_end; $column_loop++; } } else { if($row_loop == 0) { $row_num = $row_loop + 1; } else { $row_num = $row_loop; } if($column_loop == 0) { $col_num = $column_loop + 1; } else { $col_num = $column_loop; } echo $table_d_start; echo $row_num * $col_num; echo $table_d_end; $column_loop++; } }/*----------------while column loop ---------------------------*/ echo $table_row_end; $column_loop = 0; $row_loop++; }/*-------------------while row loop-----------------------*/ echo $table_end; echo "<BR>"; $main_loop++; } /* ------------------------------------------while main loop-------------------------------*/ echo "</body></html>"; ?> Sorry I've been drupal alot lately. Quote Link to comment https://forums.phpfreaks.com/topic/214753-leanrning-me-some-php-logics/ Share on other sites More sharing options...
kenrbnsn Posted September 29, 2010 Share Posted September 29, 2010 Please surround your code with not <code></code> tags. Ken Quote Link to comment https://forums.phpfreaks.com/topic/214753-leanrning-me-some-php-logics/#findComment-1117295 Share on other sites More sharing options...
jcbones Posted September 29, 2010 Share Posted September 29, 2010 Replace: while($main_loop != 4) With: for($i = 0; $i < 5; $i++) Remove this line: $main_loop++; Quote Link to comment https://forums.phpfreaks.com/topic/214753-leanrning-me-some-php-logics/#findComment-1117376 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.