为函数控制器中的foreach()codeigniter3提供的无效参数

时间:2015-11-09 02:55:45

标签: php ajax codeigniter

对不起我想问的是为什么突然为foreach()CodeIgniter提供的无效参数当我尝试了benerin但仍然像这样.. 最初我想在CodeIgniter中创建一个可嵌套的.. 这是我在控制器中的脚本的一个例子

function load_menus() {
    $this->load->model("webadmin/we_model");
    $data["menus"] = $this->me($this->we_model->get_menus());
    $this->load->view("webadmin/loadmenu.php",$data);
}
private function me($menus = array()) {
        $html = '';

        // Build all application menus in a tree format
        foreach($menus as $menu)
        {
            // Check if this menu has children
            $count_menu_children = count(get_value($menu, 'children'));

            // Build a single line menu
            $html .= '<li id="menu-item-' . get_value($menu, 'id_menu') . '" class="dd-item dd3-item" data-id="' . get_value($menu, 'id_menu') . '" order="' . get_value($menu, 'order_') . '">';

            $html .= '<div class="dd-handle dd3-handle"></div>';

            $html .= '<div class="dd3-content">';

                $class = get_value($menu, 'dtt_inative') != '' ? 'text-error' : '';


                $label = get_value($menu, 'url_img') == '' && get_value($menu, 'label') == '' ? '[NO NAME]' : lang(get_value($menu, 'label'));

                $html .= '<a href="javascript:void(0)" class="menu-label ' . $class . '" data-toggle="modal" data-target="#modal-menu-' . get_value($menu, 'id_menu') . '">' . $label . '</a>';

                $html .= '<i class="text-success fa fa-fw fa-check-circle" style="display: none; margin-left: 5px"></i>';

                $html .= '<a href="javascript:void(0)" class="menu-delete pull-right hidden"><i class="fa fa-times fa-fw"></i></a>';

            $html .= '</div>';

            // If current menu has children items, then build all again
            if($count_menu_children > 0)
                $html .= '<ol class="dd-list">' . custom_menu_tree(get_value($menu, 'children')) . '</ol>';

            $html .= '</li>';
        }

        return $html;
    }

我的模特

public function get_menus()
    {
        $sql = "SELECT m.*,
                       IFNULL(parent, 0) AS id_menu_parent
                  FROM m_a m";
        return $this->db->query($sql)->result_array();
    }

我的观点

<?php if( count($menus) > 0 ) { ?>
<div class="dd"><ol class="dd-list"><?php echo $menus; ?></ol></div>
<?php } else { echo message('info', '', lang('There is no menu items for this group. Click on button New menu above to start adding a new menu.')); } ?>

4 个答案:

答案 0 :(得分:0)

在此

private function me($menus = array()) {
        $html = '';

        // Build all application menus in a tree format
        foreach($menus as $menu)
        {

您忘记调用包含菜单数据的模型方法。在foreach循环之前添加此项。

$this->model_name->get_menus();

喜欢这个

private function me($menus = array()) {
    $html = '';

    $this->model_name->get_menus();
    foreach($menus as $menu)
    {

答案 1 :(得分:0)

在将$menus传递给foreach()

之前,请检查if(count($menus)){ foreach($menus as $menu) { // your code here. } } return $html;//keep this after if(). 中是否有值
FREQ=WEEKLY;BYDAY=MO,TU

答案 2 :(得分:0)

我想你必须先尝试这个

foreach($menus as $menu){}

检查是否

if(!empty($menus)){
     foreach($menus as $menu){
       your code;
     }
}
else{
   your code;
}

答案 3 :(得分:0)

好的,谢谢你的帮助,我解决了,我忘记了

{{1}}