Welcome Guest, Not a member yet? Register   Sign In
codeigniter pagination: it shows the correct result until page 2 or 3
#1

(This post was last modified: 07-26-2015, 08:56 AM by codeigniter1932.)

I create a pagination with codeigniter

first i take the category id ($id) to display the related post to that category, and because the menu can has the subcategories i make the id list in an array to select post from that array: this is my

controller:

 
Code:
 function index($id=null){

   $this->load->model('category_model');
   $id_list = $this->category_model->all_category_ids($id);

   $id_list .= $id;
   $id_list = explode(',',$id_list);

   $config['base_url']= base_url()."/category/index/".$id."/page/";
   $config['total_rows'] = $this->category_model->total_category($id_list);
   $config['per_page'] = 2;
   $config['uri_segment'] = 5;
   $config['use_page_numbers'] = TRUE;    

   $this->load->library('pagination');
   $this->pagination->initialize($config);
   $pagination = $this->pagination->create_links();
   $page = ($this->uri->segment(5))?$this->uri->segment(5):0;


   $this->load->model('category_model');  
   $single_category = $this->category_model->select_category($id,$config['per_page'],$page,$id_list);

   $this->load->model('category_model');
   $all_category = $this->category_model->all_category();
   $multi_category = $this->category_model->build_multi_menu($all_category,0);        

   $data = array(
       'single_category' => $single_category,
       'multi_category' => $multi_category,
       'pagination' => $pagination
   );

   $this->template->load('category/index',$data);
}

and this is my model file: Category_model.php


Code:
function __construct(){

   parent::__construct();  

}

function all_category(){

   return $this->db->get('category')->result_array();

}

function has_children($rows, $id) {
foreach ($rows as $row) {
   if ($row['parent_id'] == $id) {
       return true;
   }
}
return false;
}

function build_multi_menu($rows, $parent = 0) {
   $result = "<ul>";
   foreach ($rows as $row) {
       if ($row['parent_id'] == $parent) {
           $id = $row['id'];
           $url = base_url();
           $result .= "<li><a href=\"$url"."category/index/$id\">{$row['category']}</a>";
           if ($this->has_children($rows, $row['id'])) {
               $result .= $this->build_multi_menu($rows, $row['id']);
           }
           $result .= "</li> \n";
       }
   }
   $result .= "</ul> \n";
   return $result;
}

function select_category($id,$limit,$start,$id_list){


   $this->db->select('*');
   $this->db->from('products');
   $this->db->join('category_post','products.id=category_post.post_id');
   $this->db->where_in('category_post.category_id',$id_list);
   $var = $this->db->limit($limit,$start)->get()->result_array();

   $var2 = array();
   foreach($var as $value){
       $postid = $value['post_id'];
       if(!in_array($value['post_id'],$var2)){

               $var2[$postid] = array(
                   'id'=> $value['post_id'],
                   'title' => $value['title'],
                   'abstract' => $value['abstract'],
                   'full-text' => $value['full-text'],
                   'price' => $value['price'],
                   'real-price' => $value['real-price'],
                   'image-name' => $value['image-name'],
                   'category_id' => $value['category_id'],
               );

       }
   }
   return $var2;
   /*echo"<pre>";
   print_r($var2);
   exit();*/

}

function total_category($id_list){

   $this->db->select('*');
   $this->db->from('products');
   $this->db->join('category_post','products.id=category_post.post_id');
   $this->db->where_in('category_post.category_id',$id_list);
   $var = $this->db->get()->result_array();

   $var2 = array();
   foreach($var as $value){
       $postid = $value['post_id'];
       if(!in_array($value['post_id'],$var2)){

               $var2[$postid] = array(
                   'id'=> $value['post_id'],
                   'title' => $value['title'],
                   'abstract' => $value['abstract'],
                   'full-text' => $value['full-text'],
                   'price' => $value['price'],
                   'real-price' => $value['real-price'],
                   'image-name' => $value['image-name']
               );

       }
   }

   $c = count($var2);
   return $c;


}


function all_category_ids($level = 0) {
   $rows = $this->db
       ->select('id,category,parent_id')
       ->where('parent_id', $level)
       ->order_by('id','asc')
       ->get('category')
       ->result();
   $id_list = null;

   if (count($rows) > 0) {

       foreach ($rows as $row) {
           $id_list .= $row->id.",";
           // Append subcategories
           $id_list .= $this->all_category_ids($row->id);
       }
   }
   return $id_list;
}


it works fine to page 2 or 3, from that page it mixed up the result and some times repeats the page 3

I really confused. I appreciate any help
Reply


Messages In This Thread
codeigniter pagination: it shows the correct result until page 2 or 3 - by codeigniter1932 - 07-26-2015, 05:23 AM



Theme © iAndrew 2016 - Forum software by © MyBB