Codeigniter分页错误

时间:2014-07-31 14:13:17

标签: codeigniter pagination

我正在我的页面中进行分页,但我收到这样的错误。

  

遇到PHP错误

     

严重性:注意

     

消息:未定义的属性:Home :: $ login_model

     

文件名:controllers / home.php

     

行号:41

     

致命错误:在非对象中调用成员函数getDet()   C:\ XAMPP \ htdocs中\毕加索\ new_design \ photo_frame \应用\控制器\ home.php   在第41行

这是我的代码

型号:

function getDet(){
$query_str = "SELECT * FROM frame";

$result = $this->db->query($query_str);
return $result;

}

控制器:

public function frame1()
    {
    $things = $this->login_model->getDet();
    $total_rows = $things->num_rows();

    $this->load->library('pagination');
    $config['base_url'] = base_url() . 'home/';
    $config['total_rows'] = '$total_rows';
    $config['per_page'] = '1';

    $this->pagination->initialize($config);
    $this->view_data['pagination'] = $this->pagination->create_links();
    $this->view_data['things'] = $things;

    $this->load->model('login_model');
    $data1['wood']=$this->login_model->getDetails('wood');
    $this->load->view('frame1',$data1);
}

查看:

<div>

    <?php if($wood!=""||$wood!=null){foreach($wood as $row){  ?>
<div class="frm1" style="border:1px solid rgb(204, 204, 204); background-color: rgb(244, 244, 244);list-style-type:none; float:left; display:inline; padding:20px;">
<img src="<?php echo base_url()."uploads/frame/icon/".$row['iconimage'];?>" style="cursor:pointer" class="changemount" data-id="<?php echo base_url()."uploads/frame/".$row['frameimage'];?>" 
data-price="<?php echo $row['price'];?>" data-thick="<?php echo $row['thickness'];?>" data-change="0" data-w="515" data-h="356" data-ch="385" data-cw="554" /></div>
<?php }}  ?>

    </div>
    <?php echo $pagination; ?>

    </div>

2 个答案:

答案 0 :(得分:1)

在使用之前,您必须加载模型。

public function frame1() {
    $this->load->model('login_model'); //You have to load this model, before use it
    $things = $this->login_model->getDet(); $total_rows = $things->num_rows();

    $this->load->library('pagination');
    $config['base_url'] = base_url() . 'home/';
    $config['total_rows'] = '$total_rows';
    $config['per_page'] = '1';

    $this->pagination->initialize($config);
    $this->view_data['pagination'] = $this->pagination->create_links();
    $this->view_data['things'] = $things;


    $data1['wood']=$this->login_model->getDetails('wood');
    $this->load->view('frame1',$data1);
}

答案 1 :(得分:0)

undefined login_model,表示您在不加载模型的情况下调用模型,将其添加到控制器中的_construct

$this->load->model("model_name");

并且在您的视图中,您错过了一些重要的事情,要在循环中执行您的代码,请执行以下代码

//add the ->result() in your var $wood
foreach($wood->result() as $row)

这将解决您的错误