yandoo Posted August 19, 2013 Share Posted August 19, 2013 (edited) 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 Edited August 19, 2013 by yandoo Quote Link to comment https://forums.phpfreaks.com/topic/281354-dynamic-table-columns-max-number-before-new-row/ Share on other sites More sharing options...
Solution sasa Posted August 19, 2013 Solution Share Posted August 19, 2013 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>'; } Quote Link to comment https://forums.phpfreaks.com/topic/281354-dynamic-table-columns-max-number-before-new-row/#findComment-1445777 Share on other sites More sharing options...
cyberRobot Posted August 19, 2013 Share Posted August 19, 2013 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>'; ?> Quote Link to comment https://forums.phpfreaks.com/topic/281354-dynamic-table-columns-max-number-before-new-row/#findComment-1445797 Share on other sites More sharing options...
yandoo Posted August 19, 2013 Author Share Posted August 19, 2013 Thank you very much, that's solved my problem and elevated my level of understanding. Quote Link to comment https://forums.phpfreaks.com/topic/281354-dynamic-table-columns-max-number-before-new-row/#findComment-1445870 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.