2013-07-27 72 views
1

我不知道我在做什麼錯在這裏。我看遍了所有,但找不到我在找什麼。我試圖按日期排列我的結果。但我不確定是否有助於瞭解日期在表格中的順序不正確。我在每個頁面上獲得了類似的結果,而不是加載下一組記錄,並且日期不合適。CodeIgniter分頁順序按日期結果錯誤,並顯示相同的結果

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

可以包括基本URL和分頁鏈接生成一個URL,如果他們通常顯示相同的記錄的URI段設置不正確 –

+0

我有我的基礎URL和索引頁面設置就像這樣,因爲我已經用乾淨的URL設置了它... $ config ['base_url'] \t =''; $ config ['index_page'] =''; –

+0

是的,但你已經把你的uri段設置爲$ config [「uri_segment」] = 3;所以你需要分頁參數是類似domain.com/home/pages/$no的東西,如果它不是這樣的話,你會得到相同的內容返回你的查詢 –

回答

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

這工作:)

相關問題