分页脚本需要放在codeigniter的查看页面上

时间:2014-07-31 06:48:06

标签: php codeigniter pagination

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'];
?>

这里开始我的问题我发布完整的控制器,模型,查看o / p我做了什么,我想要一些能帮我编辑模型,控制器,视图的人可以在分页中显示结果。

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

我会感谢能帮助我的人。

1 个答案:

答案 0 :(得分:0)

<强>控制器

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查询中使用LIMIT

查看

<?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 />';
    }
}

希望这有帮助!