digitalmartyr Posted February 12, 2008 Share Posted February 12, 2008 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. Quote Link to comment https://forums.phpfreaks.com/topic/90638-how-do-i-write-a-nested-loop-to-make-a-grid/ Share on other sites More sharing options...
nethnet Posted February 12, 2008 Share Posted February 12, 2008 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 Quote Link to comment https://forums.phpfreaks.com/topic/90638-how-do-i-write-a-nested-loop-to-make-a-grid/#findComment-464653 Share on other sites More sharing options...
digitalmartyr Posted February 12, 2008 Author Share Posted February 12, 2008 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>'; } Quote Link to comment https://forums.phpfreaks.com/topic/90638-how-do-i-write-a-nested-loop-to-make-a-grid/#findComment-464660 Share on other sites More sharing options...
wildteen88 Posted February 12, 2008 Share Posted February 12, 2008 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>"; Quote Link to comment https://forums.phpfreaks.com/topic/90638-how-do-i-write-a-nested-loop-to-make-a-grid/#findComment-465142 Share on other sites More sharing options...
marcus Posted February 12, 2008 Share Posted February 12, 2008 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 Quote Link to comment https://forums.phpfreaks.com/topic/90638-how-do-i-write-a-nested-loop-to-make-a-grid/#findComment-465148 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.