Reputation: 7520
Need a little help here. And sorry for my wrong title if it confused you. Right now I am creating a table guide for indexing company name. I need to limit the column of table by 40.
So every rows is 40 columns. And the continuation of the display will be going to the next row. I have a code like this but I don't have an idea how to include the next rows .
<?php
$company_list = db_get_array('SELECT company FROM cscart_companies ORDER BY company');
$div = $total /40; //set rows EX: 22
echo "<table border='1'>";
foreach($company_list as $company_details){
if($div != 0){
$counter = 1; //set for columns
if($counter == 40){
//create another row
}else{
//add new column
echo "<td>".strtoupper(substr($company_details['company'],0,3))."</td>";
$counter++;
}
}else{
//exit loop and <tr>
}
$div--;
}
echo "</table>";
?>
Upvotes: 0
Views: 70
Reputation: 6275
This problem occurs a lot in programming - that is collecting groups of things from a larger set. You're on the right track, and there are many ways to accomplish this. This is one way...
$counter = 0;
$group = array();
echo "<table>";
foreach ($company_list as $company_details) {
if ($counter == 40) {
// output one row (40 records)
echo "<tr>" . join("\n", $group) . "</tr>";
// reset the counter and empty the group
$counter = 0;
$group = array();
}
// add one record to the group array and increment the counter
$group[] = "<td>" . strtoupper(substr($company_details['company'],0,3)) . "</td>";
++$counter;
}
// output any remaining records
if (count($group) > 0)
echo "<tr>" . join("\n", $group) . "</tr>";
// close the table
echo "</table>";
Upvotes: 1