Part of the EllisLab Network
   
1 of 2
1
Code Igniter - Pagination How?
Posted: 04 April 2007 07:38 AM   [ Ignore ]  
Summer Student
Avatar
Total Posts:  21
Joined  04-02-2007

Hello All,

Please advise me how to build an pagination on CI. I have tried a few ideas which I have but at whole the script become too heavy.
What I try to build is an page where I can show results in table view from one table. This page initial contains search by different option, fast letter (when click filter results only starting with that letter), and of course pagination (as the result set can be about 15000 rows). I have already build that page using some MVC at very early stage, but now want to migrate to CI.

Any idea how easily pagination could be implement in CI?

Thanks in advance.

Profile
 
 
Posted: 04 April 2007 09:13 AM   [ Ignore ]   [ # 1 ]  
Lab Assistant
RankRank
Total Posts:  104
Joined  02-15-2007

Not sure exactly if this is what you aming for but i assume you want a way to auto generate the pages based on the rows u get back from your search function.

In the pagination class there are two variable called ‘total_rows’ and ‘per_page’. As the name suggests total_rows are total records you get back and ‘per_page’ is the number of entrie on each page u want to display.

So, you will set the variable as such
$numEntriesPerPage = 10;
$config[‘total_rows’] = $this->db->getNumSearchResults(); //return the number of rows
$config[‘per_page’] = $numEntriesPerPage;

In your db function which retrieves the search results, you will have two pramaters along with your search keyword, one is the limit and the other offset and you will pass arguments to the function as such

$this->db->getSearchResults($keyword, $numEntriesPerPage, $this->uri->segment(3));

$this->uri-segment(3) will fetch the number where to start looking fom the next results in the db. For example, when the user click on the 3nd page, the arguments to the function will look this $this->db->getSearchResults($keyword, 10, 20) and your uri will look this http://yourdomain.com/search/index/20

hope that helps

 Signature 

image upload host web site
Learn PHP

Profile
 
 
Posted: 04 April 2007 01:07 PM   [ Ignore ]   [ # 2 ]  
Summer Student
Avatar
Total Posts:  21
Joined  04-02-2007

Hello Striker,

I did not understand how to send more than one value to the search page. Let me explain what I thought:

I need to send the following to the search page:
- search type (name, description , etc…) coded as 1,2,3,etc.
- search value
- page
- filter type (by supplier, by price) coded as 1,2,3,etc
- filter value (either alpha or numeric values)
so using query string we normally have link like http://example.com/search.php?stype=0&search=BLUE+SHOES&page=1&ftype=5&filter=1
So translated this query means:
Find all products which match $search in their $stype field AND show only these products which belongs to supplier identify by $filter and show products starting $page*10

how can such a large query can be send? I mean something like
example.com/search/index/0/BLUE+SHOES/1/1/1 as we don’t have key=value pair

Profile
 
 
Posted: 04 April 2007 03:26 PM   [ Ignore ]   [ # 3 ]  
Research Assistant
Avatar
RankRankRank
Total Posts:  366
Joined  04-27-2006
Borislav Nedelchev - 04 April 2007 01:07 PM

Hello Striker,
....as we don’t have key=value pair

that´s not really correct, this is what you need: user guide - uri library

$this->uri->uri_to_assoc(n)

This function lets you turn URI segments into and associative array of key/value pairs. Consider this URI:
index.php/user/search/name/joe/location/UK/gender/male

Using this
function you can turn the URI into an associative array with this prototype:
[array]
(
    
'name' => 'joe'
    'location'
=> 'UK'
    'gender'
=> 'male'
)

$this->uri->assoc_to_uri()

Takes an associative array as input and generates a URI string from it. The array keys will be included in the string. Example:
$array = array('product' => 'shoes', 'size' => 'large', 'color' => 'red');

$str = $this->uri->assoc_to_str($array);

// Produces: product/shoes/size/large/color/red

In your case you could end with an url like this:
url controller/function/search/BLUE+SHOES/page/1/ftype/5/filter/1

first I thought you want just a pagination tutorial…
if so, here is a good one: pagination tutorial - with full working example as zip file

Profile
 
 
Posted: 05 April 2007 02:05 AM   [ Ignore ]   [ # 4 ]  
Summer Student
Avatar
Total Posts:  21
Joined  04-02-2007

Hello gunter,

thanks for a good explanation and great example. Please excuse my ignorance as I am newbie in the CI. I will test all in practice.

Regards
B

Profile
 
 
Posted: 05 April 2007 03:08 AM   [ Ignore ]   [ # 5 ]  
Research Assistant
Avatar
RankRankRank
Total Posts:  631
Joined  12-26-2006
Borislav Nedelchev - 05 April 2007 02:05 AM

Hello gunter,

thanks for a good explanation and great example. Please excuse my ignorance as I am newbie in the CI. I will test all in practice.

Regards
B

 
Hi Boris,
 
I prefer the simple Google approach to searching which is not to restrict the user by having to select various options. If there are too many choices then increase the search terms to “drill down”.
 
Check out my search at the bottom of the web page. If you like it I will supply the code.
 
Cheers,
 
John_Betong
 

 Signature 

Joke of the day     (ongoing development site)

My Hippy Trail    Source code   

Latest Project

Profile
 
 
Posted: 24 September 2007 11:41 AM   [ Ignore ]   [ # 6 ]  
Summer Student
Total Posts:  14
Joined  05-10-2007

I have a solution to this issue of counting the results of large queries:
http://codeigniter.com/forums/viewthread/61454/

Profile
 
 
Posted: 24 September 2007 12:49 PM   [ Ignore ]   [ # 7 ]  
Summer Student
Avatar
Total Posts:  30
Joined  08-15-2007

Hi
I think Scaffolding->view class is a very good example of pagination

Profile
 
 
Posted: 25 September 2007 03:50 AM   [ Ignore ]   [ # 8 ]  
Summer Student
Total Posts:  14
Joined  05-10-2007

cheers thanks… i’ve not used the scaffolding much

Profile
 
 
Posted: 28 October 2008 07:27 PM   [ Ignore ]   [ # 9 ]  
Summer Student
Total Posts:  6
Joined  10-22-2008

GREETINGS!

im having a problem with my pagination…
i have a table displayed in my under-development website in this format

Company Name   Acronym   Date of Reg   Status      
J-CLyps Company   JC   2008-10-27   pending   edit   delete
Test Company   TC   2008-10-28   pending   edit   delete
ABC Company   ABCC   2008-10-28   pending   edit   delete
Sam’s Pooch Co   SPP   2008-10-28   pending   edit   delete
Sam’s Pooch Co   SPP   2008-10-28   pending   edit   delete
1 2 >

but what happens is that when i click either “2” or “>”,
it proceeds to the next page but observe that the pagination links didn’t change

Company Name   Acronym   Date of Reg   Status      
Kill Joy   KJ   2008-10-29   pending   edit   delete
1 2 >

it appears like the displayed item is in the first page which was supposed to be in page 2, but when i click on the “back” button in the browser, it displays the previous state.

how can i correct this problem? :(


here is my code for my controller:

class Supplier extends Controller{

    
function Supplier(){
        parent
::Controller();    
    
}
    
    
function index($orderby, $segment){
        $data[
'title'] = "Suppliers ||";
        
$data['main'] = 'supplier/view';
        
        
$this->load->library('pagination');
        
$config['base_url'] = base_url().'index.php/supplier/index/'.$orderby;
        
$config['total_rows'] = $this->db->count_all('suppliers');
        
$config['per_page'] = '5';
           
$config['full_tag_open'] = '<p>';
        
$config['full_tag_close'] = '</p>';

        
$this->pagination->initialize($config);
        
        
//load the model and get results
        
$data['results'] = $this->supp_model->getSupp($config['per_page'],$this->uri->segment(4), $orderby);

        
// load the view
        
$this->load->vars($data);
        
$this->load->view('template');
    
}
...

here goes my view code:

<table>
<
tr>
    <
td><?php echo anchor('supplier/index/companyname/0/', 'Company Name', array('class'=>'us'))?></td>
    <
td><?php echo anchor('supplier/index/acronym/0/', 'Acronym', array('class'=>'us'))?></td>
    <
td><?php echo anchor('supplier/index/registerdate/0/', 'Date of Registration', array('class'=>'us'))?></td>
    <
td><?php echo anchor('supplier/index/status/0/', 'Status', array('class'=>'us'))?></td>
    <
td>&nbsp;</td>
    <
td>&nbsp;</td>
</
tr>
<?php foreach($results as $row):?>
<tr>
<
td><?php echo $row->companyname?></td>
<
td><?php echo $row->acronym?></td>
<
td><?php echo $row->registerdate?></td>
<
td><?php echo $row->status?></td>
<
td><?php echo anchor('supplier/edit', 'edit', array('class'=>'us'))?></td>
<
td><?php echo anchor('supplier/delete', 'delete', array('class'=>'us'))?></td>
</
tr>
<?php endforeach;?>
</table>

<?php echo $this->pagination->create_links(); ?>

i need any kind of help :D
thanks!!!

by the way, this would be my first month as a “CodeIgniter-er” and a web developer… just so you would know.

Profile
 
 
Posted: 14 December 2008 01:05 PM   [ Ignore ]   [ # 10 ]  
Summer Student
Total Posts:  1
Joined  12-14-2008
SamCris - 28 October 2008 07:27 PM

GREETINGS!

im having a problem with my pagination…
i have a table displayed in my under-development website in this format

Company Name   Acronym   Date of Reg   Status      
J-CLyps Company   JC   2008-10-27   pending   edit   delete
Test Company   TC   2008-10-28   pending   edit   delete
ABC Company   ABCC   2008-10-28   pending   edit   delete
Sam’s Pooch Co   SPP   2008-10-28   pending   edit   delete
Sam’s Pooch Co   SPP   2008-10-28   pending   edit   delete
1 2 >

but what happens is that when i click either “2” or “>”,
it proceeds to the next page but observe that the pagination links didn’t change

Company Name   Acronym   Date of Reg   Status      
Kill Joy   KJ   2008-10-29   pending   edit   delete
1 2 >

it appears like the displayed item is in the first page which was supposed to be in page 2, but when i click on the “back” button in the browser, it displays the previous state.

how can i correct this problem? :(


here is my code for my controller:

class Supplier extends Controller{

    
function Supplier(){
        parent
::Controller();    
    
}
    
    
function index($orderby, $segment){
        $data[
'title'] = "Suppliers ||";
        
$data['main'] = 'supplier/view';
        
        
$this->load->library('pagination');
        
$config['base_url'] = base_url().'index.php/supplier/index/'.$orderby;
        
$config['total_rows'] = $this->db->count_all('suppliers');
        
$config['per_page'] = '5';
           
$config['full_tag_open'] = '<p>';
        
$config['full_tag_close'] = '</p>';

        
$this->pagination->initialize($config);
        
        
//load the model and get results
        
$data['results'] = $this->supp_model->getSupp($config['per_page'],$this->uri->segment(4), $orderby);

        
// load the view
        
$this->load->vars($data);
        
$this->load->view('template');
    
}
...

here goes my view code:

<table>
<
tr>
    <
td><?php echo anchor('supplier/index/companyname/0/', 'Company Name', array('class'=>'us'))?></td>
    <
td><?php echo anchor('supplier/index/acronym/0/', 'Acronym', array('class'=>'us'))?></td>
    <
td><?php echo anchor('supplier/index/registerdate/0/', 'Date of Registration', array('class'=>'us'))?></td>
    <
td><?php echo anchor('supplier/index/status/0/', 'Status', array('class'=>'us'))?></td>
    <
td>&nbsp;</td>
    <
td>&nbsp;</td>
</
tr>
<?php foreach($results as $row):?>
<tr>
<
td><?php echo $row->companyname?></td>
<
td><?php echo $row->acronym?></td>
<
td><?php echo $row->registerdate?></td>
<
td><?php echo $row->status?></td>
<
td><?php echo anchor('supplier/edit', 'edit', array('class'=>'us'))?></td>
<
td><?php echo anchor('supplier/delete', 'delete', array('class'=>'us'))?></td>
</
tr>
<?php endforeach;?>
</table>

<?php echo $this->pagination->create_links(); ?>

i need any kind of help :D
thanks!!!

by the way, this would be my first month as a “CodeIgniter-er” and a web developer… just so you would know.


Controller

$config['uri_segment'] = 4;
Profile
 
 
Posted: 10 January 2009 07:17 PM   [ Ignore ]   [ # 11 ]  
Summer Student
Total Posts:  13
Joined  01-09-2009

Miguel Carmona, thank you for your answer. i had the same problem too.

Profile
 
 
Posted: 06 February 2009 03:15 PM   [ Ignore ]   [ # 12 ]  
Summer Student
Total Posts:  1
Joined  02-06-2009

Hi

In your controller change:

$config['per_page'] = '5';


in

$config['per_page']= 5;
Profile
 
 
Posted: 01 July 2009 10:15 AM   [ Ignore ]   [ # 13 ]  
Summer Student
Total Posts:  3
Joined  07-01-2009

Thanks Miguel Carmona, same issue here too and your solution fixed it.

Thanks again!

Profile
 
 
Posted: 03 October 2009 07:09 AM   [ Ignore ]   [ # 14 ]  
Summer Student
Avatar
Total Posts:  19
Joined  08-26-2009

Thanks for the solution Miguel!

 Signature 

keep calm and good for others

Profile
 
 
Posted: 22 December 2009 08:19 AM   [ Ignore ]   [ # 15 ]  
Summer Student
Total Posts:  1
Joined  12-28-2008

Tnx same problem solve! wink

Profile
 
 
   
1 of 2
1
 
Post Marker Legend
New Topic New posts Hot Topic Hot Topic with new posts New Poll New Poll Moved Topic Moved Topic Sticky Topic Sticky topic
Old Topic No new posts Hot Old Topic Hot Topic with no new posts Old Poll Old Poll Closed Topic Closed Topic Announcement Announcements
Theme
Change Theme
Visitor Statistics
The most visitors ever was 819, on March 11, 2010 11:15 AM
Total Registered Members: 119889 Total Logged-in Users: 56
Total Topics: 126031 Total Anonymous Users: 4
Total Replies: 663001 Total Guests: 475
Total Posts: 789032    
Members ( View Memberlist )