【发布时间】:2018-09-14 08:29:16
【问题描述】:
我有一个完美运行的分页列表页面!很好!我在这里看到了很多关于让分页工作的帖子,但我没有看到同样的问题。如果您认为有一个帖子会有所帮助,请直接在那里。
现在我转到同一页面上的一个表单,用户可以在其中“过滤”结果,并且该模型/函数将结果返回到同一页面。虽然搜索结果的分页不起作用。
我的猜测是 Total_rows 需要更改,但我不确定是否是这样。
要更广泛地了解正在发生的事情,heres my repository for it.
控制器
public function searchGiraffe($offset = 0)
{
// pagination configuration
$config['base_url'] = base_url() . 'MainForms/searchGiraffe/';
$config['total_rows'] = $this->session->flashdata('resultcount');//$this->db->count_all('heroes');
$config['per_page'] = 10;
$config['uri_segment'] = 3;
// Init pagination
$this->pagination->initialize($config);
$GiraffeName = $_POST['name'];
$GiraffeWork = $_POST['FieldsOfWork'];
$GiraffeLocation = $_POST['location'];
$GiraffeGender = $_POST['gender'];
$GiraffeAge = $_POST['age'];
$GiraffeOccupation = $_POST['occupation'];
$data = array('h_name' => $GiraffeName,
'type_of_activism1' => $GiraffeWork,
'type_of_activism2' => $GiraffeWork,
'type_of_activism3' => $GiraffeWork,
'global_area_1' => $GiraffeLocation,
'global_area_2' => $GiraffeLocation,
'state_1' => $GiraffeLocation,
'state_2' => $GiraffeLocation,
'gender' => $GiraffeGender,
'age' => $GiraffeAge,
'occupation' => $GiraffeOccupation); // end of array
$results = $this->Main_model->searchForGiraffe($data,$config['per_page'],$offset);
//$count = $this->Main_model->countForGiraffe($data);
if($results != null)
{
$data['g_heroes'] = $results;
//$this->girafferesult($results);
$this->load->view('header');
$this->load->view('heroes/find-giraffe',$data);
$this->load->view('footer');
} //end if
else
{
$data['g_heroes'] = 'error';
$this->load->view('header');
$this->load->view('heroes/find-giraffe',$data);
$this->load->view('footer');
}
} // end of SearchGiraffe()
模型
public function searchForGiraffe($data,$limit = FALSE,$offset = FALSE){
if($limit){
$this->db->limit($limit,$offset);
}
$search_for = array();
foreach($data as $key => $value){
if(!empty($value) && $value != 'Select'){
array_push($search_for, $key);
}else{
// do nothing
}
}
$counter = 1;
$filter = 'SELECT * FROM heroes WHERE ';
foreach($data as $key => $search){
if(!empty($search) && $search != 'Select'){
if($counter < count($search_for)){
if($key == 'type_of_activism1' || $key == 'global_area_1'){
$filter .= '(' . $key . '="' . $search . '" OR ';
}else if($key == 'type_of_activism2' || $key == 'global_area_2' || $key == 'country_2' || $key == 'state_1' || $key == 'country_1'){
$filter .= $key . '="' . $search . '" OR ';
}else if($key == 'type_of_activism3'){
$filter .= $key . '="' . $search . '") AND ';
}else if($key == 'state_2'){
$filter .= $key . '="' . $search . '") AND ';
}
else
{
$filter .= $key . '="' . $search . '" AND ';
}
$counter = $counter + 1;
}else if($counter == 3 && $key == 'type_of_activism3'){
$filter .= $key . '="' . $search . '")';
}else{
$filter .= $key . '="' . $search . '"';
}
}else{
// do nothing
}
} // end of foreach($data as $search)
$result = $this->db->query($filter);
$count = $result->num_rows();
$this->session->set_flashdata('resultcount', $count);
return($result->result());
} // end searchForGiraffe
对于我的最后一个技巧,视图:
<main id="find">
<?php
if(isset($g_heroes)){
foreach($g_heroes as $row){
?>
<section class="giraffe">
<div class="ghero">
<a href="view-giraffe/<?php echo $row->id; ?>">
<?php echo $row->h_name; ?>
</a>
</div>
<div class="intro">
<?php echo $row->blurb; ?>
</div>
</section>
<?php
} // end of foreach
} // end of else
echo '<div id="pagination">';
echo $this->pagination->create_links();
echo '</div>';
?>
【问题讨论】:
标签: php codeigniter search pagination codeigniter-3