Welcome to the MacNN Forums.

If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

You are here: MacNN Forums > Software - Troubleshooting and Discussion > Developer Center > php question - nested for loops

php question - nested for loops
Thread Tools
cnelson87
Forum Regular
Join Date: Jul 2002
Location: Seattle
Status: Offline
Reply With Quote
Apr 27, 2004, 08:08 PM
 
my brain just can't seem to wrap itself around this so please forgive a php newbie. I would like to dynamically generate the rows and cells of an html table at 5 cells to a row. for example, if I have 27 product images to display, I need 6 rows of 5 (the last row having 3 empty cells). I can write the script to round up to 30, and then loop thru 6 times to generate 6 rows. I have that part of my script working with any random number I throw at it. but the part I don't have is to have the 1st row loop thru the first 5 items, the 2nd row loop thru the next 5, etc etc...
hoping my request makes sense, thanks in advance.
Chris
     
larkost
Mac Elite
Join Date: Oct 1999
Location: San Jose, Ca
Status: Offline
Reply With Quote
Apr 27, 2004, 09:58 PM
 
*knuckles crack*
Code:
<table> <?php $itemsPerRow = 5; $theItems = array(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11); for($i = 0; $i < count($theItems);) { ?> <tr> <?php for($j = 0; $j < $itemsPerRow and $i < count($theItems); $j++, $i++) { ?> <td width="20"><?php print $theItems[$i] ?></td> <?php } ?> </tr> <?php } ?> </table>
In some cases you might want to handle running out of items in the middle of a row differently, but this will give you a start.
     
redJag
Senior User
Join Date: Dec 2002
Status: Offline
Reply With Quote
Apr 27, 2004, 10:05 PM
 
whoa, do you always close your PHP tags when you want to print HTML? Seems like a PITA, but I guess it *sort of* seperates front-end from back-end (both integrated in the same file, still..). You know you can use echo "</tr>" etc, right?
Travis Sanderson
     
Arkham_c
Mac Elite
Join Date: Dec 2001
Location: Atlanta, GA, USA
Status: Offline
Reply With Quote
Apr 27, 2004, 10:30 PM
 
Originally posted by redJag:
whoa, do you always close your PHP tags when you want to print HTML? Seems like a PITA, but I guess it *sort of* seperates front-end from back-end (both integrated in the same file, still..). You know you can use echo "</tr>" etc, right?
I always close mine. Otherwise you end up escaping double quotes and other sorts of hackery.
Mac Pro 2x 2.66 GHz Dual core, Apple TV 160GB, two Windows XP PCs
     
madmacgames
Grizzled Veteran
Join Date: Oct 2003
Status: Offline
Reply With Quote
Apr 28, 2004, 11:04 AM
 
there is no need to have nested loops here.

Here are 2 example of how to do this without using nested loops. The 1st is for if the items are in an array, the second is for if the items are not in an array but the total count is known.

[php]
Example 1: using items in an array:
<table border="1" width="50%">
<tr>
<?php
$cells_per_row = '6';

$product_images = array(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15);

$i=0;
foreach ($product_images as $product_image) {
$i++;

echo "<td>$product_image</td>";

if (!($i % $cells_per_row) && ($i != sizeof($product_images))) {
echo "</tr><tr>";
}
}
?>
</tr>
</table>
<br /><br />
Example 2: items not in an array, but count known:
<table border="1" width="50%">
<tr>
<?php
$cells_per_row = '6';
$number_of_items = '15';

for ($i=1; $i<=$number_of_items; $i++) {

echo "<td>$i</td>";

if (!($i % $cells_per_row) && ($i != $number_of_items)) {
echo "</tr><tr>";
}
}
?>
</tr>
</table>
[/php]
     
larkost
Mac Elite
Join Date: Oct 1999
Location: San Jose, Ca
Status: Offline
Reply With Quote
Apr 28, 2004, 12:56 PM
 
redJag: Unless there is good reason to do so, I usually close my tags. It tends to produce cleaner code (as Arkham_c said), and the majority of the time you have more code between the segments. Plus you get the benefit that if you are using a color-coding editor things wind up the right colors.

The reason that many people do not do this is because they come from a ASP background. In ASP moving between code and non-code blocks is fairly expensive, and slows down the render. This is not really the case with PHP.
     
Lucidwray
Junior Member
Join Date: Oct 2001
Location: Great State of Texas
Status: Offline
Reply With Quote
Apr 28, 2004, 03:29 PM
 
This is how i usually handle loops to build tables. It creates a variable that holds a string with the entire completed table in it. Keeps it nice and portable so you can move it around anywhere on the page.

The first part is just some basic code to return results from a MySQL database.
Code:
<? // Start the opening tag of the table $table = "<table width=100% border=0 cellspacing=2 cellpadding=2><tr>"; $cell_count = "0" ; // This is just coded to return results from a MySQL database $results = @mysql_query($sql_count,$connection) or die("Error #" . mysql_errno() . ":" . mysql_error() . ")"); while ($row = @mysql_fetch_array($results)) { $item_name=$row['item_name']; $table .= "<td>$item_name</td>" ; $cell_count++ ; if ($cell_count == 4) { $table .= "</tr><tr>" ; $cell_count = 0 ; } } // close out the table after the while loop exits if ($cell_count < 4) { $table .= "</tr></table>" ; } else { $table .= "</table>" ; } ?>
The formatting is kinda screwed up from the copy paste into here and I havent run this specific code to make sure it runs.. but you get the idea..
nolo contendere: A legal term meaning: "I didn't do it, judge, and I'll never do it again."
     
cnelson87  (op)
Forum Regular
Join Date: Jul 2002
Location: Seattle
Status: Offline
Reply With Quote
May 4, 2004, 03:44 PM
 
Just wanted to say thanks! I got larkost's code working first try.
Chris
     
   
 
Forum Links
Forum Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Top
Privacy Policy
All times are GMT -4. The time now is 06:36 PM.
All contents of these forums © 1995-2017 MacNN. All rights reserved.
Branding + Design: www.gesamtbild.com
vBulletin v.3.8.8 © 2000-2017, Jelsoft Enterprises Ltd.,