dongliang1941 2015-03-03 13:43
浏览 14
已采纳

来自codeigniter库文件夹的动态菜单无法返回

I create a dynamic menu in the folder of library in codeigniter framwork.

class Left_menu {

private $ci; 

function __construct()
{
    $this->ci =& get_instance();    // get a reference to CodeIgniter.
}

function get_company () 
    {
         $html_out = '';

         $company = $this->ci->db->query("select * from perusahaan");

         $html_out  .= "<ul class='sub_list'>";

         foreach ($company->result() as $row)
            {
                $id = $row->id; 
                $name = $row->name; 
                $location = $row->location;
                    $html_out .= "<a href='".site_url("perusahaan_controller/detailPersahaan/".$id."")."'>";
                    $html_out .= "<li>".$name."</li>";
                    $html_out .= "</a>";
            }

         $html_out  .= "</ul>";

         $html = $html_out;
         //print_r ($html); 

         return $html;

    }

 }

And the in the view I call it:

<?php $this->left_menu->get_company(); ?>                   

However, it doesn't show the menu at all. It does if only I print it, //print_r ($html); , and the weird is it printed the menu as how I want to return it. (It looks like it turns the return function into print_r).

  • 写回答

1条回答 默认 最新

  • duanleiming2014 2015-03-03 14:43
    关注

    You don't need to create a library for this.

    Simply use a Model to fetch the data and inject your sidebar into your current controller.

    You can do this by creating a partial view called sidebar, and pass it some data from the model, then insert it into your current view.

    Controller

    class Controller extends CI_Controller
    {
        public function __construct()
        {
            parent::__construct();
        }
    
        public function index()
        {
            $this->load->model('mymodel');
    
            $data = $this->MyModel->get_data();
    
            // call the sidebar view and pass it some data from model
            // at this point the view is in the buffer so it can be manipulated before final output.
            $sidebar = $this->load->view('sidebar', array(
                'data'  =>  $data
            ), true);
    
            return $this->load->view('index', array(
                'sidebar'  =>  $sidebar
            ));        
        }
    }
    

    Model

    class MyModel extends CI_Model
    {
        public function __construct()
        {
            parent::__construct();
        }
    
        public function get_data()
        {
            $query = $this->db->get('perusahaan');
    
            return ( $query->num_rows() > 0 ) ? $query->result() : false;
        }
    }
    

    views/sidebar

    <ul class="sublist">
        <?php foreach($data as $_data): ?>
            <li id="<?php echo $_data->id;?>">
                <a href="<?php echo ".site_url('perusahaan_controller/detailPersahaan/')."/".$_data->id.""><?php echo xss_clean($_data->name);?></a>
            </li>
        <?php foreach; ?>
    </ul>
    

    views/index

    <aside class="sidebar">
        <?php echo $sidebar; ?>
    </aside>
    
    <div>
        //... content
    </div>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 Vue3 大型图片数据拖动排序
  • ¥15 划分vlan后不通了
  • ¥15 GDI处理通道视频时总是带有白色锯齿
  • ¥20 用雷电模拟器安装百达屋apk一直闪退
  • ¥15 算能科技20240506咨询(拒绝大模型回答)
  • ¥15 自适应 AR 模型 参数估计Matlab程序
  • ¥100 角动量包络面如何用MATLAB绘制
  • ¥15 merge函数占用内存过大
  • ¥15 使用EMD去噪处理RML2016数据集时候的原理
  • ¥15 神经网络预测均方误差很小 但是图像上看着差别太大