Sturm
Sturm

Reputation: 759

How to create a hierarchical list from data

With PHP, I need to create a three-level-deep navbar. I have data like this:

Data returned from SQL call to a database

And I need it to be organized like so:

Napkins --Colored Napkins ----American Tradition --White Beverage Napkins ----American Digital ----American Tradition ----American Hi-Speed --White Luncheon Napkins ----American Digital ----American Tradition ----American Hi-Speed --White Dinner Napkins ...(etc)... Plates --Eco-Plates ----American Tradition --Plastic Trays ...(etc)...

Just using a relatively simple <ul> structure with <li> and <a>. Clearly a loop is needed to go through the rows of data. And I know that I'd need to do lots of "testing" to see if the "current" category/subcategory/method matches the one from the previous iteration of the loop.

But I'm having trouble getting the <ul> and <li> tags to close at the correct locations and to not show the print method tier when there is only one print method for a given category/subcategory. Or when there is no print method (sometimes it can be null, such as for plastic utensils). How can I set this up?

Edit

The code I have so far, which almost works, but is very clunky ($items is the data from the database):

function addMethodMenuItem($result, $short_cat_name, $short_subcat_name, $short_method_name, $this_method_name)
{
    $result .= "<li>";
    $result .= "<a";
    $result .= " href=\"product.php?category={$short_cat_name}&subcategory={$short_subcat_name}&printMethod={$short_method_name}\"";
    $result .= " target=\"_self\"";
    $result .= " title=\"{$this_method_name}\">";
    $result .= $this_method_name;
    $result .= "</a>";
    $result .= "</li>";
    return $result;
}

function addSubMenuItem($item, $previous_cat_name, $previous_subcat_name, $result, $newCat)
{
    $this_cat_name = $item["category_name"];
    $this_subcat_name = $item["subcategory_name"];
    $this_method_name = $item["method_name"];
    $short_cat_name = $item["category_short"];
    $short_subcat_name = $item["subcategory_short"];
    $short_method_name = $item["method_short"];

    if ($this_subcat_name != $previous_subcat_name || $this_cat_name != $previous_cat_name) { // We have to create a new "subcategory" menu item
        if ($previous_subcat_name != null && !$newCat) { // if this isn't the first subcategory menu item of the category.
            $result .= "</ul></li>";
        }
        $result .= "<li>";
        $result .= "<a class=\"ajxsub\"";
        $result .= " href=\"#\">";
        $result .= $this_subcat_name;
        $result .= "</a>";
        $result .= "<ul>";
    }

    $result = addMethodMenuItem($result, $short_cat_name, $short_subcat_name, $short_method_name, $this_method_name);
    return $result;
}

function printMenu(array $items, $previous_cat_name, $result)
{
    // $result .= "<ul>";
    $previous_subcat_name = null;
    foreach ($items as $item) {
        $newCat = false;
        $this_cat_name = $item["category_name"];
        $this_subcat_name = $item["subcategory_name"];
        if ($this_cat_name != $previous_cat_name) {
            if ($previous_cat_name != null) { // if this isn't the very first top-level menu item.
                $result .= "</ul></li>";
                $result .= "</ul></li>";
                $newCat = true;
            }
            $result .= "<li>";
            $result .= "<a class=\"ajxsub\"";
            $result .= " href=\"#\">";
            $result .= $this_cat_name;
            $result .= "</a>";
            $result .= "<ul>";
        }
        $result = addSubMenuItem($item, $previous_cat_name, $previous_subcat_name, $result, $newCat);
        $previous_subcat_name = $this_subcat_name;
        $previous_cat_name = $this_cat_name;
    }
    $result .= "</ul></li>";
    // $result .= "</ul>";

    return $result;
}

Upvotes: 0

Views: 56

Answers (1)

wordragon
wordragon

Reputation: 1357

Give this a whirl - assumes you have the data already sorted (else sort it first):

$flds = ['category_name', 'subcategory_name', 'method_name'];
$lval = ['it will never be this'];
$result = "";
$start = true;
foreach ($items as $item) {
    if ($start) {
        $result .= "<ul>";
        $first = $start = false;
    } else $first = true;
    foreach ($flds as $k=>$val) {
        if ($item[$val] != $lval[$k]) {
            $result .= genhtml($k, $item[$val], $first);
            $first = false;
            $lval[$k] = $item[$val];
            $lval[$k+1] = '';   // Don't care if this goes over the max
        }
    }
}
if (!$start) $result .= "</ul></li></ul></li></ul>\n";
echo $result;

function genhtml($level, $value, $first) {
    switch ($level) {
        case 0:
            $close = $first ? "</ul></li></ul></li>" : "";
            return "{$close}<li><a href=\"#\" class=\"level0\">{$value}</a><ul>\n";
        case 1:
            $close = $first ? "</ul></li>" : "";
            return "  {$close}<li><a href=\"#\" class=\"level1\">{$value}</a><ul>\n";
        case 2:
            return "    <li><a href=\"#\" class=\"level2\">{$value}</a></li>\n";
        default:
            throw new Exception("I don't know how to do '{$level}'");
    }
}

The code above (with some of your data) produces:

<ul><li><a href="#" class="level0">Napkins</a><ul>
  <li><a href="#" class="level1">Colored Napkins</a><ul>
    <li><a href="#" class="level2">American Tradition</a></li>
  </ul></li><li><a href="#" class="level1">White Beverage Napkins</a><ul>
    <li><a href="#" class="level2">American Digital</a></li>
    <li><a href="#" class="level2">American Tradition</a></li>
    <li><a href="#" class="level2">American Hi Speed</a></li>
  </ul></li><li><a href="#" class="level1">White Luncheon Napkins</a><ul>
    <li><a href="#" class="level2">American Digital</a></li>
    <li><a href="#" class="level2">American Tradition</a></li>
    <li><a href="#" class="level2">American Hi Speed</a></li>
  </ul></li><li><a href="#" class="level1">White Dinner Napkins</a><ul>
    <li><a href="#" class="level2">American Digital</a></li>
    <li><a href="#" class="level2">American Tradition</a></li>
    <li><a href="#" class="level2">American Hi Speed</a></li>
</ul></li></ul></li><li><a href="#" class="level0">Plates</a><ul>
  <li><a href="#" class="level1">Eco Plates</a><ul>
    <li><a href="#" class="level2">American Tradition</a></li>
</ul></li></ul></li></ul>

(Not pretty, but I think its right).

Also will ignore duplicate records (maybe not what you want?). If you need other data from your record to produce the html, pass $item to genhtml as well.

Upvotes: 2

Related Questions