我是 codeigniter 的新手。希望能帮助解决codeigniter分页问题。
- 我从我的 View 中选择了一些记录,并使用 $_POST 传递到我的 Controller 。
- Controller 将使用 $_POST 变量从模型中选择记录。
- 然后记录将在同一 View 中显示并分页。
步骤1-3正常, View 显示正确信息。
- 当从我的 View 中按下分页按钮时。这将调用相同的 Controller ,但 $_POST 信息为空。因此,我的 View 没有根据需要显示所选记录。
希望能帮到你。
我将代码简化如下:-
Controller :
$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?
我想可以将分页配置“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();
然后在 Controller 中使用 $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]" />
然后,在您的 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.02 美元在这个细节上。
关于php - Codeigniter 对选定记录进行分页,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8223341/