bukwus Posted March 20, 2012 Share Posted March 20, 2012 Hello I'm trying to use PHP "for loops" to build a table of 10 rows with 5 columns and I'm sure there's a better way to do this, but the result is the page won't fully load and continually spins its wheel. Here's the code: $states = array(1 => 'AK','AL','AR','AZ','CA','CO','CT','DC','DE','FL','GA','HI','IA','ID','IL','IN','KS','KY','LA','MA','MD','ME','MI','MN','MO','MS','MT','NC','ND','NE','NH','NJ','NM','NV','NY','OH','OK','OR','OZ','PA','PR','RI','SC','SD','TN','TX','UT','VA','VT','WA','WI' ); <table id="stateCheck"> <?php for ($i = 1; $i <= 10; $i++) { echo '<tr>'; for ($j = 5*$i; $j <= $j+4; $j++) { echo '<td> <input type="checkbox" name="state" value="'.$states[$j].'" /> '.$states[$j].' </td>'; } echo '</tr>'; } ?> <tr> <td> <input type="checkbox" name="state" value="WV" /> WV </td> <td> <input type="checkbox" name="state" value="WY" /> WY </td> </tr> </table> Is it breaking because I'm using arithmetic operators to define the nested for loop? Many thanks Quote Link to comment https://forums.phpfreaks.com/topic/259337-using-arithmetic-operators-to-define-a-for-loop/ Share on other sites More sharing options...
smerny Posted March 20, 2012 Share Posted March 20, 2012 $j <= $j+4 $j will always be less than $j+4 Quote Link to comment https://forums.phpfreaks.com/topic/259337-using-arithmetic-operators-to-define-a-for-loop/#findComment-1329430 Share on other sites More sharing options...
creata.physics Posted March 20, 2012 Share Posted March 20, 2012 I'm not sure what you mean by breaking. Does it timeout or just not work? Anyway, try this code: <?php $states = array(1 => 'AK','AL','AR','AZ','CA','CO','CT','DC','DE','FL','GA','HI','IA','ID','IL','IN','KS','KY','LA','MA','MD','ME','MI','MN','MO','MS','MT','NC','ND','NE','NH','NJ','NM','NV','NY','OH','OK','OR','OZ','PA','PR','RI','SC','SD','TN','TX','UT','VA','VT','WA','WI' ); $idx = $count = 0; $list = '<table>'; foreach($states as $state) { if($count == 0) $list .= "<tr>"; $list .= "<td align='center' width='20%'>{$state}</td>"; ++$idx; ++$count; if ( ( $count == 5 ) OR ( $state == end( $states ) ) ) { $count = 0; } } $surplus = 5 - ($idx - (5 * (ceil($idx / 5) - 1))); if($idx > 5) { for($i = 1; $i <= $surplus; $i++) { $list .= '<td> </td>'; } } $list .= '</tr>'; echo $list; Edit: Oops, I didn't include the html for your checkboxes, make sure you add that if you use this code. Quote Link to comment https://forums.phpfreaks.com/topic/259337-using-arithmetic-operators-to-define-a-for-loop/#findComment-1329431 Share on other sites More sharing options...
bukwus Posted March 20, 2012 Author Share Posted March 20, 2012 Wow. I clearly need an arithmetic refresher. Many thanks, this is very helpful. Quote Link to comment https://forums.phpfreaks.com/topic/259337-using-arithmetic-operators-to-define-a-for-loop/#findComment-1329443 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.