我有一个函数,它将状态名称作为参数,并显示该特定状态的所有城市.由于城市列表很长,我在同一个函数中使用了分页,但是当我点击’Next’或任何其他分页链接时,函数接受$state变量中的偏移值.功能是
public function load_Page($state){
$this->load->database();
$this->load->library('pagination');
$a = 1;
$this->db->select("*")->where("userstate" , $a)->where("state" , $state);
$query0 = $this->db->get("city");
$this->db->select("*")->where("userstate" , $a)->where("state" , $state);
$query1 = $this->db->get('city' , 10 , $this->uri->segment(3));
$config["base_url"] = base_url()."index.php/city/load_Page";
$total_row = $query0->num_rows();
$config['page_query_string'] = TRUE;
$config["total_rows"] = $total_row;
$config["per_page"] = 10;
$this->pagination->initialize($config);
$data["state"] = $state;
$data["result"] = $query1->result();
//$data["rows"] = $query1->num_rows();
$this->load->view('header');
$this->load->view('city', $data);
$this->load->view('footer');
}
还有其他方法可以做到这一点,或者我完全错了吗?
最佳答案 首先,当您进行分页时,页码必须来自URL,并且始终可用作控制器方法中的参数.它应默认为第1页.
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class City extends CI_Controller {
public function load_page( $state, $page = 1 ){
// I'm going to use an alias for this model
$this->load->model('example_model', 'model');
// Use the URL helper for site_url()
$this->load->helper('url');
// Set pagination config
$config['pagination_settings'] = [
'per_page' => 10,
'use_page_numbers' => TRUE,
'uri_segment' => 4, // This is very important!!!
'base_url' => site_url('city/load_page/' . $state)
];
// Get the total rows
$config['pagination_settings']["total_rows"] = $this->model->pagination_count( $state );
// Load and initialize pagination
$this->load->library('pagination');
$this->pagination->initialize($config['pagination_settings']);
$data = [
'state' => $state,
'rows' => $this->model->get_cities( $state, $page, $config['pagination_settings']['per_page'] ),
'links' => $this->pagination->create_links()
];
// Use data in views or wherever needed ...
$this->load->view('city', $data);
}
/**
* Create the rows to paginate
*/
public function setup()
{
// I'm going to use an alias for this model
$this->load->model('example_model', 'model');
$this->model->setup();
}
// -----------------------------------------------------------------------
}
接下来,您应该将数据库查询移动到模型.您不需要为2个选择类型查询使用事务.
<?php
defined('BASEPATH') or exit('No direct script access allowed');
class Example_model extends CI_Model{
public function __construct()
{
parent::__construct();
$this->load->database();
}
public function pagination_count( $state )
{
return $this->db->where("state" , $state)
->count_all_results('city');
}
public function get_cities( $state, $page, $limit )
{
$offset = ( $page * $limit ) - $limit;
$query = $this->db->where("state" , $state)
->limit( $limit, $offset )
->get('city');
if( $query->num_rows() > 0 )
return $query->result();
return NULL;
}
/**
* Setup for testing
*/
public function setup()
{
$this->load->dbforge();
$fields = array(
'id' => array(
'type' => 'INT',
'constraint' => 5,
'unsigned' => TRUE,
'auto_increment' => TRUE
),
'state' => array(
'type' => 'VARCHAR',
'constraint' => '32',
),
'city' => array(
'type' => 'VARCHAR',
'constraint' => '32',
),
);
$this->dbforge->add_field($fields);
$this->dbforge->add_key('id', TRUE);
$this->dbforge->create_table('city', TRUE);
for( $x = 1; $x <= 40; $x++ )
{
$this->db->insert('city', array(
'state' => 'ca',
'city' => 'x' . $x
));
}
}
}
这是我使用的视图:
<?php
echo '<h1>' . $state . '</h1>';
echo $links . '<br /><br />';
foreach( $rows as $row )
{
echo $row->city . '<br />';
}
为了设置数据库进行测试,我去了:
http://localhost/index.php/city/setup
然后检查分页是否有效,我去了:
http://localhost/index.php/city/load_page/ca
它应该适合您,因为此代码现在已经过全面测试.
更新——————–
如果要为分页添加更多参数,请使用查询字符串.您需要使用以下额外设置设置分页配置:
$config['pagination_settings']['reuse_query_string'] = TRUE;
这意味着配置看起来像这样:
$config['pagination_settings'] = [
'per_page' => 10,
'use_page_numbers' => TRUE,
'uri_segment' => 4, // This is very important!!!
'base_url' => site_url('city/load_page/' . $state),
'reuse_query_string' => TRUE
];
然后使用params创建指向第一页的链接:
http://localhost/index.php/city/load_page/ca?a=1&b=2&c=3
并且由于reuse_query_strings被设置为TRUE,这意味着?a = 1& b = 2& c = 3将全部附加到分页链接.