Jump to content

Dynamic table columns - max number before new row


yandoo

Recommended Posts

Hiya I have database records that are outputted in a table column for each record. I want to make it so that when every 5 records are outputted and new  row is created and then another 5 column records are outputted and so on. I can't get my head around this and have attempted to create a maximum record count. 

 

if ($productCount > 0) {
while($row = mysql_fetch_array($sql4)){ 
             $id = $row["id"];
$product_name = $row["product_name"];
$price = $row["price"];
$category = $row["category"];
$details = $row['details'];
$date_added = strftime("%b %d, %Y", strtotime($row["date_added"]));

$maxCount = 5;

if ($productCount != $maxCount) {

 $dynamicList .= '<td width="150"><a href="inventory_images/'.$id.'.jpg" target="_blank"><img style="border:#666 1px solid;" src="inventory_images/' . $id . '_small.jpg" alt="' . $product_name . '" border="1" width="150"/></a> '.$details.'£'.$price.' <form id="form1" name="form1" method="post" action="cart.php"><input type="hidden" name="pid" id="pid" value='.$id.' /><input type="submit" value="" name="button" id="button" class="cardbutton" /></form></td>'; 
}

else if ($productCount == $maxCount) {

 $dynamicList .= '<td width="150"><a href="inventory_images/'.$id.'.jpg" target="_blank"><img style="border:#666 1px solid;" src="inventory_images/' . $id . '_small.jpg" alt="' . $product_name . '" border="1" width="150"/></a> '.$details.'£'.$price.' <form id="form1" name="form1" method="post" action="cart.php"><input type="hidden" name="pid" id="pid" value='.$id.' /><input type="submit" value="" name="button" id="button" class="cardbutton" /></form></td></tr><tr>'; }

 

Needless to say it doesn't work and just outputs all the records in a single row. So I guess the else if ($productCount == $maxCount) never happens. Any ideas on how i can get this to work please?

 

Thank you very much.

 

Tom

He

You don't count how many record is echoed.

Put counter to zero before while loop

incrise counter in loop (just in start of it)

This counter compare with $maxCount

in second if statement return counter to zero

if ($productCount > 0) {
$cnt = 0;
while($row = mysql_fetch_array($sql4)){
$cnt++; 
             $id = $row["id"];
$product_name = $row["product_name"];
$price = $row["price"];
$category = $row["category"];
$details = $row['details'];
$date_added = strftime("%b %d, %Y", strtotime($row["date_added"]));

$maxCount = 5;

if ($cnt != $maxCount) {

 $dynamicList .= '<td width="150"><a href="inventory_images/'.$id.'.jpg" target="_blank"><img style="border:#666 1px solid;" src="inventory_images/' . $id . '_small.jpg" alt="' . $product_name . '" border="1" width="150"/></a> '.$details.'£'.$price.' <form id="form1" name="form1" method="post" action="cart.php"><input type="hidden" name="pid" id="pid" value='.$id.' /><input type="submit" value="" name="button" id="button" class="cardbutton" /></form></td>'; 
}

else if ($cnt == $maxCount) {
$cnt = 0;
 $dynamicList .= '<td width="150"><a href="inventory_images/'.$id.'.jpg" target="_blank"><img style="border:#666 1px solid;" src="inventory_images/' . $id . '_small.jpg" alt="' . $product_name . '" border="1" width="150"/></a> '.$details.'£'.$price.' <form id="form1" name="form1" method="post" action="cart.php"><input type="hidden" name="pid" id="pid" value='.$id.' /><input type="submit" value="" name="button" id="button" class="cardbutton" /></form></td></tr><tr>'; }

The process could be simplified by storing the column information in an array and using array_chunk() to break it into groups of 5. Oddly enough, I just posted a blog entry about this technique:

http://www.cyberscorpion.com/2013-08/build-html-tables-dynamically-with-php-part-2-simplify-with-array_chunk/

 

Basically, you could do something like the following (note that the following code is untested):

<?php
//INITIALIZE OUTPUT ARRAY
$dynamicList = array();
 
//GRAB COLUMN INFORMATION
while($row = mysql_fetch_array($sql4)){ 
$id = $row["id"];
$product_name = $row["product_name"];
$price = $row["price"];
$category = $row["category"];
$details = $row['details'];
$date_added = strftime("%b %d, %Y", strtotime($row["date_added"]));
 
$dynamicList[] = '<td width="150"><a href="inventory_images/'.$id.'.jpg" target="_blank"><img style="border:#666 1px solid;" src="inventory_images/' . $id . '_small.jpg" alt="' . $product_name . '" border="1" width="150"/></a> '.$details.'£'.$price.' <form id="form1" name="form1" method="post" action="cart.php"><input type="hidden" name="pid" id="pid" value='.$id.' /><input type="submit" value="" name="button" id="button" class="cardbutton" /></form></td>'; 
}
 
//BREAK APART ARRAY
$dynamicList = array_chunk($dynamicList, 5);
 
//DISPLAY TABLE
print '<table>';
foreach($dynamicList as $currRow) {
     print '<tr>' . implode('', $currRow) . '</tr>';
}
print '</table>';
?>

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.