【问题标题】:Pagination script needed to be put on view page of codeigniter分页脚本需要放在codeigniter的查看页面上
【发布时间】:2014-09-23 00:23:36
【问题描述】:

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 人员帮助我编写分页脚本,您可以完全编辑我的模型、控制器和视图。

我会感谢那些可以帮助我的人。

【问题讨论】:

  • 您不应期望我们提供任何服务。我们在这里向您展示正确的方向,而不是提高您的期望。请从您的文本中删除代码格式。只有代码应该在code format
  • 你应该学会更好地格式化你的问题。如果你想让别人为你付出努力,你应该先为自己付出一些
  • 你说的是对的,我正在尝试 3 天,但我失败了,所以我简要地发布了我的问题。我编辑了你所说的 machineaddict 先生。
  • Vicky,仍有正常的问题文本格式化为代码。请花几分钟时间来纠正它。保存时,查看问题以查看其格式是否正确。
  • 您是否将其他任何内容传递给控制器​​方法?控制器方法的名称是什么?这个页面的网址是什么?

标签: php codeigniter pagination


【解决方案1】:

控制器

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

希望这会有所帮助!

【讨论】:

  • 如果我完全复制控制器,我会得到 emty 数组并且没有得到任何输出
  • Ross ...到目前为止,您已经为我花费了很多时间..所以请您多花点时间
  • 你把方法名改成users了吗?
  • 在那种情况下,我不知道为什么这不起作用。谷歌如何开启错误日志,看看问题出在哪里。
  • $config["uri_segment"] = 3; $page = ($this->uri->segment(2))? $this->uri->segment(3) : 0;我们是不是忘了用这个罗斯//
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2021-06-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-10-03
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多