gpt4 book ai didi

php - 如何在分页 CodeIgniter 函数中使用参数?

转载 作者:塔克拉玛干 更新时间:2023-11-03 05:43:29 25 4
gpt4 key购买 nike

我有一个函数,它将州名称作为参数并显示该特定州的所有城市。由于城市列表很长,我在同一个函数中使用了分页,但是当我单击“下一步”或任何其他分页链接时,该函数接受 $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,并且它始终作为 Controller 方法中的参数提供。它应该默认为第 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
));
}
}
}

这是我使用的 View :

<?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
];

然后使用参数创建指向第一页的链接:

http://localhost/index.php/city/load_page/ca?a=1&b=2&c=3

并且由于 reuse_query_strings 被设置为 TRUE,这意味着 ?a=1&b=2&c=3 将全部附加到分页链接。

关于php - 如何在分页 CodeIgniter 函数中使用参数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45809464/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com