分页链接不能正常工作

时间:2014-08-01 16:02:49

标签: php codeigniter codeigniter-url codeigniter-routing

分页链接显示正常,但是当我选择链接2时,结果未显示且网址将更改为/kkci/searchresult/users/1,当它应更改为/kkci/searchresult/users/2时。

当我选择下一个数字链接时,不会获取数据。那可能是什么错误?我无法通过谷歌获得答案,我不确定会发生什么,所以我发布下面的代码请仔细阅读并帮助我。

当我点击分页链接的第2位时,echo var_dump()显示Result为空,我没有获得echo $data->email的任何值。 但是当页面加载问题是我选择分页链接时它正在显示,但它正在显示。

我是CI的新手,要求CI人帮我显示分页2和3的结果。总共有3条记录我正在显示单页记录。

我已经多次浏览过脚本,但是没有用,所以请点击此处查找解决方案。

控制器:

public function users($limit=1,$offset = 0)
{
    $this->load->helper('url');
    $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();

    $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');
    $config = array();
    $config['base_url'] = base_url("searchresult/users/");
    $config['total_rows'] = count($results);
    $config['per_page'] = $limit;
    $this->load->library('pagination', $config);
    $data['pagination_links'] = $this->pagination->create_links();
    $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   

           gender = '$look'
        And status='1'"
    )->result(); //Returning this to controller    
 }

查看

if (empty($results)) {
    echo 'Results set is empty';
} else {
    foreach ($results as $data) {
        echo $data->email.'<br />';
    }
} 

echo $pagination_links;//Iam able to display pagination link in my view page.

我是ci的新手,所以请求ci人帮我编写分页脚本。

提前感谢帮助我。

1 个答案:

答案 0 :(得分:0)

哇哇哇哇......你根本就没有发送$offset!因此,正如您在Controller中所述,偏移量为0,因此您将永远不会得到结果

$data['results'] = array_slice($results, $offset, $limit);

<强>解决方案:

更改方法声明
public function users($limit=1,$offset = 0)

public function users($limit=1,$offset = 10)

或始终确定您在网址中发送偏移量:

/kkci/searchresult/users/1/10

我认为您的问题可能在于您发送的分页网址,您是否可以检查其显示的目的地?因为您的链接href可能类似/kkci/searchresult/users//1,这可以解释为什么你总是转到第1页:Ci将值变为空,并将其表示为1.如果情况也是如此,请从

更改
$config['base_url'] = base_url("searchresult/users/");`

// Note that I removed the trailing slash:
$config['base_url'] = base_url("searchresult/users"); 

无论如何,你应该在使用它之前检查偏移值,并在SQL中限制giben的结果数量,因为当你有一个包含几千个结果的表时,很容易将它们分页。 SQL使用LIMIT 10,20比一个疯狂的大数组的PHP切片数组。啊,当返回的行数是“我不记得数字,但我认为是大约3000”时,CodeIgniter Active Record不会让你回来超过。

相关问题