2012-01-03 79 views
0

我有3個表site_settings,佈道&傳教士。在site_settings表中,設置要顯示的傳道者的最大數量。傳道者表包含所有傳道人的細節,講道表包含所有傳道人的講道。我正在使用分頁顯示傳教士(2 /頁)。如果site_settings表的值是1或2,那麼沒有任何傳教士會顯示2,如果3或4然後傳教士4 ...我不知道發生了什麼。這是分頁還是限制問題?我使用下面的代碼爲這個(在模型中)問題與分頁和限制

方法型號

function viewAll($offset=0, $limit=null) { 

     $this->db->select('settings_value'); 
     $this->db->from('site_settings'); 
     $this->db->where('settings_name', 'preachercount'); 
     $count = $this->db->get()->row(); 
     echo $count->settings_value; 

     $preacher = array(); 
     $this->db->select('preacher.preacher_id,preacher.first_name,preacher.last_name,preacher.preacher_image,preacher.preacher_logo,preacher.preacher_bio_brief'); 
     $this->db->distinct('preacher.preacher_id'); 
     $this->db->from('preacher'); 
     $this->db->join('sermons', 'sermons.preacher_id=preacher.preacher_id'); 
     $this->db->order_by('preacher.sort_order'); 
     $this->db->limit($count->settings_value); 
     $query = $this->db->get('', $limit, $offset); 
     if ($query->num_rows() > 0) { 

      foreach ($query->result() as $row) { 
       $preacher[$row->preacher_id]['preacher_id'] = $row->preacher_id; 
       $preacher[$row->preacher_id]['preacher_name'] = $row->first_name . ' ' . $row->last_name; 
       $preacher[$row->preacher_id]['preacher_image'] = $row->preacher_image; 
       $preacher[$row->preacher_id]['preacher_logo'] = $row->preacher_logo; 
       $preacher[$row->preacher_id]['preacher_bio_brief'] = $row->preacher_bio_brief; 

       $this->db->select('*'); 
       $this->db->from('sermons'); 
       $this->db->where('preacher_id', $row->preacher_id); 
       $this->db->order_by('sort_order '); 
       $sermon_array = array(); 

       $query = $this->db->get(); 
       if ($query->num_rows() > 0) { 
        foreach ($query->result() as $row1) { 
         $sermon_array[$row1->sermon_id] ['sermon_image'] = $row1->sermon_image; 
         $sermon_array[$row1->sermon_id] ['sermon_title'] = $row1->sermon_title; 
         $sermon_array[$row1->sermon_id] ['audio_file'] = $row1->audio_file; 
         $sermon_array[$row1->sermon_id] ['sermon_description'] = $row1->sermon_description; 
        } 
       } 
       $preacher[$row->preacher_id]['sermon'] = $sermon_array; 
      } 
      return $preacher; 
     } 
     return false; 
    } 

方法控制器

function list() { 

     $paginate_segment = $this->uri->segment(3) ? $this->uri->segment(3) : 0; 
     $winner_list = $this->sermon_model->viewAllpreachers(0, null); 
     $config['base_url'] = base_url() . 'sermons/index/'; 
     $config['total_rows'] = ($winner_list) ? count($winner_list) : 0; 
     $config['per_page'] = 2; 
     $config['full_tag_open'] = ''; 
     $config['full_tag_close'] = ''; 
     $config['uri_segment'] = 3; 
     $config['num_links'] = 4; 
     $config['display_pages'] = TRUE; 
     $config['first_link'] = 'First'; 
     $config['first_link'] = 'First'; 
     $config['first_tag_open'] = '<div>'; 
     $config['first_tag_close'] = '</div>'; 
     $config['last_link'] = 'Last'; 
     $config['next_link'] = 'Next'; 
     $config['prev_link'] = 'Prev'; 
     $config['cur_tag_close'] = ' | '; 
     $config['num_tag_close'] = ' | '; 

     //initialize pagination 
     $this->pagination->initialize($config); 
     $this->data['preachers'] = $this->sermon_model->viewAllpreachers($paginate_segment, $config['per_page']); 
     $this->data['links'] = $this->pagination->create_links(); 
     $this->data['page'] = $this->config->item('APP_template_dir') . 'site/home/sermons_view'; 
     $this->load->vars($this->data); 
     $this->load->view($this->_container); 
    } 
+0

您正在限制兩次。如果沒有將$ limit作爲參數傳遞,您是否希望「回退」到設置表? – landons 2012-01-03 06:25:22

+0

我沒有得到你..你可以解釋 – Natasha 2012-01-03 06:32:32

+0

你是通過1)顯式調用$ this-> db-> limit(),以及2)$ limit和$ offset參數來限制結果$ this-> db-> get()。我的猜測是你傳遞NULL作爲$限制值,而我不知道CI如何處理。 – landons 2012-01-03 06:36:07

回答

0

正如評論所說,您使用的是限兩次,需要結合這些方法:

function viewAll($offset=0, $limit=null) { 

    $this->db->select('settings_value'); 
    $this->db->from('site_settings'); 
    $this->db->where('settings_name', 'preachercount'); 
    $count = $this->db->get()->row(); 

    // now use value of [$count] if [$limit] is null - else use [$limit] 
    $limit = (is_null($limit)) ? $count : $limit ; 

    $preacher = array(); 
    $this->db->select('...'); 
    $this->db->distinct('preacher.preacher_id'); 
    $this->db->from('preacher'); 
    $this->db->join('sermons', 'sermons.preacher_id=preacher.preacher_id'); 
    $this->db->order_by('preacher.sort_order'); 
    // then remove the [limit()] call 
    // $this->db->limit($count->settings_value); <-- NOT NEEDED 
    $query = $this->db->get('', $limit, $offset); 

這實際上可以讓你通過一個$limit的方法,或使用數據庫中的設置。