关于从模型调用方法的视图文件中的错误

时间:2015-09-02 08:51:57

标签: php codeigniter model

        <div class="collapse navbar-collapse" id="navbar-collapse-1">
            <ul class="nav navbar-nav">
                <?php
                $this->load->model('cms_model');
                if (isset($article_data)) {

                } else {
                    $article_data = $this->cms_model->get_end_user_cms_menu(); (this line gives error)
                }
                 foreach ($article_data as $row) { ?>
                    <li class="dropdown">  <a href='<?php echo site_url('endusers/end_cms_controller/cms_type_selected?type_id=' . $row->type_id) ?>'>
                    <?php echo $row->type_name; ?>
                        </a> </li>
                <?php } ?>

            </ul>
            <div class="nav-search-wrap">
                <input class="txt-search" type="text" name="search" placeholder="Search">
            </div>
        </div><!-- /.navbar-collapse -->

这是我的视图文件中的代码,它显示了我突出显示的行号错误。错误是&#34;致命错误:在第158行和第34行的E:\ xampp \ htdocs \ Santulan \ application \ views \ endusers \ header.php中的非对象上调用成员函数get_end_user_cms_menu();

2 个答案:

答案 0 :(得分:0)

首先,您必须在控制器中调用$this->load->model('cms_model');,而不是在您的视图中。 然后,在控制器中,不在您的视图中,您可以这样做:

    if (!isset($article_data)) {
         $article_data = $this->cms_model->get_end_user_cms_menu(); 
       }
   $data['list'] = '';
   foreach ($article_data as $row){
     $data['list'] .= "<li class=\"dropdown\">  <a href='".site_url('endusers/end_cms_controller/cms_type_selected?type_id=' . $row->type_id).">$row->type_name</a> </li>";
   }

然后,同样在您的控制器中,当您加载视图时,添加$data

$this->load->view('your_viwe', $data);

在你看来:

<div class="collapse navbar-collapse" id="navbar-collapse-1">
            <ul class="nav navbar-nav">
            <?php echo $list;?> 
            </ul>
            <div class="nav-search-wrap">
                <input class="txt-search" type="text" name="search" placeholder="Search">
            </div>
        </div>   

你很高兴。

答案 1 :(得分:0)

关于其他问题:问题出在您的控制器内部。我发布了带有解释性评论的更正版本:

 public function index(){
        $str=$this->input->post('search'); 
        $search_id=$this->cms_model->search_tag_id($str);      
        //echo  $id= $search_id; -> you cannot echo $search_id becouse it's an array: $query->result() from search_tag_id() model function.
        //$query->result() is allways an array
        //FOR DEBUGGING: when you want to see inside $query->result(), use: echo '<pre>'.print_r($query->result(), true).'</pre>';
        // instead you use:
        $id = $search_id->article_id;
        $search_data['article']=$this->cms_model->search_article($id);// function for search article
        if($search_data){
            $this->load->view('endusers/cms_view', $search_data); // this is ok, if you call for $article and $article_image inside the view
        }else{
            echo "No result found";
        }   
    }