我目前正在制作产品页面。我需要一个显示多级别的选择框 在哪里添加产品的类别。需要像(php / mysql动态):
<option>Workstations</option>
<option>--Macs</option>
<option>--Windows</option>
<option>Software</option>
<option>--Image editing</option>
<option>----Pro</option>
<option>----Semi pro</option>
我的mysql字段是,cat_id,cat_name,cat_parent。 我有以下代码,所以有没有办法自定义它以使用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);
问候,西蒙
答案 0 :(得分:1)
这样的事情可以解决问题:
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);
}
}
}
我认为您还可以稍微简化您的查询
$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);
返工示例
以下内容允许您使用1个查询完成整个过程。由于闭包,它使用PHP 5.3。
// 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>
*/
答案 1 :(得分:1)
如果你真的想要一个选择框,你只需要用带选项的select和li标签替换ul标签。
修改后的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);
唯一的问题是链接在选择框内不起作用。我建议使用select的onchange属性,并在从选择框中选择项目时使用javascript来处理逻辑。
<强>使用Javascript:强>
function showProduct(productLink) {
//do something with the product value
// maybe something like...
location.href = productLink;
}
答案 2 :(得分:0)
我认为你可能正在寻找<optgroup>
我从未在多层中使用它,但它值得一试。