Welcome Guest, Not a member yet? Register   Sign In
pagination problems
#1

[eluser]immi[/eluser]
Hi all,

I am new to codeigniter and new to all frameworks. I have few questions if someone can kindly reply me.

1. $config['num_links'] = '2'; shows only 2 page links? i mean if the database has 600 records and when user click it page 2 there is no page link ahead generated by pagination?


2. I am having problems with my test pagination script. when i click at page 2 it shows the records from 1 to 10 instead of 5 to 10.

Controller:

Code:
if ($this->uri->segment(3) === FALSE)
{
        $data['url_cat'] = "Hindi Ringtones";
}
else
{
        $data['url_cat'] = str_replace("-", " ", $this->uri->segment(3));
}

        
        $this->load->library('pagination');
        
        $config['base_url'] = base_url() . "ringtones/download/" . str_replace(" ", "-",$data['url_cat']) . "/";
        $config['total_rows'] = $this->db->query("SELECT * from ". RINGTONES_TABLE . " WHERE cat='". $data['url_cat'] ."'")->num_rows();
        $config['per_page'] = '5';
        $config['num_links'] = '2';
        $this->pagination->initialize($config);
        
        $offset = $this->uri->segment(4); //this will work like site/folder/controller/function/query_string_for_cat/query_string_offset
        $limit = $config['per_page'];
        //uri lib get loaded automatically
        //$data["records"] = $this->db->query("SELECT * from ". RINGTONES_TABLE . " WHERE cat='". $data['url_cat'] ."' LIMIT $limit, $offset");
    
        $data['query'] = $this->db->query("SELECT * from ". RINGTONES_TABLE . " WHERE cat='". $data['url_cat'] ."' ORDER BY id DESC LIMIT $limit,$offset");
$this->load->view('ringtones_view', $data);

View page:
Code:
<table cellspacing="1" cellspacing="1">
<tr>
<th>id</th><th style="width:80%">title</th><th>filetype</th></tr>
&lt;?php foreach($query->result() as $row):?&gt;
<tr><td>&lt;?php echo $row->id?&gt;</td><td>&lt;?php echo substr($row->name,0,40)?&gt;</td><td>&lt;?php echo $row->filetype?&gt;</td></tr>
&lt;?php endforeach;?&gt;
</table>
            
            <div id="pagination">
            &lt;?php echo $this->pagination->create_links();?&gt;
            </div>
#2

[eluser]cideveloper[/eluser]
lets see if this helps

Code:
$data['query'] = $this->db->query("SELECT * from ". RINGTONES_TABLE . " WHERE cat='". $data['url_cat'] ."' ORDER BY id DESC LIMIT $limit,$offset")

change to

Code:
$data['query'] = $this->db->query("SELECT * from ". RINGTONES_TABLE . " WHERE cat='". $data['url_cat'] ."' ORDER BY id DESC LIMIT $offset,$limit")


Below from MYSQL.com

Quote:With two arguments, the first argument specifies the offset of the first row to return, and the second specifies the maximum number of rows to return. The offset of the initial row is 0 (not 1):

SELECT * FROM tbl LIMIT 5,10; # Retrieve rows 6-15
#3

[eluser]immi[/eluser]
Hi

Thanks for your reply! I have changed to
Code:
$data['query'] = $this->db->query("SELECT * from ". RINGTONES_TABLE . " WHERE cat='". $data['url_cat'] ."' ORDER BY id DESC LIMIT $offset,$limit")
This works fine as far as the record showing.

But the pagination is still not fine. its shows me only 3 page links which i have set in
Code:
$config['num_links'] = '2'
. When i click to page 3 i dont find any more link to go ahead (i mean page number 4) doesnt appear Though i have records 600 and page links should be minimum 20.


Any Idea Please?
#4

[eluser]cideveloper[/eluser]
Let me get this straight

Below I will give an example of what I think your urls and pagination links should look like. Tell me If this is not what it looks like

First Page
--------------
URL: http://localhost/ringtones/download/Hindi-Ringtones
Pagination links: 1 2 3 Next Last

When you click on 2
-----------------------
URL: http://localhost/ringtones/download/Hindi-Ringtones/5
Pagination links: Previous 1 2 3 4 Next Last

When you click on 3
-----------------------
URL: http://localhost/ringtones/download/Hindi-Ringtones/10
Pagination links: Previous 1 2 3 4 5 Next Last

When you click on 4
-----------------------
URL: http://localhost/ringtones/download/Hindi-Ringtones/15
Pagination links: First Previous 2 3 4 5 6 Next Last


See if this code works

Controller
---------------
Code:
$this->load->library('pagination');
$limit = 5;
$offset = $this->uri->segment(4,0);
$url_cat = $this->uri->segment(3,'Hindi-Ringtones');
$data['url_cat'] = str_replace("-", " ", $url_cat);

$config['base_url'] = base_url() . "ringtones/download/" . $url_cat . "/";
$config['total_rows'] = $this->db->where('cat', $data['url_cat'])->get(RINGTONES_TABLE)->num_rows();
$config['per_page'] = $limit;
$config['num_links'] = 2;
$this->pagination->initialize($config);
$data['pagination_links'] => $this->pagination->create_links();
$data['query'] = $this->db->where('cat', $data['url_cat'])->order_by('id', 'desc')->get(RINGTONES_TABLE, $limit, $offset);
$this->load->view('ringtones_view', $data);

View
---------------
Code:
<table cellspacing="1" cellspacing="1">
<tr>
<th>id</th><th style="width:80%">title</th><th>filetype</th></tr>
&lt;?php foreach($query->result() as $row):?&gt;
<tr><td>&lt;?php echo $row->id?&gt;</td><td>&lt;?php echo substr($row->name,0,40)?&gt;</td><td>&lt;?php echo $row->filetype?&gt;</td></tr>
&lt;?php endforeach;?&gt;
</table>

<div id="pagination">
&lt;?php echo $pagination_links;?&gt;
</div>




Theme © iAndrew 2016 - Forum software by © MyBB