pagination does not change page(分页不会改变页面)
问题描述
我正在为我的网站进行分页,我在数据库中显示来自两个表的数据我的页面中有分页,但它的行为不像它假设的那样,它不会改变页面.目前我有 16 条记录显示在网页上并设置了 $config['per_page'] = 1;
分页栏上升到 16 但它不会翻页所有记录都显示在页面上.任何帮助将不胜感激这里是我的代码:
控制器
load->model('result_model');$data['query'] = $this->result_model->result_getall();//print_r($data['query']);死();$this->load->library('分页');$config['base_url'] = 'http://localhost/Surva/index.php/result_controller/getall';$config['total_rows'] = $this->db->get('tblanswers, credentials')->num_rows();$config['per_page'] = 1;$config['num_links'] = 10;$this->分页->初始化($config);$data['records'] = $this->db->get('tblanswers, credentials', $config['per_page'], $this->uri->segment(1, 0))->result_array();$data['pagination'] = $this->pagination->create_links();$this->load->view('result_view', $data);}}?>
查看
<表格边框=1"><tr><th>姓名</th><th>第二名</th><th>电话</th><th>电子邮件</th><th>Answer</th><th>评论</th></tr><?php foreach ($query as $row): ?><tr><td><?php echo $row->name;?></td><td><?php echo $row->second_name;?></td><td><?php echo $row->phone;?></td><td><?php echo $row->email;?></td><td><?php echo $row->answerA;?><?php echo $row->answerB;?><?php echo $row->answerC;?></td><td><?php echo $row->comment;?><br></td></tr><?php endforeach;?><?php if (isset($pagination)){回声$分页;//echo "";var_dump($query);?>
模型
function result_getall(){返回 $this->db->select('tblanswers.*,credentials.*')->from('tblanswers, 凭据')->get()->result_object();
解决方案 查询不会神奇地知道您只需要 x 条记录.Codeigniter 将显示您从模型中提供的任何内容.
function getall(){$this->load->model('result_model');//print_r($data['query']);死();$this->load->library('分页');$config['base_url'] = 'http://localhost/Surva/index.php/result_controller/getall';$config['total_rows'] = $this->db->get('tblanswers, credentials')->num_rows();$config['per_page'] = 1;$config['num_links'] = 10;$config['uri_segment'] = 3;//在这里猜测,但这是您用来更改页面的 uri 段所在的位置.if($this->uri->segment('3')) {$offest = $this->uri->segment('3');} 别的 {$offest = 0;}$data['query'] = $this->result_model->result_getall($config['per_page'],$offset);$this->分页->初始化($config);$data['records'] = $this->db->get('tblanswers, credentials', $config['per_page'], $this->uri->segment(1, 0))->result_array();$data['pagination'] = $this->pagination->create_links();$this->load->view('result_view', $data);}模型:函数 result_getall($limit=0,$offset=0){如果($限制!= 0){返回 $this->db->select('tblanswers.*,credentials.*')->from('tblanswers, 凭据')->limit($limit, $offset)->get()->result_object();} 别的 {返回 $this->db->select('tblanswers.*,credentials.*')->from('tblanswers, 凭据')->get()->result_object();}
注意在控制器中我将偏移量和每页传递给模型,然后使用它们来限制模型本身的返回.
I am working on pagination for my website where i am displaying data from two tables on a database i have pagination in my page but it dose not behave as it suppose to behave it dose not change pages. currently i have 16 records displaying on the web page and have set $config['per_page'] = 1;
the pagination bar goes up to 16 but it dose not flip through the pages all the records are displayed on the page. any help will be appreciated here is my code :
controller
<?php
class Result_controller extends CI_Controller{
function getall(){
$this->load->model('result_model');
$data['query'] = $this->result_model->result_getall();
// print_r($data['query']); die();
$this->load->library('pagination');
$config['base_url'] = 'http://localhost/Surva/index.php/result_controller/getall';
$config['total_rows'] = $this->db->get('tblanswers, credentials')->num_rows();
$config['per_page'] = 1;
$config['num_links'] = 10;
$this->pagination->initialize($config);
$data['records'] = $this->db->get('tblanswers, credentials', $config['per_page'], $this->uri->segment(1, 0))->result_array();
$data['pagination'] = $this->pagination->create_links();
$this->load->view('result_view', $data);
}
}
?>
view
<div>
<table border="1">
<tr>
<th>Name</th>
<th>Second Name</th>
<th>Phone</th>
<th>Email</th>
<th>Answer</th>
<th>Comment</th>
</tr>
<?php foreach ($query as $row): ?>
<tr>
<td><?php echo $row->name; ?></td>
<td><?php echo $row->second_name; ?></td>
<td><?php echo $row->phone; ?></td>
<td><?php echo $row->email; ?></td>
<td> <?php echo $row->answerA;?>
<?php echo $row->answerB;?>
<?php echo $row->answerC;?></td>
<td><?php echo $row->comment;?><br></td>
</tr>
<?php endforeach; ?>
</table>
<?php if (isset($pagination))
{
echo $pagination;
// echo "<pre>"; var_dump($query);
} ?>
model
function result_getall(){
return $this->db->select('tblanswers.*,credentials.*')
->from('tblanswers, credentials')
->get()
->result_object();
解决方案 The query doesn't magically know you only want x number of records. Codeigniter is going to display whatever you feed it from the model.
function getall(){
$this->load->model('result_model');
// print_r($data['query']); die();
$this->load->library('pagination');
$config['base_url'] = 'http://localhost/Surva/index.php/result_controller/getall';
$config['total_rows'] = $this->db->get('tblanswers, credentials')->num_rows();
$config['per_page'] = 1;
$config['num_links'] = 10;
$config['uri_segment'] = 3; //guessing here, but this is where the uri segment you use to change pages goes.
if($this->uri->segment('3')) {
$offest = $this->uri->segment('3');
} else {
$offest = 0;
}
$data['query'] = $this->result_model->result_getall($config['per_page'],$offset);
$this->pagination->initialize($config);
$data['records'] = $this->db->get('tblanswers, credentials', $config['per_page'], $this->uri->segment(1, 0))->result_array();
$data['pagination'] = $this->pagination->create_links();
$this->load->view('result_view', $data);
}
Model:
function result_getall($limit=0,$offset=0){
if($limit != 0)
{
return $this->db->select('tblanswers.*,credentials.*')
->from('tblanswers, credentials')
->limit($limit, $offset)
->get()
->result_object();
} else {
return $this->db->select('tblanswers.*,credentials.*')
->from('tblanswers, credentials')
->get()
->result_object();
}
Notice in the controller I'm passing the offset and the per page to the model, then using them to limit the returns in the model itself.
这篇关于分页不会改变页面的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:分页不会改变页面
基础教程推荐
- 在 CakePHP 2.0 中使用 Html Helper 时未定义的变量 2021-01-01
- 使用 PDO 转义列名 2021-01-01
- PHP 守护进程/worker 环境 2022-01-01
- phpmyadmin 错误“#1062 - 密钥 1 的重复条目‘1’" 2022-01-01
- 如何在 XAMPP 上启用 mysqli? 2021-01-01
- HTTP 与 FTP 上传 2021-01-01
- 在 yii2 中迁移时出现异常“找不到驱动程序" 2022-01-01
- 如何在 Symfony 和 Doctrine 中实现多对多和一对多? 2022-01-01
- 找不到类“AppHttpControllersDB",我也无法使用新模型 2022-01-01
- Doctrine 2 - 在多对多关系中记录更改 2022-01-01