使用$ this-> db-> query()和$ this-> db-> limit()进行分页

时间:2014-05-30 09:26:36

标签: php codeigniter

我尝试使用以下代码进行分页,但它对我不起作用,它不显示每页10条记录,它显示单页上的所有记录,并且下一页显示相同数量的记录我的代码在控制器中

<?php 
class Classified_detail extends CI_Controller 
{
    public function __construct()
    {
        parent::__construct();
        $this->load->model('Jobseekermodel','',TRUE);
        //$this->load->database($config);
        //$secound_db= $this->load->database('classified_db', TRUE);
        //$CI =   &get_instance();
        $this->db2 = $this->load->database('classified_db', TRUE); //doolaly
        $this->load->model('Emailmodel','',TRUE);
        $this->load->model('Bookmodel','',TRUE);
        $this->load->model('Consultantmodel','',TRUE);
        $this->load->model('Openingmodel','',TRUE);
        //$this->load->model('classified/Mobile_model','',TRUE);
        $this->load->model('classified/classified_ad','',TRUE);
        $this->load->model('classified/create_ad','',TRUE);
        $this->load->model('Commfuncmodel','',TRUE);
        $this->lang->load('message', 'english');
        $this->lang->load('mail', 'english');
        $this->load->library('form_validation');
        if (session_id() == "") session_start();
    }

    /***********************Start of Function For Showing list of ebooks from database **********************************/      
    function index($msg=NULL)
    {   
        $this->Commfuncmodel->checkJobseekerLogin();

        //******My expired ads*******/

        //post ad history
        $_SESSION["ex_ad_paging"] = PER_PAGE_RECORDS;
        $today = date('Y-m-d');
        $qry ="select AM.Ad_Title as Title,AM.Advertizement_Text as Description,AM.Price,AMP.Expiry_Date 
        from ad_mobile_phones AM inner join ad_mobilephone_plan_mapping AMP on
        AM.Ad_ID=AMP.Ad_ID
        where AM.Created_By='".$_SESSION['jobseeker_id']."' and AMP.Expiry_Date <'".$today."'
        UNION
        select A.Title,A.Description,A.Price,B.Expiry_Date from ad_vehicle A inner join ad_vehicle_plan_mapping B 
on A.Ad_ID = B.Ad_ID where A.Created_By ='".$_SESSION['jobseeker_id']."' and B.Expiry_Date <'".$today."'";

            $query=$this->db->query($qry);

            $ex_ed_count=$query->num_rows();
            $ex_ad_data=$query->result_array();


        /******************Start of Pagination****************/
        $segments = array('classified/classified_detail','index/my_ex_ads');
        $url_name=site_url($segments);
        $ex_ed_count = count($ex_ad_data); 
        $per_page_records=((@$_SESSION["ex_ad_paging"]=="All")?$ex_ed_count:@$_SESSION["ex_ad_paging"]);
$expagination=$this->Commfuncmodel->pagination_a($ex_ed_count,$this->uri->segment(5),$per_page_records,$url_name,"1","EXAD");
        /******************End Pagination******************/

        if($this->uri->segment(4)== "my_ex_ads")
        { 
            $page_no=(int)$this->uri->segment(5);
            if( empty($page_no) || ( $page_no <1 ) )
                $nextrecord = 0 ;
            else
                $nextrecord = ($page_no-1) * @$_SESSION["ex_ad_paging"] ;
        }       
            if($_SESSION["ex_ad_paging"]!="All")
            {
                    $limit_start=$nextrecord;
                    $limit_end=$_SESSION["ex_ad_paging"];
            }
            else
            {
                $limit_start=0;
                $limit_end=$ex_ed_count;
            }

        $qry ="select AM.Ad_Title as Title,AM.Advertizement_Text as Description,AM.Price,AMP.Expiry_Date 
        from ad_mobile_phones AM inner join ad_mobilephone_plan_mapping AMP on
        AM.Ad_ID=AMP.Ad_ID where AM.Created_By='".$_SESSION['jobseeker_id']."' and AMP.Expiry_Date <'".$today."'
        UNION
        select A.Title,A.Description,A.Price,B.Expiry_Date from ad_vehicle A inner join ad_vehicle_plan_mapping B 
on A.Ad_ID = B.Ad_ID where A.Created_By ='".$_SESSION['jobseeker_id']."' and B.Expiry_Date <'".$today."'";
            //$this->db->limit(@$limit_end,@$limit_start);
            //var_dump($limit_end);
            //var_dump($limit_start);
            $query=$this->db->query($qry);
            $this->db->limit(@$limit_end,@$limit_start);
            //var_dump($query);
            $my_ad_data=$query->result_array(); 
            //old data
            //$this->db2->where('Expiry_Date' < $date);

//          $this->db2->limit(@$limit_end,@$limit_start);
            //$query=$this->db2->get(ad_mobilephone_plan_mapping);

//          $my_ad_data=$query->result_array(); 
            //follw up
            $i=0;
            if($query->num_rows()>0)
            {
                foreach($my_ad_data as $item => $v)
                {
                    //$my_ad_data[$i]['applied_js']="y";
                    //follow up mail sent
                    //$this->db2->where("Ad_ID",$my_ad_data[$i]['Ad_ID']);
                    //$this->db2->where('Expiry_Date' < $date);
                    //$query=$this->db2->get(ad_mobilephone_plan_mapping);
                    //var_dump($fquery);
                    //$my_ad_data[$i]['count'] = $fquery->num_rows();
                    $qry ="select AM.Ad_Title as Title,AM.Advertizement_Text as Description,AM.Price,AMP.Expiry_Date 
        from ad_mobile_phones AM inner join ad_mobilephone_plan_mapping AMP on
        AM.Ad_ID=AMP.Ad_ID where AM.Created_By='".$_SESSION['jobseeker_id']."' and AMP.Expiry_Date <'".$today."'
        UNION
        select A.Title,A.Description,A.Price,B.Expiry_Date from ad_vehicle A inner join ad_vehicle_plan_mapping B 
on A.Ad_ID = B.Ad_ID where A.Created_By ='".$_SESSION['jobseeker_id']."' and B.Expiry_Date <'".$today."'";
                    $fquery = $this->db->query($qry);
                    //var_dump($fquery);
                    //var_dump($my_ad_data[$i]['count']);
                    $my_ad_data[$i]['count'] = $fquery->num_rows();
                    if($fquery->num_rows() < 2)
                    {
                        $my_ad_data[$i]['sent_follow']="y";
                    }

                    $oid = substr($my_ad_data[$i]['opening_id'],-9);
                    $my_ad_data[$i]['oid'] = substr($oid,0,-1);

                    $i++;
                }
            }
            $data['acc'] = 'acc';
            $data['page_no'] = $page_no;
            $data['ex_ad_data'] = $my_ad_data;
            $data['ex_ed_count'] = $ex_ed_count;
            $data['expagination'] =$expagination;
            $data['nextrecord'] = $nextrecord;
            $data['msg_display'] =$msg_display;
            $data['view_file'] = 'classified/account';
            if($this->uri->segment(4)=="h")
            {
                $data['ajax'] = 1;
                $this->load->view('account/historylist',$data);
            }
            else if($this->uri->segment(4)=="my_ads")
            {
                $data['ajax'] = 1;
                $this->load->view('classified/classified_ads',$data);
            }
            else if($this->uri->segment(4)=="my_ex_ads")
            {
                $data['ajax'] = 1;
                $this->load->view('account/expired_ads',$data);
            }
            else if($this->uri->segment(4)=="job")
            {
                $data['ajax'] = 1;
                $this->load->view('account/joblist',$data);
            }
            else
            {               
                $this->load->view('classified/classified_layout',$data);
            }
    }

请帮助我,并提前致谢

Model Commfuncmodel.php

<?php
class Commfuncmodel extends CI_Model
{

     function __construct()
    {
        parent::__construct();
        //$this->load->model('Adminmodel','',TRUE);
    }


     function getcounter($tblnm,$id,$val)
     {
         $this->db->select('counter'); 
         $this->db->where($id,$val);
         $query = $this->db->get($tblnm);
         $data=$query->result_array();
         return $data[0];
     }

     function setcounter($tblnm,$id,$val)
     {
        $cnt = $this->getcounter($tblnm,$id,$val);
        $data['counter'] = $cnt['counter'] + 1;
        $this->db->where($id, $val);
        $this->db->update($tblnm,$data);
        return true;
     }
    /**************************PAGINATION FUNCTION START*************************************/
    function pagination($total_rec, $current_page=0, $perPage, $url_name)
    {
        $pagination = "" ; 
        //echo $current_page;
        // if total records are less than per page record then dont show pagination
        if($total_rec<= $perPage)
        {
            return ;
        }

        // Retrieve Current Page number 
        if($current_page < 0 || $current_page==0)
        {
            $p = 1 ;
        }
        else
        {
            $p = $current_page ;
        }

        //  Calculate Total Pages
        $total_pages = (int)($total_rec/$perPage) ;
        if( ($total_rec%$perPage) > 0 )
        {
            $total_pages++ ;
        }
        //echo "total pages ".$total_pages;
        //echo "p is ".$p;
        //  To show pagination
        if($total_pages >= 1)
        {
            $pagination .= '<ul class="pagination" style="margin-left:150px;">';
            //$pagination .= "<br>Showing Page ".$p." of ".$total_pages."<br>";
            //  FIRST & PRIV LINKS
            if($p>1)
            {
                //$pagination .= "<a href='".$url_name."/1'>"."First"."</a>&nbsp;" ;
                $pagination .= "<li><a style='color: #000;' href='".$url_name."/". ($p-1) ."'>"." Prev"."</a></li>" ;   
            }

            // Middle links..

            if( ($p%BEFORE_AFTER_NO) == 0)
            {
                if( ($total_pages - $p) > BEFORE_AFTER_NO)
                {
                    $start = $p-BEFORE_AFTER_NO ? $p-BEFORE_AFTER_NO:1 ;
                }

                else
                {
                    $start = $total_pages - TOTAL_PAGINATION_NO;
                    if($start <= 0)
                    {
                        $start = 1 ;
                    }   
                }   

                $end = $p+BEFORE_AFTER_NO ;
            }
            else
            {   
                if($p > 9)
                {
                    if( ($total_pages - $p) > BEFORE_AFTER_NO)
                    {
                        $start = $p - BEFORE_AFTER_NO ;
                    }
                    else
                    {
                        $start = $total_pages - TOTAL_PAGINATION_NO;
                    }       
                $end = $p + BEFORE_AFTER_NO ;
                }
                else
                {    
                $start = 1 ;

                $end = TOTAL_PAGINATION_NO;

                }   
            }   
            for($i=$start; $i<=$end && $i<=$total_pages;$i++)
            {
                if($i==$p)
                {
                    $pagination .= '<li class="active"><a href="'.$url_name.'/'.$i.'">'.$i.'</a></li>' ;
                }

                else
                {   
                    $pagination .= "<li><a style='color: #000;' href='".$url_name."/". $i ."'>".$i."</a></li>" ;
                }   
            }
            //  NEXT & LAST links
            if($p>=1 && $p<$total_pages)
            {
                $pagination .= "<li ><a style='color: #000;' href='".$url_name ."/". ($p+1) ."'>"."Next"."</a></li>" ;
                //$pagination .= "<a href='".$url_name ."/$total_pages'>"."Last"."</a>&nbsp;" ;
            }
        }   //  if($total_pages>1) end.

        $pagination .='</ul>' ;

        return ($pagination) ;
    }

    function pagination_a($total_rec, $current_page=0, $perPage, $url_name,$ajaxflag=NULL,$d)
    {
        $pagination = "" ; 
        // if total records are less than per page record then dont show pagination
        if($total_rec<= $perPage)
        {
            return ;
        }

        // Retrieve Current Page number 
        if($current_page<0 || $current_page==0)
        {
            $p = 1 ;
        }
        else
        {
            $p = $current_page ;
        }

        //  Calculate Total Pages
        $total_pages = (int)($total_rec/$perPage) ;
        if( ($total_rec%$perPage) > 0 )
        {
            $total_pages++ ;
        }

        //  To show pagination
        if($total_pages >= 1)
        {
            $pagination .= '<ul class="pagination" style="margin-left:227px;">';
            //  FIRST & PRIV LINKS
            if($p>1)
            {

                if($ajaxflag)
                {
                    //$pagination .= "<a href=javascript:ajaxPagination('".$url_name."/1')>".lang("FIRST")."</a>&nbsp;" ;
                    $pagination .= "<li><a href=javascript:ajaxPagination('".$url_name."/". ($p-1) ."/".$d."')>"."PREV"."</a></li>" ;   
                }
                else
                {
                    //$pagination .= "<a href='".$url_name."/1'>".lang("FIRST")."</a>&nbsp;" ;
                    $pagination .= "<li><a href='".$url_name."/". ($p-1) ."'>"."PREV"."</a></li>" ; 
                }
            }

            // Middle links..
            //echo $total_pages;
            if( ($p%BEFORE_AFTER_NO) == 0)
            {
                if( ($total_pages - $p) > BEFORE_AFTER_NO)
                {
                    $start = $p-BEFORE_AFTER_NO ? $p-BEFORE_AFTER_NO:1 ;
                }

                else
                {
                    $start = $total_pages - TOTAL_PAGINATION_NO;
                    if($start <= 0)
                    {
                        $start = 1 ;
                    }   
                }   

                $end = $p+BEFORE_AFTER_NO ;
            }
            else
            {   
                if($p > 9)
                {
                    if( ($total_pages - $p) > BEFORE_AFTER_NO)
                    {
                        $start = $p - BEFORE_AFTER_NO ;
                    }
                    else
                    {
                        $start = $total_pages - TOTAL_PAGINATION_NO;
                    }       
                $end = $p + BEFORE_AFTER_NO ;
                }
                else
                {    
                $start = 1 ;

                $end = TOTAL_PAGINATION_NO;

                }   
            }   
            for($i=$start; $i<=$end && $i<=$total_pages;$i++)
            {
                if($i==$p)
                {
                    $pagination .= '<li class="active"><a class="page-links" id="current">'.$i.'</a></li> ' ;
                }

                else
                {   
                    if($ajaxflag)
                        $pagination .= "<li><a href=javascript:ajaxPagination('".$url_name."/". $i ."/".$d."')>$i</a></li>" ;
                    else
                        $pagination .= "<li><a href='".$url_name."/". $i ."'>$i</a></li>" ;
                }   
            }
            //  NEXT & LAST links
            if($p>=1 && $p <$total_pages)
            {
                if($ajaxflag)
                {
                    $pagination .= "<li><a href=javascript:ajaxPagination('".$url_name ."/". ($p+1) ."/".$d."')>"."NEXT"."</a></li>";
                    //$pagination .= "<a href=javascript:ajaxPagination('".$url_name ."/$total_pages')>".lang("LAST")."</a>&nbsp;" ;
                }   
                else
                {
                    $pagination .= "<li><a href='".$url_name ."/". ($p+1) ."'>"."NEXT"."</a></li>";
                    //$pagination .= "<a href='".$url_name ."/$total_pages'>".lang("LAST")."</a>&nbsp;" ;
                }
            }
        }   //  if($total_pages>1) end.

        $pagination .='</ul>';
        //echo $pagination;

        return ($pagination) ;
    }
}
?>

0 个答案:

没有答案