简体   繁体   English

如何在带有变量的codeigniter中加载视图?

[英]how to load view in codeigniter with variable?

hi i want to just load the view page using variable not by direct name 嗨,我只想使用变量而不是直接名称来加载视图页面

here is my code:- 这是我的代码:-

function load_views()
    {
      $this->load->model('test_model');
      $data=$this->test_model->getMenu();
      foreach($data as $data_menu) {
            echo $data_menu->views;
            $this->load->view($data_menu->views);
      }
    }

output : 输出:

test_view
  An Error Was Encountered Unable to load the requested file: .php

actually it takes the value from db but it did not call the view file which is present in the view folder. 实际上,它从db获取值,但没有调用view文件夹中存在的view文件。

this is my view page : 这是我的查看页面:

    <div class="panel-heading">
           <h3 class="panel-title">Dashboard</h3>
    </div>
    <br/>
        <div class="row">


            <?php
                $count = 0;
                 foreach($m as $data_menu){

                    if(($count% 4== 0))
                    {
            ?>      
                        <div class = "row"></div>
            <?php
                    }
            ?>
                 <div class="col-md-3"> 
                    <!--<a href="<?/*php echo base_url()*/?>index.php/<?php/* echo $data_menu->views*/?>">-->

                    <a href="<?php echo base_url()?>index.php/dn_login_controller/load_views">

                        <img class="img-rounded" src="" height="80" width="80" />
                         <div class="caption"> 
                            <h3><?php echo $data_menu->function_name; ?></h3>
                        </div>
                    </a>

                 </div>

            <?php
                    $count++;
                 }
            ?>


      </div>  
</div>

Now in the view i want load views dynamically but i cant... please help me..... 现在在视图中,我想动态加载视图,但我不能...请帮助我.....

If you var_dump($data) you will see that it has an empty value somewhere in the array. 如果您使用var_dump($data) ,则将在数组中某处看到一个空值。

As kumar_v said in a comment, try to check for empty values 正如kumar_v在评论中所说,请尝试检查空值

foreach($data as $data_menu) {
    if(!empty(trim($data_menu->views)))
        $this->load->view($data_menu->views);
}

This issue might be because, you cannot just directly load multiple views from single function/controller, when you just load view CI will send it to browser. 这个问题可能是因为,您不能仅从单个功能/控制器直接加载多个视图,而仅在加载视图CI时会将其发送到浏览器。 There are two other workaround for this : 还有其他两种解决方法:

  • Pass 2nd parameter NULL & 3rd parameter TRUE while loading your menu views, which will create your html as data instead of sending it to browser. 在加载菜单视图时传递第二个参数NULL和第三个参数TRUE,这将创建html作为数据,而不是将其发送到浏览器。 Refer this link : codeigniter: loading multiple views 请参阅此链接: codeigniter:加载多个视图
  • Create a view file, pass your data to that view & on that view execute for loop & load all your views. 创建一个视图文件,将数据传递到该视图,然后在该视图上执行循环并加载所有视图。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM