1

我不确定我在这里做错了什么。我已经翻遍了,但找不到我要找的东西。我正在尝试按日期排序我的结果。我不确定知道日期在表格中是否按顺序排列是否会有所帮助。我在每个页面上都得到了类似的结果,而不是加载下一组记录并且日期不正确。

**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);
    }
}
4

1 回答 1

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

这有效:)

于 2013-07-28T14:57:56.833 回答