0
votes

Hello i am stuck on codeigniter pagination.I am getting total rows 6, per page showing 2 row.

I am getting pagination link {Page: 1,2,3} where 1 is not clickable.

On Clicking Page no:2 my link look like localhost/demo/index.php/home/press/?per_page=0/2 but i am getting error on sql

You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '/2,2' at line 1

SELECT * FROM media where media_type = 'PRESS' AND id <> 0 ORDER BY id DESC LIMIT 0/2

Same Error,pagintaion link getting when i click on link : 3 I dont know how to fix this issue.I am sharing my code which i used on my controller,model and view.

My controller code:

function press() 
{
    $this->load->model('Home_model');
    $page= $this->input->get('per_page') ? $this->input->get('per_page') : 0;
    $this->load->library('Pagination');
    $data['page'] = $this->input->get('page');
    if($data['page'] == '') {
        $data['page'] = $config['per_page'] = '2'; // Per Page
    } else {
        $data['page'] = $config['per_page'] = $this->input->get('page');
    }
    $config['first_url']='0';
    $pageno = $this->input->get('per_page');
    if($pageno == ''){
        $pageno = '0';
    } 
    $url_to_paging = $this->config->item('base_url');
    $config['base_url'] = $url_to_paging.'home/press/?per_page='.$page;
    $return = $this->Home_model->pressdata($config['per_page'],$pageno, $data);

    $data['pressdata'] = $return['result']; // Get Two Row Data
    $config['total_rows'] = $return['count']; // Total Count 6

    $this->pagination->initialize($config);
    $this->load->view('press',$data);
}

Model :

function pressdata($pg_num, $offset, $content) 
{
    if($offset == ''){
        $offset = '0';
    }
    $sql = "SELECT * FROM  media where media_type = 'PRESS' AND id <> 0";
    if($pg_num!=0 || $pg_num!="")
    {
        $sql .= " ORDER BY id DESC LIMIT ".$offset.",".$pg_num;
    } 
    $query = $this->db->query($sql);
    $sql_couint = "SELECT * FROM  media where media_type = 'PRESS' AND id <> 0";
    $query1 = $this->db->query($sql_couint);
    $ret['result'] = $query->result();
    $ret['count']  = $query1->num_rows();
    return $ret;
} 
3
I have wrote tutorial about Cdeigniter Pagination. Please have a look and give your suggestions cloudways.com/blog/pagination-in-codeigniter - Owais Alam

3 Answers

0
votes

Try it like this

$this->load->library('pagination');
$this->load->model('Home_model');

$config['base_url'] = base_url().'home/press/;
$config['total_rows'] = 200;   // Total no of rows returned from the database table
$config['uri_segment'] = 3;    // It is the page no,used as offset in model
$offset = 0;

if ($this->uri->segment($config['uri_segment']) != "") {
    $offset = $this->uri->segment($config['uri_segment']);
}

$config['per_page'] = 20;      // you can change it as you want
$return = $this->Home_model->pressdata($config['per_page'],$offset, $data); 
$this->pagination->initialize($config);

In your model you can do something like this

function pressdata($per_page, $offset) {
    if($per_page !== '' && $offset !== '') {
        $this->db->limit($per_page, $offset);
    }
    //rest of your query
}  

Don't copy paste it though, I just wanted to give you an idea. Modify it, implement it according to your needs. Let me know if it worked. Go through CodeIgniter's pagination library for more detailed documentation.

0
votes

Finally i found the way to solve this problem. Here are the final code for Controller, Model, View.

Controller Code :

  function press()
   {
    $this->load->library('pagination');
    $url_to_paging = $this->config->item('base_url'); // URL here (www.example.com)
    $config['base_url'] = $url_to_paging.'home/press/'; // www.example.com/home/press
    $config['per_page'] = '4'; // Per Page Data Show 4
    $data = array();
    $return = $this->Home_model->pressdata($config['per_page'],$this->uri->segment(3));
    $data['pressdata'] = $return['result'];
    $config['total_rows'] = $return['count'];
    $this->pagination->initialize($config);
    $this->load->view('press', $data);
    }

Model Code :

function pressdata($num, $offset) 
{
    if($offset == '')
    {
        $offset = '0';
    }
    $sql = "SELECT * FROM media where media_type = 'PRESS' AND id <> 0 ";
    if($num!='' || $offset!='')
    {
        $sql .= " order by id desc limit ".$offset.",".$num."";
    }
    $query = $this->db->query($sql);
    $sql_count = "SELECT * FROM media  WHERE media_type = 'PRESS' AND id <> 0";
    $query1 = $this->db->query($sql_count);
    $ret['result'] = $query->result_array();
    $ret['count']  = $query1->num_rows();
    return $ret;
}

View Page for pagination link :

    if($this->pagination->create_links()) {  ?>
       <div style="clear:both; border-top: solid 1px #e9ecf1;"></div>
         <div style="float:right;">
           <span class="pagination" style="margin:0px; border:none;">
                <?php echo $this->pagination->create_links(); ?>
           </span>
          </div>
    <?php } 
0
votes
function pressdata($pg_num, $offset, $content) 
{
    if($offset == ''){
        $offset = '0';
    }
    $sql = "SELECT * FROM  media where media_type = 'PRESS' AND id => 0";
    if($pg_num!=0 || $pg_num!="")
    {
        $sql .= " ORDER BY id DESC LIMIT ".$offset.",".$pg_num;
    } 
    $query = $this->db->query($sql);
    $sql_couint = "SELECT * FROM  media where media_type = 'PRESS' AND id => 0";
    $query1 = $this->db->query($sql_couint);
    $ret['result'] = $query->result();
    $ret['count']  = $query1->num_rows();
    return $ret;
} 

<-------id should be greater and equal to zero----->