Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/298.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 如何在带有分页代码点火器的函数中使用参数?_Php_Codeigniter_Pagination_Codeigniter 3 - Fatal编程技术网

Php 如何在带有分页代码点火器的函数中使用参数?

Php 如何在带有分页代码点火器的函数中使用参数?,php,codeigniter,pagination,codeigniter-3,Php,Codeigniter,Pagination,Codeigniter 3,我有一个函数,它以州名为参数,显示该州的所有城市。由于城市列表很长,我在同一函数中使用了分页,但当我单击“下一步”或任何其他分页链接时,该函数接受$state变量中的偏移值。功能是 public function load_Page($state){ $this->load->database(); $this->load->library('pagination'); $a = 1; $this->db->select("*

我有一个函数,它以州名为参数,显示该州的所有城市。由于城市列表很长,我在同一函数中使用了分页,但当我单击“下一步”或任何其他分页链接时,该函数接受$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();
    }

    // -----------------------------------------------------------------------
}
然后,为了检查分页是否有效,我转到:

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\u query\u strings
被设置为
TRUE
,这意味着
?a=1&b=2&c=3
将全部附加到分页链接。

只是一个提示:我将在config/autoload.php中自动加载数据库库,这样您就不必使用
$this->load->database()始终发布您的
视图
文件。为什么要使用事务来获得单个查询的结果?我仍然在CodeIgniter中学习。在我看来,我只是有一个for循环,在表中显示特定州的城市列表。视图很长很抱歉我不能粘贴到这里。另外,请忽略事务部分,因为我正在了解它,所以我只是添加了它。非常感谢您的帮助,但它并没有解决我的问题。我的问题是,当我单击2或3或下一个页码时,上面的控制器函数在参数$state中没有任何值的情况下重新运行,这导致了一个错误,因为我要求该州显示特定于它的城市。我的代码将$state传递给分页配置的base_url,而您的代码没有。这就是$state将持续存在的方式。也许有些事情与分页无关,但与$state本身有关?我已经修改了答案,现在在测试时包括了所有代码,包括视图和数据库设置。它100%有效。谢谢你的帮助。如果我有一个以上的参数,你能给我一个建议吗。上述解决方案不适用于n个数字参数。CodeIgniter分页允许您在整个分页链接中添加查询字符串并维护查询字符串。有关更多信息,请参阅用户指南。有了我提供的,你应该会觉得很有趣。
$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