分页和限制问题

发布于 2024-12-24 16:22:26 字数 4237 浏览 0 评论 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);
    }

I have 3 tables site_settings,sermons & Preachers. In site_settings table, set the max number of preachers to be displayed. Preacher table contains all the preacher details and sermons table contains sermons of all preachers. Am using pagination for displaying preachers(2/page). if the value of site_settings table is 1 or 2 then no of preachers will display 2, if 3 or 4 then preachers 4... I don't know what is happening. Is it a problem with pagination or limit? I am using the following code for this ( In Model)

Method in Model

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;
    }

Method in controller

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

╭ゆ眷念 2024-12-31 16:22:26

正如评论中所述,您使用了两次限制,并且需要组合这些方法:

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 传递给该方法,或使用数据库中的设置。

As stated in the comments, you're using a limit twice and need to combine those methods:

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);

This effectively allows you to pass a $limit to the method, or use the settings from the db.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文