0

是否可以在 codeigniter 活动记录中编写此查询?

$Main_Nav_Query = mysql_query("SELECT * FROM kategorie WHERE nadrad = 0 order by poradie;");

因为我找到了这个代码来创建带有子字段的菜单,并且我想将它更改为 codeigniter 代码,并且还添加了一些可能拥有无限子菜单的可能性,例如

Menu1
     Menu 1 1
     Menu 1 2
     Menu 1 3
             Menu 1 3 1
             Menu 1 3 2
             Menu 1 3 3
                       Menu 1 3 3 1
Menu 2
Menu 3

等等

这是完整的代码:

$Main_Nav_Query = mysql_query("SELECT * FROM kategorie WHERE nadrad = 0 order by poradie;");    //Query for the main items

    echo "<ul id=\"navigation-list\">\n";
    //Main items
    while($Main_Nav_Query_Results = mysql_fetch_array($Main_Nav_Query)) {
        //Set main data
        $Main_Nav_ID         = $Main_Nav_Query_Results['id'];
        $Main_Nav_Name         = $Main_Nav_Query_Results['nazov'];
        $Main_Nav_Link        = $Main_Nav_Query_Results['menu'];
        $Main_Nav_Parent    = $Main_Nav_Query_Results['nadrad'];

        //Create the list items for each main level
        echo "\t\t\t\t<li class=\"mainlevel\" id=\"\"><a >$Main_Nav_Name</a>";

            //Sub items
            $Sub_Nav_Query     = mysql_query("SELECT * FROM kategorie WHERE nadrad = '$Main_Nav_ID';");    //Query for the sub items
            while($Sub_Nav_Query_Results = mysql_fetch_array($Sub_Nav_Query)) {
                //Set Sub data
                $Sub_Nav_ID         = $Sub_Nav_Query_Results['id'];
                $Sub_Nav_Name         = $Sub_Nav_Query_Results['nazov'];
                $Sub_Nav_Link        = $Sub_Nav_Query_Results['menu'];
                $Sub_Nav_Parent        = $Sub_Nav_Query_Results['nadrad'];

                //Create the markup for the sub items (list items)
                $Sub_List = "<li class=\"sub-item\"><a >$Sub_Nav_Name</a></li>";    

                //Check if the current item has any sub items
                if($Sub_Nav_Parent == $Main_Nav_ID) {
                        echo "<ul class=\"sub-list\">";
                        echo "$Sub_List";
                        echo "</ul>";                    
                }                            
            }        
        echo "</li>\n";    //Close the sub items
    }
    echo "\t\t\t</ul>\n";        //Close the unordered list for the main items

你能帮我吗?

4

1 回答 1

0

像这样:

$this->db->where('nadrad', 0);
$this->db->order_by('poradie', 'ASC');
$rows = $this->db->get('kategorie')->result();

foreach ($rows as $row) {
    $Main_Nav_ID = $row->id;
    //etc..
}
于 2012-06-28T11:43:08.380 回答