当我试图在codeigniter中创建分页时。我收到一个错误调用未定义函数初始化null
class Admin_Controller extends CI_Controller
{
function __construct()
{
parent::__construct();
$this->load->model('admin_model');
$this->load->library('pagination');
}
function index()
{
$this->load->view('registration');
}
public function manage()
{
//$this->session->set_userdata($active);
$config = array();
$config["base_url"] = site_url('admin_controller/manage');
$config["total_rows"] = $this->db->get('entries_new') ->num_rows();
$config["per_page"] = 2;
$config["uri_segment"] = 3;
$config["num_links"] = 3;
$config['prev_link'] = 'Previous';
$config['next_link'] = 'Next';
$config['last_link'] = 'Last';
//echo "<pre>";die(print_r($config));
$this->pagination->initialize($config);
$page = ($this->uri->segment(3)) ? $this->uri->segment(3) : 0;
$data['pagination'] = $this->pagination->create_links();
$data['ResAre']=$this->admin_model->get_data($config["per_page"],$page);
$this->load->view('adminpage',$data);
}
function register()
{
if($this->input->post('register'))
{
$this->admin_model->insert_data();
redirect(base_url('index.php/login_controller'));
}
}
function update()
{
if(isset($_REQUEST['editid']) && $_REQUEST['editid']!='' && $_REQUEST['editid']!=0)
{
$data['ResAre']=$this->admin_model->update();
$this->load->view('updatepage',$data);
}
}
function updated()
{
if(isset($_POST['update']))
{
$this->admin_model->updated();
redirect(base_url('index.php/admin_controller/manage'));
}
}
function delete()
{
if(isset($_REQUEST['deleteid']) && $_REQUEST['deleteid']!='' && $_REQUEST['deleteid']!=0)
{
// load model
$this->admin_model->delete();
redirect(base_url('index.php/admin_controller/manage'));
}
}
}
这里是我得到错误的代码,即函数初始化null但我已经在构造函数中加载库。请给我解决方案我错了
此处为型号代码
class Admin_Model extends CI_Model
{
public function __construct()
{
parent::__construct();
}
public function get_data($limit , $offset)
{
$query = $this->db->select('*') ->from('entries_new') ->limit($limit) ->offset($offset) ->get();
//print_r($query);die;
return $query->result();
}
public function insert_data()
{
$name = $this->input->post('name');
//echo $name;die;
$email = $this->input->post('email');
$password = $this->input->post('password');
$mobile = $this->input->post('mobile');
$this->db->select('*');
$this->db->from('entries_new');
$this->db->where('name',$name);
$query = $this->db->get();
if($query->num_rows() == 0)
{
$data = ['name'=> $name ,'email'=>$email , 'password'=>$password ,'mobile'=>$mobile];
$this->db->insert('entries_new',$data);
}
else{ return false ;}
}
public function update()
{
$id=$_REQUEST['editid'];
$query = $this->db->select('*') ->from('entries_new') ->where('id', $id) ->get();
return $result = $query->result();
}
public function updated()
{
$id=$this->input->post('upid');
$name=$this->input->post('name');
$email=$this->input->post('email');
$password=$this->input->post('password');
$mobile=$this->input->post('mobile');
$data=array('name'=>$name, 'email'=>$email, 'password'=>$password, 'mobile'=>$mobile);
$this->db->where('id',$id);
$this->db->update('entries_new',$data);
return true;
}
function delete()
{
$id=$_REQUEST['deleteid'];
$this->db->where('id', $id);
$this->db->delete('entries_new');
}
查看页面
<link href="<?php echo base_url(); ?>assets/bootstrap.min.css"
rel="stylesheet" media="screen">
<div class="row">
<div class="col-md-3"></div>
<div class="col-md-6">
<h2 align="center">HOME</h2>
<h4 align="center"> Welcome !!!!!!!!!! </h4>
<form>
<div class="table-responsive" >
<table border="0" align="center" class="table" style="border:1px solid
darkorange;">
<tbody>
<tr>
<th>Id</th>
<th>Name</th>
<th>Email</th>
<th>Mobile</th>
<th>Action</th>
</tr>
</tbody>
<?php $i=0;
// print_r($records); die;
foreach($ResAre as $row)
{ ?>
<tr>
<td><?php echo $row->id;?></td>
<td><?php echo $row->name;?></td>
<td><?php echo $row->email;?></td>
<td><?php echo $row->mobile;?></td>
<td><a href="<?php echo base_url('index.php/admin_controller/update');?
>?editid=<?php echo $row->id; ?> ">Edit</a> | <a href="<?php echo
base_url('index.php/admin_controller/delete');?>?deleteid=<?php echo
$row->id;?>">Delete</a></td>
</tr> <?php $i++;} ?>
<td colspan="6">
<div align="center"><a href="<?php echo
base_url('index.php/admin_controller');?>">ADD</a></div></td>
</table>
</div>
</form>
<div align="center"><a href='<?php echo
base_url('index.php/login_controller/logout'); ?>' > Logout</a>
</div>
</div>
<div class="col-md-3"></div>
</div>
答案 0 :(得分:1)
希望这会对您有所帮助:
在您的模型中:
首先:在admin_model
中,您使用CI_Controller
进行扩展,更改CI_Model
class Admin_Model extends CI_Model
{
public function __construct()
{
parent::__construct();
}
/* `get_data()` should be like this:*/
public function get_data($limit, $start)
{
$this->db->limit($limit, $start);
$query = $this->db->select('*') ->from('entries_new') ->get();
//print_r($query);die;
return $query->result();
}
}
第二:在get_data()
和controller
model
方法设置限制和偏移量
$config = array();
$config["base_url"] = site_url('admin_controller/manage');
$config["total_rows"] = $this->db->get('entries_new')->num_rows();
$config["per_page"] = 2;
$config["uri_segment"] = 3;
$config["num_links"] = 3;
$config['prev_link'] = 'Previous';
$config['next_link'] = 'Next';
$config['last_link'] = 'Last';
$this->pagination->initialize($config);
$page = ($this->uri->segment(3)) ? $this->uri->segment(3) : 0;
/* pass offest and limit here if not passing*/
$data["ResAre"] = $this->admin_model->get_data($config["per_page"],$page);
$data["pagination"] = $this->pagination->create_links();
$this->load->view('adminpage',$data);