分页CI合并表

时间:2012-10-18 03:24:56

标签: php codeigniter pagination limit offset

我需要在联合桌上的CI分页方面提供帮助。

以下代码无效。所有信息都显示在单个页面中,但链接($ this-> pagination-> create_links();)显示正确分页时应具有的正确页面数量。每当我点击下一页或其他页面时,它都会显示相同的结果。任何使这项工作成功的建议。谢谢。

public function last_three_months(){
$this->load->model('mymodel');
$this->load->database();
$this->load->library('btree');
$this->load->library('session');
$this->load->library('string');
$this->load->library("Pagination");



    if(date("l",strtotime('now')) == "Monday")
        $isMonday = strtotime('now');
    else
        $isMonday = strtotime('last Monday');
    $min = $isMonday - 7257600; // 7257600 = 12 weeks
    $max = strtotime('now');
    //echo(date("l d M Y",$duration));  

    $result = $this->mymodel->query('SELECT DISTINCT pi.ID PLAN_ID, pi.IMP_PLAN_DATE DUE_DATE, pi.CHANGE_TICKET CHANGE_TICKET, 
    pi.PLAN_DESC PLAN_DESC, pi.EC_STAT EC_STAT, per_info.ID PERSON_ID, per_info.FIRSTNAME OWNER_FIRST,per_info.LASTNAME OWNER_LAST,ps.STATUS PLAN_STAT, 
    pi.TIMESTAMP TIMESTAMP, sn.SN SN, loc.ABBR ABBR, sf.SF SF, pn.ID pn_id, pn.TYPE pn_type, pn.CODE pn_code, pn.YEAR pn_year, pn.NAME pn_name, si.IMP_STAT IMP_STAT
    FROM `Plan_Info` pi, `Member` member,`Person_Info` per_info,`Plan_Stat` ps,`Sn_Imp` si,`System_Name` sn,
    `Location` loc, System_Family sf, PN pn
    WHERE member.MEM_TYPE = "Implementer"  AND pi.OWNER = member.PERSON_ID AND member.PERSON_ID = per_info.ID AND pi.PLAN_STAT = ps.ID AND pi.ID = si.PLAN_ID AND si.SN_ID = sn.ID AND sn.LOC_ID = loc.ID AND sn.SF_ID = sf.ID 
    AND pi.PN_ID = pn.ID  AND  pi.IMP_PLAN_DATE >= '.$min. ' AND pi.IMP_PLAN_DATE < '.$max.' ORDER BY pi.IMP_PLAN_DATE, loc.ABBR, sf.SF')->result_array();      

    $plan_info = $this->btree->convertToBtree($result, array('PLAN_ID','DUE_DATE','CHANGE_TICKET','PLAN_DESC','EC_STAT','PERSON_ID','OWNER_FIRST',  
    'OWNER_LAST','PLAN_STAT','TIMESTAMP','SN','ABBR','SF','pn_id','pn_type','pn_code','pn_year','pn_name','IMP_STAT'), array('PLAN_ID','pn_id','DUE_DATE','ABBR'));

    $total = count($plan_info);

    $config = array();
    $config["base_url"] = base_url() . 'index.php/implementer/last_three_months/';
    $config["total_rows"] = $total;
    $config["per_page"] = 3;
    $config["uri_segment"] = 3;
    $config['first_link'] = 'First';
    $config['last_link'] = 'Last';
    $config['next_link'] = 'next';
    $config['prev_link'] = 'prev';
    //$config['use_page_numbers'] = TRUE;
    $this->pagination->initialize($config);         




    $link = $this->pagination->create_links();
    $content_data = array(
        'plan_info' => $plan_info,
        'link' => $link

    );

    $this->load->view('implementer/view_weekly/header');
    $this->load->view('logo');
    $this->load->view('nev');
    $this->load->view('implementer/view_weekly/content',$content_data);
    $this->load->view('implementer/view_weekly/footer');

}

1 个答案:

答案 0 :(得分:1)

首先,你完全搞砸了MVC的设计理念。尽管如此,这是一个广泛的主题,这不是你所要求的,所以我将集中精力解决你的问题。

public function last_three_months(){
$this->load->model('mymodel');
$this->load->database();
$this->load->library('btree');
$this->load->library('session');
$this->load->library('string');
$this->load->library("Pagination");

    $per_page = 10;
    $page = $this->uri->segment(3, 1);
    $limit = ($page-1)* $per_page;




    if(date("l",strtotime('now')) == "Monday")
        $isMonday = strtotime('now');
    else
        $isMonday = strtotime('last Monday');
    $min = $isMonday - 7257600; // 7257600 = 12 weeks
    $max = strtotime('now');
    //echo(date("l d M Y",$duration));


//Change the following query to COUNT(id) or something like that.
    $sql = 'SELECT DISTINCT pi.ID PLAN_ID, pi.IMP_PLAN_DATE DUE_DATE, pi.CHANGE_TICKET CHANGE_TICKET, 
    pi.PLAN_DESC PLAN_DESC, pi.EC_STAT EC_STAT, per_info.ID PERSON_ID, per_info.FIRSTNAME OWNER_FIRST,per_info.LASTNAME OWNER_LAST,ps.STATUS PLAN_STAT, 
    pi.TIMESTAMP TIMESTAMP, sn.SN SN, loc.ABBR ABBR, sf.SF SF, pn.ID pn_id, pn.TYPE pn_type, pn.CODE pn_code, pn.YEAR pn_year, pn.NAME pn_name, si.IMP_STAT IMP_STAT
    FROM `Plan_Info` pi, `Member` member,`Person_Info` per_info,`Plan_Stat` ps,`Sn_Imp` si,`System_Name` sn,
    `Location` loc, System_Family sf, PN pn
    WHERE member.MEM_TYPE = "Implementer"  AND pi.OWNER = member.PERSON_ID AND member.PERSON_ID = per_info.ID AND pi.PLAN_STAT = ps.ID AND pi.ID = si.PLAN_ID AND si.SN_ID = sn.ID AND sn.LOC_ID = loc.ID AND sn.SF_ID = sf.ID 
    AND pi.PN_ID = pn.ID  AND  pi.IMP_PLAN_DATE >= '.$min. ' AND pi.IMP_PLAN_DATE < '.$max.' ORDER BY pi.IMP_PLAN_DATE, loc.ABBR, sf.SF';

    $sql2 = 'SELECT DISTINCT pi.ID PLAN_ID, pi.IMP_PLAN_DATE DUE_DATE, pi.CHANGE_TICKET CHANGE_TICKET, 
    pi.PLAN_DESC PLAN_DESC, pi.EC_STAT EC_STAT, per_info.ID PERSON_ID, per_info.FIRSTNAME OWNER_FIRST,per_info.LASTNAME OWNER_LAST,ps.STATUS PLAN_STAT, 
    pi.TIMESTAMP TIMESTAMP, sn.SN SN, loc.ABBR ABBR, sf.SF SF, pn.ID pn_id, pn.TYPE pn_type, pn.CODE pn_code, pn.YEAR pn_year, pn.NAME pn_name, si.IMP_STAT IMP_STAT
    FROM `Plan_Info` pi, `Member` member,`Person_Info` per_info,`Plan_Stat` ps,`Sn_Imp` si,`System_Name` sn,
    `Location` loc, System_Family sf, PN pn
    WHERE member.MEM_TYPE = "Implementer"  AND pi.OWNER = member.PERSON_ID AND member.PERSON_ID = per_info.ID AND pi.PLAN_STAT = ps.ID AND pi.ID = si.PLAN_ID AND si.SN_ID = sn.ID AND sn.LOC_ID = loc.ID AND sn.SF_ID = sf.ID 
    AND pi.PN_ID = pn.ID  AND  pi.IMP_PLAN_DATE >= '.$min. ' AND pi.IMP_PLAN_DATE < '.$max.' ORDER BY pi.IMP_PLAN_DATE, loc.ABBR, sf.SF LIMIT '.$limit.', '.$per_page;

     $total =  $this->mymodel->query($sql)->num_rows();

    $result = $this->mymodel->query($sql2)->result_array();      

    $plan_info = $this->btree->convertToBtree($result, array('PLAN_ID','DUE_DATE','CHANGE_TICKET','PLAN_DESC','EC_STAT','PERSON_ID','OWNER_FIRST',  
    'OWNER_LAST','PLAN_STAT','TIMESTAMP','SN','ABBR','SF','pn_id','pn_type','pn_code','pn_year','pn_name','IMP_STAT'), array('PLAN_ID','pn_id','DUE_DATE','ABBR'));

    $total = count($plan_info);

    $config = array();
    $config["base_url"] = base_url() . 'index.php/implementer/last_three_months/';
    $config["total_rows"] = $total;
    $config["per_page"] = $per_page;
    $config["uri_segment"] = 3;
    $config['use_page_numbers'] = TRUE;
    $config['first_link'] = 'First';
    $config['last_link'] = 'Last';
    $config['next_link'] = 'next';
    $config['prev_link'] = 'prev';
    //$config['use_page_numbers'] = TRUE;
    $this->pagination->initialize($config);         




    $link = $this->pagination->create_links();
    $content_data = array(
        'plan_info' => $plan_info,
        'link' => $link

    );

    $this->load->view('implementer/view_weekly/header');
    $this->load->view('logo');
    $this->load->view('nev');
    $this->load->view('implementer/view_weekly/content',$content_data);
    $this->load->view('implementer/view_weekly/footer');

}