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

发布时间:2022-04-30 发布网站:脚本宝典
脚本宝典收集整理的这篇文章主要介绍了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",$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');

}

还有其他方法可以做到一点,或者我完全错了吗?

解决方法@H_404_17@ @H_301_18@ 首先,当您进行分页时,页码必须来自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,$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',);
        $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&amp;b=2&c=3

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

脚本宝典总结

以上是脚本宝典为你收集整理的php – 如何在功能中使用参数CodeIgniter?全部内容,希望文章能够帮你解决php – 如何在功能中使用参数CodeIgniter?所遇到的问题。

如果觉得脚本宝典网站内容还不错,欢迎将脚本宝典推荐好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。