如何在php codeigniter中逐个打印每个行值

时间:2016-03-23 08:48:34

标签: php sql codeigniter

我试图获取数据库的所有行,它应该循环自己以获得预期的输出。输出的图像是[在此输入图像描述] [1]

代码如下: 在模型中:

function get_menugroup() 
 {
    $data=array();
    $sql = $this->db->query("SELECT MenuName FROM MenuGroup;");
    $menu_res = $sql->result();   
        if($sql -> num_rows() > 0 ) 
        {   
                return $menu_res;
        }
        else{
            echo "Nothing to process";
        }

}`

在控制器中

public function menugroupname()
{

        $menugroups=$this->UserRight_model->get_menugroup(); 
        if ($menugroups){       
        $data['menu'] = $menugroups;

        //$data['count'] = count($data['menu']);
        $this->load->view('UserRight_view',$data);      
        }   
}

在视图中:

<ul class="collapsible" data-collapsible="accordion">
                    <li>
                    <?php 
                    if(is_array($menu)){
                     foreach($menu as $menus){
                     $menulist = $menus ->MenuName; 
                     //$menulist = $menus['MenuName'];
                     $fun = explode(",",$menus ->MenuName); 
                                            }
                     $no_menu = count($menu);
                      //echo $no_menu;
                     for($i=0; $i<$no_menu ; $i++){ 
                     ?>
                     <div class="collapsible-header"><i class="material-icons">place</i><?php echo $menulist ?></div>
                      <div class="collapsible-body"><p>Submenu.</p></div>
                    </li>
                    <?php }} else { echo "Wrong way";} ?>


            </ul>

并提前致谢

1 个答案:

答案 0 :(得分:0)

试试这个。

<ul class="collapsible" data-collapsible="accordion">
    <li>
    <?php 
    if(($menu)){
     foreach($menu[0] as $menus){
     $menulist = $menus->MenuName; 
     $fun = explode(",",$menus ->MenuName);                             }
     $no_menu = count($menu);
     for($i=0; $i<$no_menu ; $i++){ 
     ?>
     <div class="collapsible-header"><i class="material-icons">place</i><?php echo $menulist ?></div>
      <div class="collapsible-body"><p>Submenu.</p></div>
    </li>
    <?php }} else { echo "Wrong way";} ?>
</ul>

希望这能帮到你!