【发布时间】:2012-04-18 05:58:08
【问题描述】:
我想在视图页面中显示 2 行作为输出。再次,当我单击转到下一页时,它将显示 2 下一行,依此类推(我总共有 8 行 桌子)。但是当我运行以下代码时,它会在视图页面中显示所有 8 行以及分页链接。我尝试了一整天来找到不这样做的实际原因 工作,但我的问题仍未解决。我在互联网上搜索了相关查询的帮助,但没有用。 Finalyy我在这里用我自己的话来解释 问题。我已经注释了代码中的几乎所有行。我正在自动加载中加载库以进行分页和表单和 URL 的帮助程序。如果我真的很感激 有人帮帮我。提前致谢。
<?php
class ManageUser extends CI_Controller { // creating class for the controller
function index()
{
if($this->session->userdata('logged_in')) // checking users under session if he is already logged in
{
$this->load->model('admin/user'); // loading model . I am not using Datamapper.
$result = $this->user->view_user(); // getting response from the model and storing it to result
$total_rows = count($result); // counting number of rows countered ( its 8 in in my database )
//echo $total_rows;
if($result)
{
$data['users'] = $result;
$config['base_url'] = "http://192.168.0.102/project/index.php/admin/manageUser"; // this is the address where I am pointing the view page url
$config['total_rows'] = $total_rows; // Total numbers of rows assigned to pagination-config
$config['per_page'] = '2'; // I want to display 2 rows in 1 page
$config['uri_segment'] = '2';
$this->pagination->initialize($config); // initilizing the pagination-config
//$data['pagination'] = $this->pagination->create_links();
//$this->load->vars($data);
$this->load->view('admin/manageUser',$data); // Loading the page
}
//$this->load->view('admin/manageUser',$data);
}
else
{
redirect('admin/login'); // incase of faliured session user will be redirected to the login-page.
}
}
}
?>
以下代码来自我的名为 User 的模型
<?php
Class User extends CI_Model // extending the model
{
function __construct()
{
parent::__construct();
}
function view_user() // function which is loaded from controller
{
$query = $this -> db -> get('users'); //query to fetch all the information from the database
return $query->result(); // returning result to the Controller.
}
}
?>
最后这是我用来显示内容的视图页面。
<!-- This is the view page -->
<?php if(isset($users)) { ?> <!-- Checking if user is set -->
<?php foreach($users as $user) { ?> <!-- running in a loop to accept all the value from the database and display it row wise -->
<td><?php echo $user -> us_display_name; ?></td> <!-- Displaying name -->
<td><?php echo $user -> us_first_name . " " . $user -> us_last_name; ?></td> <!-- Displaying first name and last name together -->
<td><?php echo $user -> us_email_id; ?></td> <!-- Displaying email-ID -->
<?php } } else { ?>
<tr> <td>No records found!</td>
<?php } ?>
<?php echo $this->pagination->create_links(); ?>
【问题讨论】:
-
您需要在查询中定义偏移量和限制......
-
我也这样做了。但它不起作用。请检查下面的代码。 load->model('user'); $this->load->library('分页'); $user_list = 新用户(); $total_rows = $user_list->count(); //$student_list->order_by('name'); $data['user_list'] = $user_list->get(5, $offset)->all;
-
非常感谢您的回复。当我加载页面时,它会显示所有行。它会创建分页链接,但是当我单击该链接时,它会显示一个错误页面,说找不到页面。
-
weblee.co.uk/2009/06/06/codeigniter-pagination-part-1 请检查我目前所指的上述链接。我昨天自己粘贴了我的代码,但没有得到回复,但我一直在寻找相关代码。我找到了这个并试图实现这个新代码。你不会花太多时间去经历。它也允许您下载代码。我刚刚在我的 CI 中实现它并发现了这个错误:无法加载请求的文件:inc/header.php
-
我从你的链接得到的代码有基本的语法问题,比如你没有在你的控制器中加载模型(可能你已经在 autoload.php 中添加了)并且你在控制器中使用了错误的名称,你的模型名称是帖子模型,但您指的是帖子本身就是控制器名称
标签: php pagination codeigniter-2