simon
simon

Reputation: 2377

Selectbox multilevel

I am currently making an product page. I need a selectbox displaying multilevels of categories where to add the product in. Need something like (php/mysql dynamic):

<option>Workstations</option>
<option>--Macs</option>
<option>--Windows</option>
<option>Software</option>
<option>--Image editing</option>
<option>----Pro</option>
<option>----Semi pro</option>

My mysql fields are, cat_id, cat_name, cat_parent. I have the following code, so is there any way to customize it to work with a selectbox insted of ul/li?

function display_children($parent, $level) {
    $result = mysql_query("SELECT a.id, a.label, a.link, Deriv1.Count FROM `menu` a  LEFT OUTER JOIN (SELECT parent, COUNT(*) AS Count FROM `menu` GROUP BY parent) Deriv1 ON a.id = Deriv1.parent WHERE a.parent=" . $parent);
    echo "<ul>";
    while ($row = mysql_fetch_assoc($result)) {
        if ($row['Count'] > 0) {
            echo "<li><a href='" . $row['link'] . "'>" . $row['label'] . "</a>";
            display_children($row['id'], $level + 1);
            echo "</li>";
        } elseif ($row['Count']==0) {
            echo "<li><a href='" . $row['link'] . "'>" . $row['label'] . "</a></li>";
        } else;
    }
    echo "</ul>";
}

display_children(0, 1);

Regards, Simon

Upvotes: 1

Views: 1929

Answers (3)

CAMason
CAMason

Reputation: 1122

Something like this would do the trick:

function display_children($parent, $level) {
    $result = mysql_query("SELECT a.id, a.label, a.link, Deriv1.Count FROM `menu` a  LEFT OUTER JOIN (SELECT parent, COUNT(*) AS Count FROM `menu` GROUP BY parent) Deriv1 ON a.id = Deriv1.parent WHERE a.parent=" . $parent);

    $indent = str_repeat('-', $level);
    $tpl = '<option value="%s">%s</option>';

    while ($row = mysql_fetch_assoc($result)) {

        echo sprintf($tpl, $row['link'], $indent . $row['label']);

        if ($row['Count'] > 0) {
            display_children($row['id'], $level + 1);
        }
    }
}

I think you could also simplify your query slightly

    $result = mysql_query("SELECT a.id, a.label, a.link, (SELECT COUNT(id) FROM `menu` WHERE parent = a.id) AS Count FROM `menu` a  WHERE a.parent=" . $parent);

Reworked example

The following would allow you to complete the whole process with 1 query. It uses PHP 5.3 due to closures.

// I assume this is how your data comes out of MySQL
$items = array(
    array('id' => 1, 'label' => 'Item One',  'link' => 'Link One',    'parent' => 0),
    array('id' => 2, 'label' => 'Child One', 'link' => 'Link Two',    'parent' => 1),
    array('id' => 3, 'label' => 'Child Two', 'link' => 'Link Three',  'parent' => 1),
    array('id' => 4, 'label' => 'Item Two',  'link' => 'Link Four',   'parent' => 0),
    array('id' => 5, 'label' => 'Child One',  'link' => 'Link Five',  'parent' => 4),
    array('id' => 6, 'label' => 'Child Two',  'link' => 'Link Six',   'parent' => 4),
    array('id' => 7, 'label' => 'Child Three',  'link' => 'Link Six',   'parent' => 6),
    array('id' => 8, 'label' => 'Child Four',  'link' => 'Link Six',   'parent' => 6),
);

// Loop using references to build a tree
$childs = array();
foreach($items as &$item)
{
    $childs[$item['parent']][] = &$item;
}

unset($item);

foreach($items as &$item)
{
    if(isset($childs[$item['id']]))
    {
        $item['children'] = $childs[$item['id']];
    }
}
// We now have a tree with 'children' key set on each node that has children
$tree = $childs[0];

// Prepare a template and recursive closure (note reference on &$print)
$tpl = '<option value="%s">%s %s</option>';
$print = function($item, $indent = '') use (&$print, $tpl)
{
    echo sprintf($tpl, $item['link'], $indent, $item['label']) . "\n";

    if(isset($item['children']))
    {
        foreach($item['children'] as $child)
        {
            $print($child, $indent . '-');
        }
    }
};

echo '<select onchange="showProduct(this.value);">';
// Call the function for each top-level node
foreach($tree as $row)
{
    $print($row);
}
echo '</select>';

/*
<select onchange="showProduct(this.value);">
<option value="Link One"> Item One</option>
<option value="Link Two">- Child One</option>
<option value="Link Three">- Child Two</option>
<option value="Link Four"> Item Two</option>
<option value="Link Five">- Child One</option>
<option value="Link Six">- Child Two</option>
<option value="Link Six">-- Child Three</option>
<option value="Link Six">-- Child Four</option>
</select>
*/

Upvotes: 1

ae14
ae14

Reputation: 411

If a select box is all you really want you just need to replace the ul tags with select and li tags with option.

Modified PHP:

function display_children($parent, $level) {
    $result = mysql_query("SELECT a.id, a.label, a.link, Deriv1.Count FROM `menu` a  LEFT OUTER JOIN (SELECT parent, COUNT(*) AS Count FROM `menu` GROUP BY parent) Deriv1 ON a.id = Deriv1.parent WHERE a.parent=" . $parent);
    echo '<select onchange="showProduct(this.value);">';
    while ($row = mysql_fetch_assoc($result)) {
        if ($row['Count'] > 0) {
            echo "<option value='" . $row['link'] . "'>" . $row['label'];
            display_children($row['id'], $level + 1);
            echo "</option>";
        } elseif ($row['Count']==0) {
            echo "<option value='" . $row['link'] . "'>" . $row['label'] . "</option>";
        } else;
    }
    echo "</select>";
}

display_children(0, 1);

The only problem is that the links will not work inside a select box. I would suggest looking into using the onchange attribute of select and using javascript to handle the logic when an item is chosen from the select box.

Javascript:

function showProduct(productLink) {
    //do something with the product value
    // maybe something like...
    location.href = productLink;
}

Upvotes: 1

Martin Lyne
Martin Lyne

Reputation: 3065

I think you may be looking for <optgroup> I've never used it with multiple tiers, but it's worth a shot.

http://www.htmldog.com/reference/htmltags/optgroup/

Upvotes: 0

Related Questions