如何设置codeigniter多级菜单的样式?

时间:2015-07-14 12:17:09

标签: php css codeigniter

我想用codeIgniter创建一个多级菜单。

我的表结构是这样的:

  

id - category - parent_id

我在我的模型中使用此代码:

function all_category_ordered($level = 0, $prefix = '<li>') {
    $rows = $this->db
        ->select('id,category,parent_id')
        ->where('parent_id', $level)
        ->order_by('id','asc')
        ->get('category')
        ->result();

    $category = null;

    if (count($rows) > 0) {

        foreach ($rows as $row) {
            $category .= $prefix . $row->category . "\n";
            // Append subcategories
            $category .= "</li>".$this->all_category_ordered($row->id, $prefix . '*') ;
        }
    }
    return $category;
}

它工作正常,代码的输出是这样的:

<li>menu 1</li>
<li>menu 2</li>
<li>menu 3</li>
<li>*submenu 1</li>
<li>menu 4</li>
<li>*submenu 2</li>

但我想将模型更改为ulli正确的顺序,以便正确设置样式。

首选订单:

<ul>
<li>menu 1</li>
<li>menu 2</li>
<li>menu 3
<ul>
<li>*submenu 1</li>
</ul>
</li>
<li>menu 3
<ul>
<li>*submenu 1</li>
</ul>
</li>
</ul>

任何帮助都将不胜感激。

2 个答案:

答案 0 :(得分:0)

您正在使用递归功能 - 这是好的。你应该坚持使用这种方法,但是你不能控制什么时候应该打开和关闭特定的ul以及那些缺失的东西。

下面你会发现我的非常古老的废话,它会给你一个关于控制每个级别打开/关闭列表的过程的提示。

<?php

$query = mysql_query('SELECT menu.id, menu.id_parent, menu.link, menu.alt, library.title FROM library library, menu menu WHERE menu.link = library.id ORDER BY menu.level ASC');
while($row = mysql_fetch_array($query))
{

    $menu_array[] = array('id'=>$row['id'], 'id_parent'=>$row['id_parent'], 'link'=>$row['link'], 'alt'=>$row['alt'], 'title'=>$row['title']);
}

function menu($id) 
{   
    global $menu_array;

    $has_childs = false;
    $result = array();

    // nothing if empty
    if (empty($menu_array))
    {
        return;
    }

    foreach ($menu_array as $value)
    {
        if ($value['id_parent'] == $id)
        {
            $result[] = $value;
        }
    }

    foreach ($result as $value)
    {
        if ($has_childs === false)
        {
            $has_childs = true;

            if ($value['id_parent'] == 0)
            {
                echo ('<ul id="root">'); // root level
            }
            else
            {
                echo ('<ul>'); // sub levels
            }
        }

        echo ('<li><a href="#">'.$value['title'].'</a>');

        menu($value['id']);

        echo ('</li>');
    }

    if ($has_childs === true)
    {
        echo ('</ul>');
    }
}

menu(0);

再次,调整它。删除查询并将其替换为CI模型返回的内容。看看结构是否相似。这是为您提供示例函数menu()

编辑:

这是数据库结构。

CREATE TABLE IF NOT EXISTS `menu` (
  `id` int(11) NOT NULL,
  `id_parent` int(11) NOT NULL DEFAULT '0',
  `link` int(11) NOT NULL DEFAULT '0',
  `level` int(11) NOT NULL DEFAULT '0',
  `alt` varchar(255) COLLATE utf8_polish_ci NOT NULL DEFAULT ''
) ENGINE=MyISAM AUTO_INCREMENT=74 DEFAULT CHARSET=utf8 COLLATE=utf8_polish_ci;

--
-- Zrzut danych tabeli `menu`
--

INSERT INTO `menu` (`id`, `id_parent`, `link`, `level`, `alt`) VALUES
(66, 0, 197, 3, 'Strona główna'),
(3, 2, 199, 1, 'Kanał RSS'),
(4, 2, 200, 2, 'Archiwum newsów'),
(5, 0, 24, 6, 'Depeche Mode'),
(6, 5, 1, 1, 'Biografia grupy'),
(7, 10, 26, 1, 'Biografia'),
(8, 15, 94, 1, 'Biografia'),
(9, 19, 107, 1, 'Biografia'),
(10, 5, 25, 2, ''),
(11, 10, 27, 2, 'Albumy'),
(12, 10, 29, 3, 'Single'),
(13, 10, 30, 4, 'Video'),
(14, 10, 31, 5, 'Teksty'),
(15, 5, 93, 3, ''),
(16, 15, 95, 2, 'Albumy'),
(17, 15, 102, 3, 'Single'),
(18, 15, 103, 4, 'Teksty'),
(19, 5, 104, 4, ''),
(20, 19, 108, 2, 'Wytwórnia płytowa Toast Hawaii'),
(21, 19, 109, 3, 'Tost Hawajski'),
(22, 5, 105, 5, ''),
(23, 22, 111, 1, 'Biografia'),
(24, 22, 110, 2, 'Odejście z Depeche Mode'),
(26, 22, 221, 3, 'Albumy'),
(27, 22, 219, 4, 'Single'),
(28, 22, 220, 5, 'Teksty'),
(29, 5, 106, 6, ''),
(30, 29, 112, 1, 'Biografia'),
(31, 29, 113, 2, ''),
(32, 0, 55, 7, 'Dyskografia'),
(33, 32, 164, 1, 'Albumy'),
(34, 32, 165, 2, 'Single'),
(35, 32, 166, 3, 'Video'),
(36, 0, 17, 8, 'Teksty utworów'),
(37, 0, 53, 9, 'Trasy koncertowe'),
(38, 37, 76, 1, ''),
(39, 37, 77, 2, ''),
(40, 37, 78, 3, ''),
(41, 37, 79, 4, ''),
(42, 37, 80, 5, ''),
(43, 37, 81, 6, ''),
(44, 37, 82, 7, ''),
(45, 37, 83, 8, ''),
(46, 37, 84, 9, ''),
(47, 37, 85, 10, ''),
(48, 37, 86, 11, ''),
(49, 37, 87, 12, ''),
(50, 37, 88, 13, ''),
(51, 37, 89, 14, ''),
(52, 37, 90, 15, ''),
(53, 37, 91, 16, ''),
(54, 0, 201, 10, 'Ciekawostki'),
(55, 0, 202, 11, 'Fani'),
(56, 0, 203, 12, 'Forum'),
(57, 0, 42, 13, ''),
(58, 0, 196, 14, ''),
(59, 0, 41, 15, ''),
(60, 59, 204, 1, 'Kontakt'),
(61, 59, 277, 2, ''),
(62, 37, 282, 17, ''),
(63, 62, 280, 1, ''),
(67, 0, 198, 5, 'Aktualności'),
(68, 67, 199, 1, 'Kanał RSS'),
(69, 67, 200, 2, 'Archiwum newsów'),
(70, 62, 289, 2, ''),
(71, 55, 292, 1, ''),
(72, 56, 293, 1, '');

当然,随意使用它。 :) 就像我说的。这是非常古老的代码,但它完全正常运行。您可以看到完全粘贴的示例here。 Id和Id_parent - 这两个保持彼此之间的关系,更改链接到你的href只是你将放在href内的东西,level是深度,alt是你的菜单条目的标题。所以你有一些东西需要清理,但它可以100%工作。在CI中创建一个控制器,将查询部分移动到模型中的新方法,然后就可以了。生成列表后,您可以在视图中设置样式。 :)

答案 1 :(得分:0)

我使用了这个PHP代码并更改为CodeIgniter MVC

<?php
function has_children($rows, $id) {
    foreach ($rows as $row) {
        if ($row['parent_id'] == $id) {
            return true;
        }
    }
    return false;
}
function build_menu($rows, $parent = 0) {
    $result = "<ul>";
    foreach ($rows as $row) {
        if ($row['parent_id'] == $parent) {
            $result .= "<li>{$row['category']}";
            if (has_children($rows, $row['id'])) {
                $result .= build_menu($rows, $row['id']);
            }
            $result .= "</li>";
        }
    }
    $result .= "</ul>";
    return $result;
}
$category = array(
        array('id' => 1, 'parent_id' => 0, 'category' => 'category 1'),
        array('id' => 2, 'parent_id' => 0, 'category' => 'category 2'),
        array('id' => 3,  'parent_id' => 1, 'category' => 'sub category'),
        array('id' => 4,  'parent_id' => 2, 'category' => 'sub category'),
        array('id' => 5,  'parent_id' => 1, 'category' => 'sub category'),
        array('id' => 6,  'parent_id' => 5, 'category' => 'sub category'),
);
?>

<!DOCTYPE html>
<html>
    <head>
        <title>menu</title>
    </head>
    <body>
        <?php echo build_menu($category, 0);?>
    </body>
</html>