分页和限制的问题

时间:2012-01-03 05:41:38

标签: codeigniter pagination limit

我有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);
    }

1 个答案:

答案 0 :(得分: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传递给方法,或使用数据库中的设置。

相关问题