Jump to content

how do i write a nested loop to make a grid


digitalmartyr

Recommended Posts

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

Link to comment
Share on other sites

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>';

}

Link to comment
Share on other sites

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>";

Link to comment
Share on other sites

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

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.