tready29483 Posted May 3, 2007 Share Posted May 3, 2007 Hey, I have a question that might be siple to all of you but for some reason I can't figure it out. I have an array with 12 items in the array. I want to loop through them but I want them stacked in columns of 3. So total there should be 4 rows and 3 columns. I've seen a couple of examples on the web but I can't get them to work. can some one please help me? thnx Link to comment https://forums.phpfreaks.com/topic/49861-horizontal-loop/ Share on other sites More sharing options...
Caesar Posted May 3, 2007 Share Posted May 3, 2007 Just to clarify what you're asking....Are you saying you want the results/values from an array, to be displayed in 3 columns/4 rows, in the HTML of your page? MInd showing some of your code? Link to comment https://forums.phpfreaks.com/topic/49861-horizontal-loop/#findComment-244619 Share on other sites More sharing options...
tready29483 Posted May 3, 2007 Author Share Posted May 3, 2007 I half way figured it out by just messing around: public function createTemplateTable() { define('NUM_COLS', 4); $count=0; $temps = $this->Content->fetchtemplatelistByTypeID(1); foreach($temps as $key=> $value) { $count++; echo $value['template_name'] . ' '; if($count== NUM_COLS){ echo '<br/>'; $count=0; } } Link to comment https://forums.phpfreaks.com/topic/49861-horizontal-loop/#findComment-244625 Share on other sites More sharing options...
Barand Posted May 3, 2007 Share Posted May 3, 2007 try <?php $ar = range(1,12); $k = count ($ar); $cols = 4; $rows = ceil($k/$cols); echo '<table border="1">'; for ($r=0; $r < $rows; $r++) { echo '<tr>'; for ($c=0; $c < $cols; $c++) { echo "<td>{$ar[$r+$rows*$c]}</td>"; } echo '</tr>'; } ?> --> 1 4 7 10 2 5 8 11 3 6 9 12 Link to comment https://forums.phpfreaks.com/topic/49861-horizontal-loop/#findComment-244646 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.