CodeIgniter分页顺序按日期结果排序错误并显示相同的结果

时间:2013-07-27 00:29:59

标签: codeigniter datetime pagination sql-order-by

我不确定我在这里做错了什么。我看了一遍,但找不到我要找的东西。我想按日期订购我的结果。我不确定是否有必要知道日期不在表格中。我在每个页面上都得到了类似的结果,而不是加载下一组记录,日期也不按顺序排列。

**Model**

<?php

class Topics_Model extends CI_Model {

    public $db_table_name = 'px_topics';

    function __construct()
    {
        parent::__construct();
    }

    public function record_count()
    {
        return $this->db->count_all($this->db_table_name);
    }

    public function fetch_topics($limit, $offset)
    {
        $this->db->limit($limit, $offset);
        $this->db->order_by("date", "desc");
        $query = $this->db->get($this->db_table_name);

        if($query->num_rows() > 0) {
            foreach($query->result() as $row) {
                $data[] = $row;
            }
            return $data;
        }

        return FALSE;
    }
}

**Controller**

<?php

class Test extends MY_Controller {

    function __construct()
    {
        parent::__construct();
        $this->load->model('user_model');
        $this->load->model('topics_model');
        $this->load->library('pagination');
    }

    function index()
    {
        $data['users'] = $this->user_model->get_all();
        $data['sessid'] = $this->user_model->get_by_sess_id();
        $data['sessdata'] = $this->user_model->get_session_data();
        $data['main_content'] = 'test_view';
        $this->load->view('template', $data);
    }

    function paged()
    {
        $config = array();
        $config["base_url"] = base_url() . "test/paged";
        $config["total_rows"] = $this->topics_model->record_count();
        $config["per_page"] = 5;
        $config["uri_segment"] = 3;
        $config["use_page_numbers"] = TRUE;
        $config["next_tag_open"] = '<div class="next">';
        $config["next_tag_close"] = '</div>';
        //$choice = $config["total_rows"] / $config["per_page"];
        //$config["num_links"] = round($choice);
        $config["num_links"] = "3";

        $this->pagination->initialize($config);

        $page = ($this->uri->segment(3)) ? $this->uri->segment(3) : 0;
        $data["results"] = $this->topics_model->fetch_topics($config["per_page"], $page);
        $data["links"] = $this->pagination->create_links();
        $data["main_content"] = 'test2_view';

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

1 个答案:

答案 0 :(得分:2)

$page = ($this->uri->segment(3)) ? ($this->uri->segment(3) * $config["per_page"]) - $config["per_page"] : 0;

这有效:)