tags:

views:

297

answers:

1

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:

  1. Is this the correct way of doing so?
  2. How can this be done in PHP?
+3  A: 

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";
}
Matthew Scharley
Defintely not what I was looking for, but none the less accurate. If you had a similar answer for the problem (I've explained it a bit better) that would be great. Thank you
privateace
If you don't care about the column lengths being equal, then it's a trivial problem, I'll work something out soon.
Matthew Scharley
That's absolutely perfect. Just a quick question however, if I could trouble you a bit further, how would I sort the categories in alphabetical order throughout the columns? For instance: Sort the categories alphabetically, then distribute the sorted list vertically across the columns?
privateace
If you've got a structure like mine, then look into http://php.net/ksort (I'll edit it in since it's a one liner though anyway)
Matthew Scharley
privateace
Then a simplistic algorithm would just use array_splice, or just use a for loop instead of foreach with appropriate limits. If you want to start doing this sort of length detection with that style of column, you start getting into far more advanced algorithms, especially for more than two columns.
Matthew Scharley