Jump to content

Horizontal Loop


tready29483

Recommended Posts

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

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

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

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.