使用分页库

时间:2017-08-15 09:48:02

标签: php codeigniter pagination

我使用默认的codeiginter分页库。我尝试在之前创建的页面中实现此功能,该页面显示了所有空缺,但由于我们在网站上获得了很多空间,因此性能非常糟糕。这就是我在这个页面上需要分页的原因。请注意,这不是最干净的解决方案,有一条新的轨道可以检修整个页面并从头开始。这是一个快速的&肮脏的解决方案,因为我们需要让它在我们的实时环境中工作,直到返工完成。

这是我的控制器代码:

public function overviewVacancies($city = null)
{
    $this->is_logged_in();

    // Load Models
    $this->load->model('vacancy/vacancies_model');
    $this->load->model('perk/interests_model');
    $this->load->model('perk/engagement_model');
    $this->load->model('user/userSavedVacancies_model');

    // Passing Variables
    $data['title'] = lang("page_title_activities_overview");
    $data['description'] = lang('meta_desc_activities');
    $data['class'] = 'vacancy';
    $data['engagements'] = $this->engagement_model->getAll();
    $data['interests'] = $this->interests_model->getAllInterests();
    $data['bread'] = $this->breadcrumbmanager->getDashboardBreadcrumb($this->namemanager->getDashboardBreadName("0"), $this->namemanager->getDashboardBreadName("1"));
    $data['tasks'] = $this->interests_model->getAllTasks();

    // Set session data
    $this->session->set_userdata('previous',"http://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]");
    $this->session->set_userdata('previous-title', "1");

    $filterdata = array(
        'interests' => $this->input->post('interests'),
        'skills' => $this->input->post('skills'),
        'fulldate' => $this->input->post('daterange'),
        'location' => $this->input->post('location'),
        'city' => $this->input->post('sublocality_level_1'),
        'capital' => $this->input->post('locality')
    );
    if (!empty($filterdata['interests'])) {
        $filterdata['interests'] = rtrim($filterdata['interests'], ";");
        $filterdata['interests'] = str_replace(' ', '', $filterdata['interests']);
        $filterdata['interests'] = str_replace(';', ',', $filterdata['interests']);
    }
    if (!empty($filterdata['skills'])) {
        $filterdata['skills'] = str_replace(' ', '', $filterdata['skills']);
        $filterdata['skills'] = explode(",", $filterdata['skills']);
    }
    //Manually clear the commune and city variables if the location was empty
    if (empty($filterdata['location'])) {
        $filterdata['city'] = '';
        $filterdata['capital'] = '';
    }

    if($city == null){
        $orgId = $this->organization_model->getOrgIdByName(LOCATION);
    }
    else{
        $orgId = $this->organization_model->getOrgIdByName($city);
        $data['bread'] = $this->breadcrumbmanager->getLocalBreadcrumb($this->namemanager->getDashboardBreadName("0"), $city, $data['title'], $data['vacancydetails']);
    }

    //Set the location to the subdomain automatically (e.g. when the link is clicked) so the activities of that subdomain only show up
    if (!empty(LOCATION)) {
        $data['title'] = sprintf(lang('page_title_local_activities'), ucwords(LOCATION));
        $data['description'] = sprintf(lang('meta_desc_local_activities'), ucwords(LOCATION));
        $filterdata['location'] = LOCATION;
        $data['bgcolor'] = $this->localSettings_model->getBgColorHexValueForOrgId($orgId->org_id);
    }

    if (!empty($filterdata['fulldate'])) {
        $splitfromandto = explode(" - ", $filterdata['fulldate']);
        $filterdata['datefrom'] = $splitfromandto[0];
        $filterdata['dateto'] = $splitfromandto[1];
    } else {
        $filterdata['datefrom'] = null;
        $filterdata['dateto'] = null;
    }

    //Put these variables in the data variable so we can prefill our filters again with the previous values
    //This is necessary because we don't use AJAX yet
    $data['filterdata'] = $filterdata;

    //Pagination : We do it here so we can re-use the filter query to count all our results
    $this->load->library('pagination');
    $data['all_vacancies'] = $this->vacancies_model->getFilteredVacancies($filterdata);
    $pagconfig['base_url'] = base_url().VACANCY_OVERVIEW;
    $pagconfig['total_rows'] = count($data['all_vacancies']);
    $pagconfig['per_page'] = 1;
    $pagconfig['uri_segment']  = 2;
    $pagconfig['use_page_numbers'] = TRUE;
    $this->pagination->initialize($pagconfig);
    $data['links'] = $this->pagination->create_links();
    $start = max(0, ( $this->uri->segment(2) -1 ) * $pagconfig['per_page']);

    //This variable contains all the data necessary for a vacancy to be displayed on the vacancy overview page
    $data['vacancies'] = $this->vacancies_model->getFilteredVacancies($filterdata, false, null, false, null, false, false, $pagconfig['per_page'], $start);

    // Template declaration
    $partials = array('head' => '_master/header/head', 'navigation' => '_master/header/navigation_dashboard', 'content' => 'dashboard/vacancy/overview', 'footer' => '_master/footer/footer');
    $data['vacancygrid'] = $this->load->view('dashboard/vacancy/vacancygrid', $data, TRUE);
    $this->template->load('_master/master', $partials, $data);
}

正如您在代码中看到的,我保留了一个名为$ filterdata的变量,它包含了我们过滤器中使用的所有数据,因为我们在这个版本中没有使用AJAX,我们需要每次都将它传递给视图再次填写并呈现给访客。 但是,使用分页会中断,因为它只是重新加载我的控制器方法,因此$ filterdata中的值会丢失。

我该如何解决这个问题?

注意:它不一定是一个干净的解决方案,它只需要工作,因为无论如何这个页面将在几周后离线。

提前多多感谢!

0 个答案:

没有答案