Codeigniter 对选定记录进行分页
我是 codeigniter 的新手。希望能帮助解决codeigniter分页问题。
- 我从我的视图中选择了一些记录并使用 $_POST 传递到我的控制器。
- 控制器将使用 $_POST 变量从模型中选择记录。
- 然后记录将显示在同一视图中并分页。
步骤1-3 没问题,视图显示正确的信息。
- 当从我的视图中按下分页按钮时。这将调用相同的控制器,但 $_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.
- i have selected some records from my view and pass to my controller using $_POST.
- the controller will use $_POST variable to select records from a model.
- then records will be display in the same view with pagination.
step 1-3 is okey, the view display correct info.
- 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
这听起来像是一个方法问题。为什么使用 POST 数据来过滤数据?分页库构建一个查询字符串来确定数据库查询结果的偏移量。为什么不能使用 $_GET 而不是 $_POST?
我想可以将分页配置“base_url”设置为:
然后在控制器中使用 $this->input->get_post(),而不是 $this->input->post( )。这里要小心——将完整的发布数据直接传递到模型中进行处理很少是安全的。最好使用 CI 的输入类来防止 XSS 攻击...
更新:
要使用 CI 的输入类,而不是直接使用 $_POST 或 $_GET,我通常将表单数据保存在“命名空间”中” 的种类,即:
然后,在你的控制器中:
因为我指定了 $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:
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.:
Then, in your controller:
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.
分页类使用 GET 请求,并简单地使用页面的记录偏移量修改 URL。例如,每页显示 50 条记录的列表的第 2 页将是 /some/url/50,第 3 页将是 /some/url/100。
您可以:
我认为#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:
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).
我建议你像这样编辑:
然后“$_GET”它形成url或jst使用
,并更改你的uri_segment
取决于通过url传递的参数数量
I suggest you to edit like:
and then "$_GET" it form url or jst use
and also change your uri_segment
Depends on number of argumnts passing through url