我在MySQL数据库中有categories
表。
看起来像这样;
categories Table Image
我有一个用于制作HTML菜单的递归函数的功能。但是,我被困了3-4个小时。这是我在Codeigniter模型中的代码;
public function deneme($parent_id = 0, $sub_mark = 0) {
$this->db->select('*');
$this->db->from('categories');
$this->db->where('parent_id = ' . $parent_id);
$this->db->order_by('id', 'ASC');
$query = $this->db->get();
if($query->num_rows() > 0) {
foreach($query->result_array() as $row) {
echo '
<li class="active">
<a href="index.html">' . $row['name'] . '</a>
</li>
';
$sub_mark++;
$this->deneme($row['id'], $sub_mark, $str);
}
}
}
所以,它只能做到这一点;
<li class="active">
<a href="index.html">Anasayfa</a>
</li>
<li class="active">
<a href="index.html">Uygulamalar</a>
</li>
<li class="active">
<a href="index.html">Cilt Bakımı</a>
</li>
<li class="active">
<a href="index.html">Medikal Cilt Bakımı</a>
</li>
<li class="active">
<a href="index.html">Innofacial İle Bakım</a>
</li>
.
.
.
但是,我想这样做;
<li class="active">
<a href="index.html">Anasayfa</a>
</li>
<li class="active">
<a href="index.html">Uygulamalar</a>
<ul>
<li>
<a href="index.html">Cilt Bakımı</a>
<ul>
<li>
<a href="index.html">Medikal Cilt Bakımı</a>
</li>
<li>
<a href="index.html">Innofacial Ile Bakım</a>
</li>
<li>
<a href="index.html">Jetpeel Ile Bakım</a>
</li>
</ul>
</li>
</ul>
</li>
.
.
.
我该怎么做?谢谢!
答案 0 :(得分:1)
您缺少ul
标签以使列表嵌套。另外,我添加了if
语句以确保只有顶级列表具有活动的类。
if($query->num_rows() > 0) {
echo '<ul>';
foreach($query->result_array() as $row) {
if ($parent_id == 0) {
echo '<li class="active">';
} else {
echo '<li>';
}
echo '<a href="index.html">' . $row['name'] . '</a> </li> ';
$sub_mark++;
$this->deneme($row['id'], $sub_mark, $str);
}
echo '</ul>';
}