Codeigniter 对选定记录进行分页

发布于 2024-12-17 12:07:42 字数 2301 浏览 0 评论 0原文

我是 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>

谢谢。

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

  1. i have selected some records from my view and pass to my controller using $_POST.
  2. the controller will use $_POST variable to select records from a model.
  3. then records will be display in the same view with pagination.

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

  1. when press on the pagination button from my view. This will call the same controller but $_POST info be come blank. So, my view is not displaying with the selected records as needed.

hope can help out.

i have simplied the code as follows:-

controllers:

    $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');

models:

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

View

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

View Gadget

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

Thank you.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

相对绾红妆 2024-12-24 12:07:42

这听起来像是一个方法问题。为什么使用 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 方法,因为您将这些参数作为查询发送,而不是向服务器发送某些内容。只是我的 0.02 美元在这个细节上。

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?

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();

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

UPDATE:

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)
    ));
}
...

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.

小巷里的女流氓 2024-12-24 12:07:42

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

您可以:

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

我认为#1 可能是最好的选择。它是 SEF,可以添加书签,总体来说是一种更简洁的方法。

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

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

The pagination class uses GET requests and simply modifies the URL with the record offset of the page. For example, page 2 for a list that shows 50 records per page would be /some/url/50 and page 3 would be /some/url/100.

You can either:

  1. Change #3 to work with either a POST or GET and call a URL that can identify which records to select, or
  2. Extend the pagination class to output a form that resends the same POST data from #2 each time you change the page (and have each page link POST a from rather than just be a link).

I think #1 is probably the best option. It's SEF, can be bookmarked, and is overall a cleaner way to do it.

For example, you use the form, as you normally do, but you can also go directly to the result using /some/unique/url

Then each pagination link will call /some/unique/url/50, where 50 is the record offset (pagination class uses offset rather than page number).

我还不会笑 2024-12-24 12:07:42

我建议你像这样编辑:

$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传递的参数数量

I suggest you to edit like:

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

and then "$_GET" it form url or jst use

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

and also change your uri_segment

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

Depends on number of argumnts passing through url

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文