bschultz Posted July 24, 2012 Share Posted July 24, 2012 The following code is causing a maximum execution time error... $count = count($links); // how many items are in the array // echo $count; exit; //this echo's 16...so there's stuff in the array $i = 0; // total count $s = 0; // table cell count...should ever get above the value of 3 while ($i <= $count); { if ($s == 0) { echo "<tr>"; } // if at the start of a table row, start the row echo $links[$i]; $i++; $s++; // print out the table cell with the proper value if ($s < 3) { echo "<td> </td>"; $s++; } // if we're not at table column 3, add a new column...this also should close the row with "null" values to fill in the table if ($s == 3) { echo "</tr>"; $s = 0; } // are we at table column 3? If so, close the row. } I'm trying to get the values from an array into an html table...three columns wide. Obviously, the loop is stuck, so the max execution time is running out. I just can't see where. Any ideas? Thanks! Quote Link to comment https://forums.phpfreaks.com/topic/266153-while-loop-with-ifs-inside/ Share on other sites More sharing options...
Genesis730 Posted July 24, 2012 Share Posted July 24, 2012 In line 9 you add 1 to the variable $s... then in line 10 you ask if $s = 3, if not you set it back to 0... if you keep setting $s back to 0 in line 10 then the test in the beginning of line 10 will never result in 3... Quote Link to comment https://forums.phpfreaks.com/topic/266153-while-loop-with-ifs-inside/#findComment-1363930 Share on other sites More sharing options...
neller Posted July 24, 2012 Share Posted July 24, 2012 $count = count($links); // how many items are in the array // echo $count; exit; //this echo's 16...so there's stuff in the array $i = 0; // total count $s = 0; // table cell count...should ever get above the value of 3 while ($i <= $count) { if ($s == 0) { echo "<tr>"; } // if at the start of a table row, start the row echo $links[$i]; $i++; $s++; // print out the table cell with the proper value if ($s < 3) { echo "<td> </td>"; $s++; } // if we're not at table column 3, add a new column...this also should close the row with "null" values to fill in the table if ($s == 3) { echo "</tr>"; $s = 0; } // are we at table column 3? If so, close the row. } try removing the ; from the end of your while statement, I have done so above, I think that will fix it Quote Link to comment https://forums.phpfreaks.com/topic/266153-while-loop-with-ifs-inside/#findComment-1363977 Share on other sites More sharing options...
Mahngiel Posted July 24, 2012 Share Posted July 24, 2012 Not sure why you're indenting the way you are, but your conditionals are not nested. You you could safe yourself execution time by combining some of your conditions and using modulus. You Further have not prepared for the situation when your table will not have 3 cells in the last row. When you're incrementing every iteration, you should use a for() loop <?php $count = count($links); // begin table row $table = '<tr>'; for( $i=0, $s=0; $i <= $count; $i++, $s++; ) { // Third cell reached, close and start a new row if( $s == 3 ) { $table .= '</tr><tr>'; $s = 0; } // Otherwise we create a cell $table .= '<td>' . $links[ $i ] . '</td>'; } // check for end of table offset if( $s != 3 ) $table .= '<td colspan=" . ( 3 - $s) . "> </td>'; // end table $table .= '</tr>'; Quote Link to comment https://forums.phpfreaks.com/topic/266153-while-loop-with-ifs-inside/#findComment-1363995 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.