Welcome Guest, Not a member yet? Register   Sign In
Pagination problem simple I think....
#1

[eluser]Jamongkad[/eluser]
Hi guys I'm thinking this is a simple problem that could be linked but right now I'm pretty stumped. Anyways I have this search function...I use pagination to display the results. Thankfully I can control the amount of information displayed. The problem is the over run cannot be view after you click on the links to see more. Lets say for example there are 10 items found. I had the pagination lib limit it to 4, so there are links for extra information right? when I click on them the DB returns no results. When I go back to the first link no information too. It's like there was no information in the first place. Anyways here's the code hope we could solve this one...

Controller
Code:
function search()
  {
   $this->load->library('pagination');
   $config['base_url'] = base_url().'sprocket/search';
   $config['total_rows'] = $this->db->count_all('sprocket_upload');
   $config['per_page'] = '4';
   $config['full_tag_open'] = '<p>';
   $config['full_tag_close'] = '</p>';
   $config['cur_tag_open'] = '<em>';
   $config['cur_tag_close'] = '</em>';
  
   $num = $config['per_page'];
   $offset = $this->uri->segment(3);
  
   $this->pagination->initialize($config);
   $datas['page_links'] = $this->pagination->create_links();
  
   $search_query = $this->input->post('search_box');
   $datas['result'] = $this->sprocketeer->fetchSearchResults($search_query,$num,$offset);
   $data['title'] = "Sprocket Fish: Search Results";
   $data['header'] = $this->load->view('snippets/header.php','',TRUE);
   $data['content'] = $this->load->view('app/sprocket/search_results.php',$datas,TRUE);
   $data['footer'] = $this->load->view('snippets/footer.php','',TRUE);
   $this->load->view('layout.php',$data);
  }

Model
Code:
function fetchSearchResults($search_query, $num, $offset)
  {
   $results = $this->db->query($sql);*/
   if($search_query != FALSE)
   {
   $this->db->like('tags',$search_query);
   $this->db->orderby('tags');
   $results = $this->db->get('sprocket_upload', $num, $offset);
   return $results->result_array();
   }

  }

view
Code:
&lt;!--Search Results--&gt;
       &lt;?php if(!empty($result)){ ?&gt;
       <table>
        <tr>
           &lt;?php foreach($result as $rows): ?&gt;
            
            <td class="displayResults"><p>
            Tags: &lt;?php
                    $pack = unserialize($rows['tags']);
                    foreach($pack as $key=>$value){
                    echo $value.", ";
                    }
                     ?&gt;<br/>
            Sprocket Name:&lt;?php echo $rows['sprocket_name']; ?&gt;
            </p>
            <p><span class="imgDetails">&lt;?php echo anchor('sprocket/viewSprocket/'.$rows['id'],'Image Details '); ?&gt;</span><span class="pricingInfo">&lt;?php echo anchor('sprocket/viewSprocket/'.$rows['id'],'Pricing Info'); ?&gt;</span></p>
            <img src="&lt;?php echo base_url(); ?&gt;sprocket_files/&lt;?php echo $rows['file_name']; ?&gt;" />
            </td>
            
           &lt;?php endforeach; ?&gt;
        </tr>
       </table>
       &lt;?php } else { ?&gt;
        <div class="searchBox"><p>No results returned.</p></div>
       &lt;?php } ?&gt;
       &lt;!--EOF Search Results--&gt;    
       &lt;?php echo $page_links; ?&gt;
#2

[eluser]alpar[/eluser]
sorry i didn't had the time to go over all of you code in detail, but i did notice that your count of the results is incorrect. You count all rows from the table, the search will most probably won't return all of them, so you have to do 2 queries, one for the result with limit, and one to count all the results. Also the offset is a page number of the pagination class. To be used with the databases LIMIT you have to calculate it, something like (offset-1)*per_page also you have to check if ofset is 0 make it 1, because the offset for page one is 0 (actually it isn't set so false is returned which evaluates to 0).
#3

[eluser]Jamongkad[/eluser]
[quote author="alpar" date="1190999003"]sorry i didn't had the time to go over all of you code in detail, but i did notice that your count of the results is incorrect. You count all rows from the table, the search will most probably won't return all of them, so you have to do 2 queries, one for the result with limit, and one to count all the results. Also the offset is a page number of the pagination class. To be used with the databases LIMIT you have to calculate it, something like (offset-1)*per_page also you have to check if ofset is 0 make it 1, because the offset for page one is 0 (actually it isn't set so false is returned which evaluates to 0).[/quote]

Ok thanks I'll to do that. My first time to use the Pagination class and it seems kinda quirky to use at first.
#4

[eluser]philm[/eluser]
Here's a 'chunk' from me incase it helps any... ;-)
Code:
// build pagination links
$this->load->library('pagination');
$config['base_url'] = site_url("category/viewCategories");
$config['total_rows'] = $this->db->count_all('categories');
$config['per_page'] = 10;
$this->pagination->initialize($config);
$data['pagination'] = $this->pagination->create_links();

// get paginated results
$num = $config['per_page'];
$offset = $this->uri->segment(3);
$this->pagination->initialize($config);
$data['rows'] = $this->Categories->getPaginated($num, $offset);
#5

[eluser]Jamongkad[/eluser]
[quote author="philm" date="1191010805"]Here's a 'chunk' from me incase it helps any... ;-)
Code:
// build pagination links
$this->load->library('pagination');
$config['base_url'] = site_url("category/viewCategories");
$config['total_rows'] = $this->db->count_all('categories');
$config['per_page'] = 10;
$this->pagination->initialize($config);
$data['pagination'] = $this->pagination->create_links();

// get paginated results
$num = $config['per_page'];
$offset = $this->uri->segment(3);
$this->pagination->initialize($config);
$data['rows'] = $this->Categories->getPaginated($num, $offset);
[/quote]

Nice! could I see your model if you don't mind? cuz my model accepts a search query as a part of the search requirement.
#6

[eluser]philm[/eluser]
Sure Smile
Code:
...
function getPaginated($num, $offset){
   $this->db->orderby("category", "asc");
   $query = $this->db->get('categories', $num, $offset);
   return $query;
}
...




Theme © iAndrew 2016 - Forum software by © MyBB