Reputation: 1377
At the moment I have a single array made up of multiple other arrays ie:
-- Category
-- Subcategory
-- Name
-- Count
-- Subcategory
-- Name
-- Count
-- Subcategory
-- Name
-- Count
-- Category
-- Subcategory
-- Name
-- Count
-- Subcategory
-- Name
-- Count
This continues on for approximately 60 categories and approx 10 - 30 subcategories under each.
I want to display the categories & subcategories on a PHP/HTML page in a column format.
My understanding so far would be that I need to divide the total amount of categories by the columns required and then possibly use array_slice
or array_splice
to display the categories for each column.
So the formula for the amount of categories per column would be something like:
$categoriesPerColumn = ceil($TotalNumberOfCategories / $numberOfColumns);
By using columns I mean using <div>
tags with css formatting to seperate the columns ie:
-----------------------------------------------------------------------------
| First Category | Third Category |
| - Subcategory 1 (Count) | - Subcategory 1 |
| - Subcategory 2 (Count) | - Subcategory 2 |
| - Subcategory 3 (Count) | Fourth Category |
| - Subcategory 4 (Count) | - Subcategory 1 |
| Second Category | Fifth Category |
| - Subcategory 1 (Count) | - Subcategory 1 |
| - Subcategory 2 (Count) | - Subcategory 2 |
| - Subcategory 3 (Count) | - Subcategory 3 |
| - Subcategory 4 (Count) | - Subcategory 4 |
| | - Subcategory 5 |
-----------------------------------------------------------------------------
Questions:
Upvotes: 1
Views: 465
Reputation: 132274
While I don't entirely understand what you're asking, I'm going to take a wild stab and go with something like the following:
EDIT: With column weightings, so the columns will come out relatively the same length:
$categories = array(
"First Category" => array(
array("name" => "First subcategory", /* etc */),
array("name" => "Second subcategory", /* etc */),
array("name" => "Third subcategory", /* etc */),
),
"Second Category" => array(
array("name" => "First subcategory", /* etc */),
array("name" => "Second subcategory", /* etc */),
),
"Third Category" => array(
array("name" => "First subcategory", /* etc */),
array("name" => "Second subcategory", /* etc */),
),
);
// Setup the preprocessing.
$numColumns = 2;
$columnLength = array();
$columnData = array();
for ($i = 0; $i <= $numColumns; $i++)
{ $columnLength[] = 0; $columnData[] = ''; }
// Sort the category array
ksort($categories);
// Process our data
foreach ($categories as $cname => $subcats) {
$minLength = $columnLength[0];
$minIndex = 0;
for ($i = 1; $i < $numColumns; $i++) {
if ($columnLength[$i] < $minLength) {
$minLength = $columnLength[$i];
$minIndex = $i;
}
}
$columnLength[$minIndex] += 1 + count($subcat);
$columnData[$minIndex] .= '<center style="text-size:1.1em">'.$cname.'</center><br/>';
foreach($subcats as $subcat) {
$columnData[$minIndex] .= '- '.$subcat['name'].'<br/>';
}
}
// Display our data
for ($i = 0; $i < $numColumns; $i++) {
echo '<div class="column'.($i+1).'">'.$columnData[$i]."</div>\n";
}
Upvotes: 4