Jump to content

how do i write a nested loop to make a grid


digitalmartyr

Recommended Posts

I need to take my mysql result and take those results and print them out, one by one, in a 3 by 3 grid, i understand that a nested loop is what it will take to make this happen, but im having trouble writing it, if someone can get me started in the right direction, it would be most appriciated.

If I'm understanding you correctly you want something that will look like this:

 

+---+---+---+
| 1 | 2 | 3 |
+---+---+---+
| 4 | 5 | 6 |
+---+---+---+
| 7 | 8 | 9 |
+---+---+---+

 

You don't need a nested loop to do this, you just need a loop and an if statement.  It'll look something like this

 

<?php

echo "<table border=\"1\">";

$array = array("1", "2", "3", "4", "5", "6", "7", "8", "9");

for($i = 0; $i < 9; $i++){
    if(($i + 1) % 3 = 1){
        echo "<tr><td>{$array[$i]}</td>";
    } elseif(($i + 1) % 3 = 2){
        echo "<td>{$array[$i]}</td>";
    } elseif(($i + 1) % 3 = 0){
        echo "<td>{$array[$i]}</td></tr>";
    }
}

echo "</table>";

?>

 

Making use of the % operator is the easiest (in my opinion) way to grid the contents of an array.

 

Theo

wow, i thought it was through nested loops. The way you showed me, is it possible to use a while loop instead of a for loop, because im pulling data from a database, and im echoing divs with data inside like this

while($info = mysql_fetch_array($data))
{
echo '<div id="productThumb">';
echo '<h3>product id: '.$info['product_id'].'</h3>';
echo '<h2>product name: '.$info['product_name'].'</h2>';
echo '<img height="140" width="100" src="'.$info['product_img'].'"</img>';
echo '<span>Product Describtion '.$info['product_dscrb'].'</span>';
echo '</div>';

}

Yes you can use a while loop, just make a counter for the $i variable:

echo "<table border=\"1\">";

// initiate counter
$i = 0;

while($info = mysql_fetch_array($data))
{
$html  = '<div id="productThumb">';
$html .= '<h3>product id: '.$info['product_id'].'</h3>';
$html .= '<h2>product name: '.$info['product_name'].'</h2>';
$html .= '<img height="140" width="100" src="'.$info['product_img'].'"</img>';
$html .= '<span>Product Describtion '.$info['product_dscrb'].'</span>';
$html .= '</div>';

    if(($i + 1) % 3 = 1){
        echo "<tr><td>$html</td>";
    } elseif(($i + 1) % 3 = 2){
        echo "<td>$html</td>";
    } elseif(($i + 1) % 3 = 0){
        echo "<td>$html</td></tr>";
    }

    // increment counter
    $i++;

    // unset html var
    unset($html);
}

echo "</table>";

that's a lot of usage of the modulus.

 

<?php

echo "<table border=\"1\" cellspacing=\"3\" cellpadding=\"3\">";

$array = array("once", "there", "was", "a", "puppy", "who", "loved", "friendly", "people");


echo "<tr>\n";

$z = round(count($array)/3);

$x=1;
$y=1;
for($i=0;$i<count($array);$i++){
echo "<td>".$array[$i]."</td>\n";

if($x == 3){
	echo "</tr>\n";

	if($y != $z){
		echo "<tr>\n";
	}

	$x=0;
	$y++;
}

$x++;
}


echo "</table>";

?>

 

i'd say something like that is a bit simpler

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.