动态过滤器分页在Codeigniter中不起作用

时间:2018-04-04 17:11:46

标签: php codeigniter search pagination codeigniter-3

我有一个分页页面,页面效果很好!很好!我在这里看到很多关于分页工作的帖子,但我没有看到同样的问题。如果您认为有一个帖子可以提供帮助,请直接在那里。

现在我转到同一页面上的表单,用户可以“过滤”结果,模型/函数将结果返回到同一页面。但搜索结果的分页不起作用。

我的猜测是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>';
?>

2 个答案:

答案 0 :(得分:0)

分页创建链接系统&lt; 1 2 3>

说你总共30行,每页10行 如果您设置了一个过滤器,则总共得到20行,即2页

当我点击数字&#34; 2&#34;你正在刷新帖子,你用过滤器发布号码2,所以数据库为你提供第2页,共3页

您可以使用会话来解决此问题

if ($this->input->get()) {
    $postdata = array(
                    'GiraffeName' =>   $_POST['name'],
                    'GiraffeWork' =>  $_POST['FieldsOfWork'],
                    'GiraffeLocation' => $_POST['location'],
                    'GiraffeGender' => $_POST['gender'],
                    'GiraffeAge' => $_POST['age'],
                    'GiraffeOccupation' =>  $_POST['occupation'],
                );

                $this->session->set_userdata($postdata);
         }
                $GiraffeName = $this->session->userdata('GiraffeName');
                $GiraffeWork = $this->session->userdata('GiraffeWork');
                $GiraffeLocation = $this->session->userdata('GiraffeLocation');
                $GiraffeGender = $this->session->userdata('GiraffeGender');
                $GiraffeAge = $this->session->userdata('GiraffeAge');
                $GiraffeName = $this->session->userdata('GiraffeName');
                $GiraffeOccupation = $this->session->userdata('GiraffeOccupation');

答案 1 :(得分:0)

虽然之前的回答帮助我阻止了另一个问题。

我也想出了为什么分页不起作用。

由于我必须使用的表单和数据库表,我有一个构建Dynamic SELECT语句的函数,然后我用$ this-&gt; db-query()运行SQL语句。

这意味着我的$ this-&gt; db-&gt; limit()不起作用,它适用于codeigniters $ this-&gt; db-&gt; get()。

所以我在我的模型中添加了一行,我在其中添加了对SQL语句的限制和偏移量。

现在它正在工作!