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?
4
  • 4
    Could you possibly rephrase your question? I don't understand what you're trying to do. Commented Jul 18, 2009 at 8:00
  • Display a list of categories in column format. Under each category list the subcategories. The main problem is how to split the categories equally into the columns Commented Jul 18, 2009 at 8:08
  • Column format, do you mean in a table? Commented Jul 18, 2009 at 8:25
  • Moreso using div tags with css formatting. I've added a bit more detail to the question. Thanks for your patience. Commented Jul 18, 2009 at 12:59

1 Answer 1

4

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";
}
Sign up to request clarification or add additional context in comments.

6 Comments

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
If you don't care about the column lengths being equal, then it's a trivial problem, I'll work something out soon.
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?
If you've got a structure like mine, then look into php.net/ksort (I'll edit it in since it's a one liner though anyway)
The problem is that the method above (including the ksort method) distributes the alphabetically sorted categories across the columns. Meaning A will be in column 1, then the next in the order, say B, is put in column 2, then the next goes back into the first column. What I've been looking for is very similar but will fill up each column for instance: putting A, B & C in the first column, D, E & F in the second column.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.