微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

php – 如何在功能中使用参数CodeIgniter?

我有一个函数,它将状态名称作为参数,并显示该特定状态的所有城市.由于城市列表很长,我在同一个函数中使用了分页,但是当我点击’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将全部附加到分页链接.

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐