带有选定记录的代码点火器分页 [英] codeigniter pagination with selected records

查看:49
本文介绍了带有选定记录的代码点火器分页的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是Codeigniter的新手。希望可以帮助解决有关代码点火器分页的问题。

I am new with codeigniter. Hope can help to solve problem on codeigniter pagination.


  1. 我从视图中选择了一些记录,并使用$ _POST传递给控制器​​。

  2. 控制器将使用$ _POST变量从模型中选择记录。

  3. 然后记录将在与分页相同的视图中显示。

步骤1-3是好的,视图显示正确的信息。

step 1-3 is okey, the view display correct info.


  1. 从我的角度按下分页按钮时。这将调用相同的控制器,但$ _POST信息为空。因此,我的视图未根据需要显示选定的记录。

希望可以提供帮助。

我已将代码简化如下:-

i have simplied the code as follows:-

控制器:

    $config['total_rows']=$this->invdata_model->getFilterData_numRows();

    $config['base_url']=site_url('site/users_area') ;
    $config['uri_segment'] = '3'; 
    $config['per_page']=18;
    $config['num_links']=4;

    $this->pagination->initialize($config);

    $data['records']=$this->invdata_model->getFilterData_Rows($config['per_page'],$this->uri->segment(3));
    $data['rec_country']=$this->invdata_model->country();

    $this->load->view('includes/header');
    $this->load->view('users_area_view',$data);
    $this->load->view('includes/footer');

型号:

    $country = $this->input->post('country') ;


    $this->db->select('stockno, bdlno, country,volton');
    if (isset($country)) { $this->db->where_in('country',$country); }
    $q=$this->db->get('inventory')->num_rows();

    return $q ; 

查看

  <?php echo form_open('site/users_area');   
        echo $this->table->generate($records);   
        echo $this->pagination->create_links() ;    ?>

  <div class="gadget">
<?php echo form_submit('submit','Apply','class="button_form"'); ?>      
  </div>

  $gadget['gadget_name']='country'; 
  $gadget['gadget_rec']=$rec_country; 
  $this->load->view('gadget',$gadget);
  </form>

查看小工具

  <div class="gadget">
  <?php

  $gadget_name2=$gadget_name.'[]';

  echo "<ul>";
  foreach ($gadget_rec as $item) {
    echo '<li >';  
    echo '<div id="sectionname">'.$item.'</div>';
    echo '<div id="sectioninput"><input type="checkbox" name="'.$gadget_name2.'" value="'.$item.'"></div>' ;
    echo '-';
    echo "</li>";  
  }

  echo "<ul>";
   ?>
  </div>

谢谢。

推荐答案

这听起来像是方法问题。为什么要使用POST数据来过滤数据?分页库将构建查询字符串,以确定数据库查询结果的偏移量。您为什么不能使用$ _GET而不是$ _POST?

It sounds like an approach issue. Why are you using POST data to filter your data? The pagination library builds a query string to determine the offset of your database query results. Why can't you use $_GET instead of $_POST?

我想可以将您的分页配置'base_url'设置为此:

I suppose it would be possible to set your pagination config 'base_url' to this:

$this->load->helper('url');
$this->load->library('pagination');

$config['base_url'] = current_url().'?'.http_build_query($_POST);
$config['total_rows'] = 200;
$config['per_page'] = 20;

$this->pagination->initialize($config);

echo $this->pagination->create_links();

然后在您的控制器中,使用$ this-> input-> get_post()而不是$ this-> input-> post()。请注意,将完整的帖子数据直接传递到模型中进行处理几乎是不安全的。最好使用CI的输入类来防止XSS攻击...

And then in your controller, use $this->input->get_post(), rather than $this->input->post(). Be careful here--it's rarely safe to pass the full post data directly into a model for processing. It would be better to use CI's input class to prevent XSS attacks...

更新:

要使用CI的输入类,而不是直接使用$ _POST或$ _GET,我通常将表单数据保存在某种命名空间中,即:

To use CI's input class, rather than $_POST or $_GET directly, I usually keep my form data in a "namespace" of sorts, i.e.:

<input type="text" name="search[criteria1]" />
<input type="text" name="search[criteria2]" />
<input type="text" name="search[criteria3]" />

然后,在您的控制器中:

Then, in your controller:

...
public function some_resource()
{
    $this->load->model('Some_model');
    $search_criteria = $this->input->get_post('search');
    if ($search_criteria)
    {
        // make sure here to remove any unsupported criteria
        // (or in the model is usually a bit more modular, but
        // it depends on how strictly you follow MVC)
        $results = $this->Some_model->search($search_criteria);
    }
    else
    {
        // If no search criteria were given, return unfiltered results
        $results = $this->Some_model->get_all();
    }

    $this->load->view('some_view', array(
        'results'         => $results,
        'pagination_base' => current_url().'?'.http_build_query($search_criteria)
    ));
}
...

因为我指定了$ this-> input- > get_post (),它将首先检查查询字符串参数;如果不存在,则会回退以发布数据。这将允许您在表单中使用POST,但仍通过分页类通过查询字符串传递相同的数据。但是,实际上,表单应该使用GET方法,因为您将这些参数作为查询发送,而不是向服务器发送内容。仅$ 0.22就是我的细节。

Because I specified $this->input->get_post(), it will first check for a query string parameter; if it doesn't exist, it falls back to post data. This will allow you to use POST in your form, but still pass in the same data through the query string with the pagination class. However, really the form should be using the GET method, as you're sending these parameters as a query, and not to send something to the server. Just my $.02 on that detail.

这篇关于带有选定记录的代码点火器分页的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆