Php 如何使用$this->;在codeigniter中使用分页;db->;查询()?
我正在开发一个关于Php 如何使用$this->;在codeigniter中使用分页;db->;查询()?,php,codeigniter,codeigniter-3,Php,Codeigniter,Codeigniter 3,我正在开发一个关于codeigniter的项目,在创建记录的分页时遇到了困难 实际上,当我使用user$this->db->get()时,分页就可以了。但是当使用$this->db->query()进行分页时,分页不起作用,下面是我的控制器中的代码 $this->load->library('pagination'); $pagination_config['base_url'] = base_url().'welcome/index'; $pagi
codeigniter
的项目,在创建记录的分页时遇到了困难
实际上,当我使用user$this->db->get()
时,分页就可以了。但是当使用$this->db->query()
进行分页时,分页不起作用,下面是我的控制器中的代码
$this->load->library('pagination');
$pagination_config['base_url'] = base_url().'welcome/index';
$pagination_config['per_page'] = 3;
$pagination_config['num_links'] = 3;
$pagination_config['total_rows'] = $this->db->get('properties')->num_rows();
$this->pagination->initialize($pagination_config);
$q = $this->db->query("SELECT title FROM properties", $pagination_config['per_page']);
$r = $q->result();
foreach($r as $p){
echo $p->title.'<br />';
}
echo '<br />';
echo '<br />';
echo $this->pagination->create_links();
它显示的是电子记录,但在所有页面上都是一样的。开始使用CodeIgniter分页
无论分页是什么,通常都需要一个URI段来指定页面,除非希望URI段指定偏移量。在我的示例中,我将假设对页面的需求
所以,假设我们想对一些foos进行分页,我们的数据库包含一个名为foos的表。假装我们有很多食物(65)。在我们的Foos控制器中,我们有一些方法:
public function index()
{
$this->page();
}
public function page( $page = 1 )
{
// Here we get and show our foos
}
当我们想要得到当前页面的foos结果时,我们有一个page number参数,这就是我们如何知道要得到哪一组foos,但首先我们还需要一个foos的总数。因此,在我们的page方法中,我们可以调用模型上的方法来完成所有工作:
public function page( $page = 1 )
{
// Here we get and show our foos
$this->load->model('foos_model');
$view_data['foos'] = $this->foos_model->get_foos( $page );
}
所有的工作都是在模型中完成的
记住,在模型中,我们需要一个foo总数、分页链接创建,然后是这个页面的foo。让我们从计算食物开始:
public function get_foos( $page )
{
// First count all foos
$count = $this->db->count_all('foos');
}
接下来,创建分页链接。假设我们希望每页10个foos:
public function get_foos( $page )
{
// First count all foos
$count = $this->db->count_all('foos');
// Create the pagination links
$this->load->library('pagination');
$this->load->helper('url');
$paging_conf = [
'uri_segment' => 3,
'per_page' => 10,
'total_rows' => $count,
'base_url' => site_url('foos/page'),
'first_url' => site_url('foos'),
'use_page_numbers' => TRUE
];
$this->pagination->initialize($paging_conf);
// Create the paging buttons for the view
$this->load->vars('pagination_links', $this->pagination->create_links());
}
现在是时候获得我们将要显示的实际的一组foo了。请注意偏移量的计算:
public function get_foos( $page )
{
// First count all foos
$count = $this->db->count_all('foos');
// Create the pagination links
$this->load->library('pagination');
$this->load->helper('url');
$paging_conf = [
'uri_segment' => 3,
'per_page' => 10,
'total_rows' => $count,
'base_url' => site_url('foos/page'),
'first_url' => site_url('foos'),
'use_page_numbers' => TRUE
];
$this->pagination->initialize($paging_conf);
// Create the paging buttons for the view
$this->load->vars('pagination_links', $this->pagination->create_links());
// The pagination offset
$offset = $page * $paging_conf['per_page'] - $paging_conf['per_page'];
// Get our set of foos
$query = $this->db->get('foos', $paging_conf['per_page'], $offset);
}
最后,在将foo传递回控制器之前,确保存在foo:
public function get_foos( $page )
{
// First count all foos
$count = $this->db->count_all('foos');
// Create the pagination links
$this->load->library('pagination');
$this->load->helper('url');
$paging_conf = [
'uri_segment' => 3,
'per_page' => 10,
'total_rows' => $count,
'base_url' => site_url('foos/page'),
'first_url' => site_url('foos'),
'use_page_numbers' => TRUE
];
$this->pagination->initialize($paging_conf);
// Create the paging buttons for the view
$this->load->vars('pagination_links', $this->pagination->create_links());
// The pagination offset
$offset = $page * $paging_conf['per_page'] - $paging_conf['per_page'];
// Get our set of foos
$query = $this->db->get('foos', $paging_conf['per_page'], $offset);
// Make sure we have foos
if( $query->num_rows() > 0 )
return $query->result();
// Else return default
return NULL;
}
回到控制器中,我们可以将foos传递给视图:
public function page( $page = 1 )
{
// Here we get and show our foos
$this->load->model('foos_model');
$view_data['foos'] = $this->foos_model->get_foos( $page );
// Load the view and pass in the foos
$this->load->view('foos_view', $view_data);
}
显示分页链接和foo
在视图中,我们现在可以显示分页链接和foo:
echo $pagination_links;
if( ! empty( $foos ) )
{
foreach( $foos as $foo )
echo $foo->name . '<br />';
}
开始使用CodeIgniter分页
无论分页是什么,通常都需要一个URI段来指定页面,除非希望URI段指定偏移量。在我的示例中,我将假设对页面的需求
所以,假设我们想对一些foos进行分页,我们的数据库包含一个名为foos的表。假装我们有很多食物(65)。在我们的Foos控制器中,我们有一些方法:
public function index()
{
$this->page();
}
public function page( $page = 1 )
{
// Here we get and show our foos
}
当我们想要得到当前页面的foos结果时,我们有一个page number参数,这就是我们如何知道要得到哪一组foos,但首先我们还需要一个foos的总数。因此,在我们的page方法中,我们可以调用模型上的方法来完成所有工作:
public function page( $page = 1 )
{
// Here we get and show our foos
$this->load->model('foos_model');
$view_data['foos'] = $this->foos_model->get_foos( $page );
}
所有的工作都是在模型中完成的
记住,在模型中,我们需要一个foo总数、分页链接创建,然后是这个页面的foo。让我们从计算食物开始:
public function get_foos( $page )
{
// First count all foos
$count = $this->db->count_all('foos');
}
接下来,创建分页链接。假设我们希望每页10个foos:
public function get_foos( $page )
{
// First count all foos
$count = $this->db->count_all('foos');
// Create the pagination links
$this->load->library('pagination');
$this->load->helper('url');
$paging_conf = [
'uri_segment' => 3,
'per_page' => 10,
'total_rows' => $count,
'base_url' => site_url('foos/page'),
'first_url' => site_url('foos'),
'use_page_numbers' => TRUE
];
$this->pagination->initialize($paging_conf);
// Create the paging buttons for the view
$this->load->vars('pagination_links', $this->pagination->create_links());
}
现在是时候获得我们将要显示的实际的一组foo了。请注意偏移量的计算:
public function get_foos( $page )
{
// First count all foos
$count = $this->db->count_all('foos');
// Create the pagination links
$this->load->library('pagination');
$this->load->helper('url');
$paging_conf = [
'uri_segment' => 3,
'per_page' => 10,
'total_rows' => $count,
'base_url' => site_url('foos/page'),
'first_url' => site_url('foos'),
'use_page_numbers' => TRUE
];
$this->pagination->initialize($paging_conf);
// Create the paging buttons for the view
$this->load->vars('pagination_links', $this->pagination->create_links());
// The pagination offset
$offset = $page * $paging_conf['per_page'] - $paging_conf['per_page'];
// Get our set of foos
$query = $this->db->get('foos', $paging_conf['per_page'], $offset);
}
最后,在将foo传递回控制器之前,确保存在foo:
public function get_foos( $page )
{
// First count all foos
$count = $this->db->count_all('foos');
// Create the pagination links
$this->load->library('pagination');
$this->load->helper('url');
$paging_conf = [
'uri_segment' => 3,
'per_page' => 10,
'total_rows' => $count,
'base_url' => site_url('foos/page'),
'first_url' => site_url('foos'),
'use_page_numbers' => TRUE
];
$this->pagination->initialize($paging_conf);
// Create the paging buttons for the view
$this->load->vars('pagination_links', $this->pagination->create_links());
// The pagination offset
$offset = $page * $paging_conf['per_page'] - $paging_conf['per_page'];
// Get our set of foos
$query = $this->db->get('foos', $paging_conf['per_page'], $offset);
// Make sure we have foos
if( $query->num_rows() > 0 )
return $query->result();
// Else return default
return NULL;
}
回到控制器中,我们可以将foos传递给视图:
public function page( $page = 1 )
{
// Here we get and show our foos
$this->load->model('foos_model');
$view_data['foos'] = $this->foos_model->get_foos( $page );
// Load the view and pass in the foos
$this->load->view('foos_view', $view_data);
}
显示分页链接和foo
在视图中,我们现在可以显示分页链接和foo:
echo $pagination_links;
if( ! empty( $foos ) )
{
foreach( $foos as $foo )
echo $foo->name . '<br />';
}
当前页面在哪里?当前页面..?当前页面的编号默认情况下,它将是在xampp上运行的第一个页面http://project.local
单击分页按钮后,用户将转到某个页码。在开始时,它将是http://project.local
您似乎缺少偏移量。查看我的答案。您的当前页面在哪里?当前页面..?当前页面的编号默认情况下,它将是在xampp上运行的第一个页面http://project.local
单击分页按钮后,用户将转到某个页码。在开始时,它将是http://project.local
您似乎缺少偏移量。看看我的答案。