【问题标题】:codeigniter pagination with selected records带有所选记录的codeigniter分页
【发布时间】:2011-11-22 07:25:11
【问题描述】:

我是 codeigniter 的新手。希望能帮助解决codeigniter分页的问题。

  1. 我从我的视图中选择了一些记录并使用 $_POST 传递给我的控制器。
  2. 控制器将使用 $_POST 变量从模型中选择记录。
  3. 然后记录将显示在与分页相同的视图中。

步骤 1-3 没问题,视图显示正确的信息。

  1. 在我看来按下分页按钮时。这将调用相同的控制器,但 $_POST 信息将变为空白。因此,我的视图没有根据需要显示所选记录。

希望能帮上忙。

我将代码简化如下:-

控制者:

    $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>

谢谢。

【问题讨论】:

  • 你能举出你的文件的例子以及你如何在它们之间传递数据吗?

标签: php mysql codeigniter


【解决方案1】:

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

我想可以将您的分页配置“base_url”设置为:

$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 攻击会更好……

更新:

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

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

然后,在您的控制器中:

...
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 方法,因为您将这些参数作为查询发送,而不是向服务器发送某些内容。只是我在那个细节上的 $.02。

【讨论】:

  • 谢谢,这应该可行。请举一个关于 CI 的输入类的例子。
  • 说了这么多,不接受?至少给我一个赞成票,嘘;)
  • @landons,我使用了您的解决方案,当我单击第二页/下一页时它起作用了。但是,当我单击第一页/上一页时出现错误,它什么也不显示。你能帮帮我吗?
【解决方案2】:

分页类使用 GET 请求,并简单地用页面的记录偏移量修改 URL。例如,每页显示 50 条记录的列表的第 2 页为 /some/url/50,第 3 页为 /some/url/100。

您可以:

  1. 更改 #3 以使用 POST 或 GET 并调用可以识别要选择的记录的 URL,或者
  2. 扩展分页类以输出一个表单,每次更改页面时都会从 #2 重新发送相同的 POST 数据(并让每个页面链接 POST 一个来源,而不仅仅是一个链接)。

我认为#1 可能是最好的选择。它是 SEF,可以添加书签,总体而言是一种更清洁的方式。

例如,您可以像往常一样使用表单,但您也可以使用 /some/unique/url 直接转到结果

那么每个分页链接都会调用/some/unique/url/50,其中50是记录偏移量(分页类使用偏移量而不是页码)。

【讨论】:

    【解决方案3】:

    我建议你编辑如下:

    $config['base_url']=site_url('site/users_area/arg1/arg2') ;
    

    然后 "$_GET" 它形成 url 或 jst 使用

    $val1 = $this->uri->segment(3);
    $val1 = $this->uri->segment(4);
    

    并且还要改变你的 uri_segment

    $config['uri_segment'] = '3';to
    $config['uri_segment'] = '5'.
    

    取决于通过 url 的参数数量

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-09-24
      • 1970-01-01
      • 2016-12-13
      • 1970-01-01
      • 2021-06-11
      • 1970-01-01
      • 1970-01-01
      • 2013-04-06
      相关资源
      最近更新 更多