Welcome Guest, Not a member yet? Register   Sign In
Pagination problem
#1

[eluser]penta997[/eluser]
Helo. I'm trying to make a items pagination. I have 3 function, first displaying category, 2nd displaying sucategory. 3rd displaying items is cold get_books_by_subcategory. 3rd function get a segment->url(3) argumnet, i want to make a pagination in the same function. but i cant do it.


This is functions code in controller:
Code:
function get_category()
        {
          
            $query = $this->Kategorie_model->get_category();
            $this->response['podkategorie'] = '';
            $this->response['kategorie'] = '';
            $podkategorie = '';
            
            


            if($query->num_rows() > 0)
            {
                foreach($query->result() as $item)
                {
                    $podkategorie = $this->get_sub_category($item->CAT_ID);
                    
                    $this->response['kategorie'] .=  $this->load->view('Ksiegarnia/left', array('kategorie' =>$item, 'podkategorie'=>$podkategorie), true);
                }
            }

            $data = $this->response['kategorie'];
            return $data;

        }





         function get_sub_category($id)
        {

            
               $this->response['wynik'] = '';

          
               $query = $this->Kategorie_model->get_sub_category($id);
              
               if($query->num_rows() > 0)
               {


                    foreach($query->result() as $row)
                    {

                       $link = site_url('ksiegarnia/get_books_by_subcategory/'.$row->SUBC_ID);
                        $this->response['wynik'] .= '<div class="subcat_name"><a href = "'.$link.'">'.$row->SUBC_Name.'</a></div>';

                    }
               }
               else
               {
                    $this->response['wynik'] = '<H1>BRAK DANYCH </H1>';
               }

          
                return $this->response['wynik'];
        }



        function get_books_by_subcategory()
        {
            $widok['center'] = '';
             $widok['left'] = $this->get_category();
             $widok['right'] = $this->load->view('Ksiegarnia/right', '', true);
              $id = $this->uri->segment(3);
            if(isset($id) and is_numeric($id))
            {
                 $query = $this->Kategorie_model->get_books_by_subcategory($id, $this->uri->segment(4));


                 if($query->num_rows() > 0)
                 {
                        
                    foreach($query->result() as $item)
                    {

                      
                        $widok['center'] .=  $this->load->view('Ksiegarnia/get_books', array('data' =>$item), true);

                    }
                }
                else
                {
                    $widok['center'] = $this->load->view('Ksiegarnia/get_books', array('tytul' =>'<h1>brak danych</h1>'), true);;
                }

                $widok['center'] .= $this->pagination->create_links();
                 $this->load->view('Ksiegarnia/index', $widok);

            }

          

        }

And this is my model:
Code:
function get_books_by_subcategory($id, $offset=0)
        {

                      $config['base_url'] = 'http://lukaszbielecki.cba.pl/ksiegarnia/CI/index.php/ksiegarnia/get_books_by_subcategory/'.$id.'/'.$offset;
                      $config['per_page'] = 7;
                      $this->db->where('SUB_CATEGORY_SUBC_ID', $id);


                      $config['total_rows'] = $this->db->get('books')->num_rows();
                      $config['num_links'] = 20;

                       $this->pagination->initialize($config);
             return   $this->db->get('books',$config['per_page'],$offset);
              //$wynik = $this->db->query("Select * from books where SUB_CATEGORY_SUBC_ID = '".$id."'");
              //return $wynik;
        }

The arguments in url is changing, but dispalyin only a items from first subcategory.
Help, please.
#2

[eluser]ELRafael[/eluser]
Try, after the model call, echo this:
Code:
$this->db->last_query();

Ex:
Code:
//old
return   $this->db->get('books',$config['per_page'],$offset);
//new
$return = $this->db->get('books',$config['per_page'],$offset);
echo $this->db->last_query();
return $return;

See the SQL statement and check if LIMIT exists inside.

[]'s
#3

[eluser]penta997[/eluser]
the echo displaying: SELECT * FROM (`books`) LIMIT 7 all the time.
#4

[eluser]ELRafael[/eluser]
[quote author="penta997" date="1295396330"]the echo displaying: SELECT * FROM (`books`) LIMIT 7 all the time.[/quote]

Your code:
Code:
$this->db->where('SUB_CATEGORY_SUBC_ID', $id);
//and
return   $this->db->get('books',$config['per_page'],$offset);

but when you debug the sql statemente
Code:
SELECT * FROM (`books`) LIMIT 7

Where is your WHERE condition (WHERE SUBCATEGORY_SUBC_ID = $id) and $offset var?
Maybe $id and $offset vars are empty or false.
Try to debug them:
Code:
var_dump($this->uri->segment(3));
var_dump($this->uri->segment(4));

These variables are empty, my guess. Are you really sure that your link it's something like that:
http://lukaszbielecki.cba.pl/ksiegarnia/...*/**OFFSET**
**ID** => $this->uri->segment(3)
**OFFSET** => $this->uri->segment(4)
[]'s
#5

[eluser]penta997[/eluser]
I've changed the base_url to:
Code:
$config['base_url'] = 'http://lukaszbielecki.cba.pl/ksiegarnia/CI/index.php/ksiegarnia/get_books_by_subcategory/'.$id;
it was my mistake, becouse in browser i have an address for ex:http://lukaszbielecki.cba.pl/ksiegarnia/CI/index.php/ksiegarnia/get_books_by_subcategory/2/0/7
now when i displayng vardump i have: string(1) "3" bool(false) without $offset var in base url.
#6

[eluser]penta997[/eluser]
now i have weird situation. I've changed code:
Controller:
Code:
function get_books_by_subcategory()
        {
            $widok['center'] = '';
             $widok['left'] = $this->get_category();
             $widok['right'] = $this->load->view('Ksiegarnia/right', '', true);
              $id = $this->uri->segment(3);
              $offset = $this->uri->segment(4);
            if(isset($id) and is_numeric($id))
            {
                
                 $query = $this->Kategorie_model->get_books_by_subcategory($id,$offset);
                   $config['base_url'] = 'http://lukaszbielecki.cba.pl/ksiegarnia/CI/index.php/ksiegarnia/get_books_by_subcategory/'.$id;
                      $config['per_page'] = 7;


                      $this->db->where('SUB_CATEGORY_SUBC_ID', $id);
                      $config['total_rows'] =  $this->db->get('books')->num_rows();
                      $config['num_links'] = 20;

                       $this->pagination->initialize($config);
                       //$query =  $this->db->get('books',  $config['per_page'], $offset );

                 if($query->num_rows() > 0)
                 {
                      
                    foreach($query->result() as $item)
                    {

                      
                        $widok['center'] .=  $this->load->view('Ksiegarnia/get_books', array('data' =>$item), true);
                        
                    }
                }
                else
                {
                    $widok['center'] = $this->load->view('Ksiegarnia/get_books', array('tytul' =>'<h1>brak danych</h1>'), true);
                }

                $widok['center'] .= $this->pagination->create_links();
                 $this->load->view('Ksiegarnia/index', $widok);

            }

          

        }


model:
Code:
function get_books_by_subcategory($id, $offset=7)
        {

                     $this->db->where('SUB_CATEGORY_SUBC_ID', $id);
                     $this->db->orderby("SUB_CATEGORY_SUBC_ID", "desc");
             //return   $this->db->get('books',$config['per_page'],$offset);
              //$wynik = $this->db->query("Select * from books where SUB_CATEGORY_SUBC_ID = '".$id."'");
              //return $wynik;
                       $return = $this->db->get('books',7,$offset);
                        
                       echo  var_dump($this->uri->segment(3));
                       echo  var_dump($this->uri->segment(4));
                        return $return;
        }

now pagination almost working for first 6 subcategorys(links form pagination not changen on using link or visited), when i click 7th subcategory the links from pgination starting from 2nd link and all the time 2nd link displaying as using link.
Maybe if I paste website link it will be easier?
#7

[eluser]ELRafael[/eluser]
Hi. Sorry about the delay

Back to basics
Controller (main.php)
Code:
function books( $category, $start = 0 )
{
  //You can use $start or $this->uri->segment(3)
  //I prefer to put as a param of method
  //Tomorrow I can debug a little faster (IMO)
  $this->load->model('book_model', 'Book');
  //Go Pagination, go!
  $this->load->library('pagination');
  $config['base_url'] = base_url().'main/books/'.$category;
  //Only if you'r using PHP 5
  $config['total_rows'] = $this->Book->get_by_category($category)->num_rows();
  $config['per_page'] = 7;
  $config['num_links'] = 20; //UHULL
  $this->pagination->initialize($config);
  $data['links'] = $this->pagination->create_links();

  $data['books'] = $this->Book->get_by_category($category, $start, $config['per_page']);
  $this->load->view('books_view', $data);
}

Model (book_model.php)
Code:
function get_by_category( $category, $start = FALSE, $end = FALSE )
{
  $this->db->select('your_fields');
  $this->db->where('category', $category);
  if ( $start !== FALSE AND $end !== FALSE )
    $this->db->limit($start, $end);

  return $this->db->get('books');
}

Anyway, you can put the url to see if there is a problem. I doubt we can find the issue with url ^_^

About the links from pagination didn't work, try to config $config['uri_segment'] = 3;

[]'s
#8

[eluser]penta997[/eluser]
I've added $config['uri_segment']=4 and pagigation working Big Grin. Thank you very much. But I still dont undrestand why this paranormal things have a place :/
#9

[eluser]ELRafael[/eluser]
If you don't set uri_segment, the default will be always 3.
Your case must be 4.

Nothing from Mars :coolsmile:




Theme © iAndrew 2016 - Forum software by © MyBB