分页脚本需要放在codeigniter的视图页面上


Pagination script needed to be put on view page of codeigniter

CI开发人员我的要求是:

我想把分页脚本,这将工作在我的视图页,即在我的结果页。

$per_page=$this->input->post('per_page');
$look = $this->input->post('look');
$age = $this->input->post('age'); 
$age_from = $this->input->post('age_from');
$age_to = $this->input->post('age_to');
$se_ct = $this->input->post('sect');
$subsect = $this->input->post('subsect');
$coun_try = $this->input->post('country');
$sta_te = $this->input->post('state');
$ci_ty = $this->input->post('city');
$qualification = $this->input->post('qualification');
$data['showdata']=$this->searchresultss->login($per_page,$look,$age, $age_to,$age_from,$se_ct,$subsect,$coun_try,$sta_te, $ci_ty,$qualification);
$this->load->view('searchresult',$data);

下面是我的模型:

public function login ($per_page=3,$look,$age,$age_to,$age_from,$se_ct,$subsect,$coun_try, $sta_te, $ci_ty,$qualification)
{
    $query="SELECT *
        FROM users
        WHERE  
        if('$se_ct'!='',sect =  '$se_ct' AND if('$subsect' !='',subsect =  '$subsect',subsect like  '%%'),sect like  '%%' AND subsect like  '%%')
        AND
        IF( '$coun_try' !='', country =  '$coun_try'
        AND 
        if('$sta_te' !='', state =  '$sta_te'
        AND  
        if('$ci_ty' !='',city =  '$ci_ty',city like  '%%'),state LIKE  '%%'
        AND city LIKE  '%%'), country LIKE  '%%'
        AND state LIKE  '%%'
        AND city LIKE  '%%' ) 
        AND age >=  '$age_from'
        AND age <=  '$age_to'
        AND 
        IF('$qualification' !='',qualification =  '$qualification',  qualification LIKE  '%%' ) 
        And gender = '$look'
        And status='1'";
    $data=array();
    $query=$this->db->query($query);
    $data['results']=$query->result_array();
    $data['count']=$query->num_rows();
    $data['pages']=ceil($data['count']/3);
    return $data;
}

下面是我的结果页面,即视图页面

<?php
if (isset($showdata)){
    foreach ($showdata['results'] as $k => $v) {
?>
        <?php echo $v['gender']; echo $v['email'];?>
<?php
    }
}
echo $showdata['count'];  echo $showdata['pages'];
?>

这里开始我的问题,我张贴完整的控制器,模型,视图/p我做了什么,我想有人可以帮助我编辑我的模型,控制器,视图,可以显示分页结果。

我是全新的CI,所以我要求所有CI家伙帮助我在分页脚本,如果需要的话,你可以完全编辑我的模型,控制器和视图。

我将感激那些能帮助我的人。

控制器

public function users($offset = 0)
{
    $this->load->library('session');
    if ($postdata = $this->session->flashdata('post_data')) {
        foreach (unserialize($postdata) as $key => $value) {
            $_POST[$key] = $value;
        }
    }
    $this->session->set_flashdata('post_data', serialize($this->input->post()));
    $data = array();
    $limit = $this->input->post('per_page');
    $look = $this->input->post('look');
    $age = $this->input->post('age');
    $age_from = $this->input->post('age_from');
    $age_to = $this->input->post('age_to');
    $se_ct = $this->input->post('sect');
    $subsect = $this->input->post('subsect');
    $coun_try = $this->input->post('country');
    $sta_te = $this->input->post('state');
    $ci_ty = $this->input->post('city');
    $qualification = $this->input->post('qualification');
    $results = $this->searchresultss->login($look, $age, $age_to, $age_from, $se_ct, $subsect, $coun_try, $sta_te, $ci_ty, $qualification);
    //for base_url()
    $this->load->helper('url');
    //Pagination Config
    $config = array();
    $config['base_url'] = base_url("searchresults/users/");
    $config['total_rows'] = count($results);
    $config['per_page'] = $limit;
    $this->load->library('pagination', $config);
    $data['pagination_links'] = $this->pagination->create_links();

    //Reducing the number of results for the view
    /** NOTE: This is not the most efficient way **/
    $data['results'] = array_slice($results, $offset, $limit);

    $this->load->view('searchresult', $data);
}

我知道这只是一个问题,你可能没有把你所有的代码在这里,但如果你有,你真的需要看看使用表单验证,因为这个代码是潜在的非常脆弱!

public function login($look, $age, $age_to, $age_from, $se_ct, $subsect, $coun_try, $sta_te, $ci_ty, $qualification)
{
        return $this->db->query("SELECT *
        FROM users
        WHERE  
        if('$se_ct'!='', sect =  '$se_ct' AND if('$subsect' !='',subsect =  '$subsect',subsect like  '%%'),sect like  '%%' AND subsect like  '%%')
        AND
        IF( '$coun_try' !='', country =  '$coun_try'
        AND 
        if('$sta_te' !='', state =  '$sta_te'
        AND  
        if('$ci_ty' !='',city =  '$ci_ty',city like  '%%'),state LIKE  '%%'
        AND city LIKE  '%%'), country LIKE  '%%'
        AND state LIKE  '%%'
        AND city LIKE  '%%' ) 
        AND age >=  '$age_from'
        AND age <=  '$age_to'
        AND 
        IF('$qualification' !='',qualification =  '$qualification',  qualification LIKE  '%%' ) 
        And gender = '$look'
        And status='1'")->result();
}

根据可能产生的行数,我会考虑添加另一种方法来计算行数,然后您可以在上述模型的SQL查询中使用use LIMIT

<<p> 视图/strong>
<?php
echo $pagination_links;
if (empty($results)) {
    echo 'Results set is empty';
} else {

    /**
     * The code between the "testing comment can be removed, however, use it to check that you are typing the parameter names corrent (remember it is case-sensitive)" 
     * Remove the code between the "Testing" comments when you're happy.
     */

    //Testing
    echo '<pre>';
    echo var_dump($results);
    echo '</pre><br /><br />';
    //Testing End

    foreach ($results as $result) {
        echo $result->email.'<br />';
    }
}

希望这对你有帮助!