CodeIgniter Forums
Can you check my pagination?[SOLVED] - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Using CodeIgniter (https://forum.codeigniter.com/forum-5.html)
+--- Forum: General Help (https://forum.codeigniter.com/forum-24.html)
+--- Thread: Can you check my pagination?[SOLVED] (/thread-67307.html)

Pages: 1 2


Can you check my pagination?[SOLVED] - kirasiris - 02-08-2017

Hello here is my issue, mi pagination has the url working fine, and I can see the pagination class insisde m view, but I still do not know how to retrieve just the number of entries that I want:

Here is my controller(News, function blog):

PHP Code:
       public function blog() {
         
       $data['blog'] = $this->blog_model->get_blog();
                
$data['categorias'] = $this->categorias_model->get_categorias();
                
$data['title'] = 'Blog';
        
        
                
$config = array();
                
$config["base_url"] = base_url() . "blog";
                
$config["total_rows"] = $this->blog_model->record_count();
                
$config["per_page"] = 1;
                
$config["uri_segment"] = 3;
                
$config['num_links'] = 1;
                
$config['query_string_segment'] = 'paginas';
                
$config['page_query_string'] = TRUE;
                
$config['display_pages'] = FALSE;
                
            
                
$this->pagination->initialize($config);
            
                
$page = ($this->uri->segment(3)) ? $this->uri->segment(3) : 0;
                
$data["pagination"] = $this->pagination->create_links();
                
$data["results"] = $this->blog_model->get_paginas($config["per_page"], $page);
                
                
$this->load->view('templates/head',$data);
                
$this->load->view('templates/navbar',$data);
                
$this->load->view("news/blog/index"$data);
                
$this->load->view('templates/footer',$data);


Here is my model(Blog_model):

PHP Code:
    class Blog_model extends CI_Model{
        
// Connect to database //
        
public function __construct(){
            
$this->load->database();
            }
            
        
// Get Posts from database //
        
public function get_blog($slug=FALSE){
            if(
$slug===FALSE){
                
// Post order, ASC-DESC Categorias/Tags tabels //
                
$this->db->order_by('blog.id''DESC');
//                $this->db->join('categorias','categorias.id = blog.categoria_id');
                // /Post order, ASC-DESC //
                
$query=$this->db->get('blog');
                return 
$query->result_array();
                }
                
                
$query=$this->db->get_where('blog', array('slug'=>$slug));
                return 
$query->row_array();
            }
            
///Pagination
 
   public function record_count() {
 
   return $this->db->count_all("blog");
}

public function 
get_paginas($limit$start) {
 
   $this->db->limit($limit$start);
 
   $query $this->db->get("blog");

 
   if ($query->num_rows() > 0) {
 
       foreach ($query->result() as $row) {
 
           $data[] = $row;
 
       }
 
       return $data;
 
   }
 
   return false;
}


        
        
// Crear post //
        
public function crear_post(){
            
$slug=url_title($this->input->post('titulo'));
            
            
$data = array(
                
'titulo' => $this->input->post('titulo'),
                
'slug' => $slug,
                
'imagen' => $this->input->post('imagen'),
//                'autor' => $this->input->post('autor'),
                
'fecha' => $this->input->post('fecha'),
                
'contenido' => $this->input->post('contenido'),
                
'categoria_id' => $this->input->post('categoria_id'),

            );
            
            return 
$this->db->insert('blog',$data);
            
            }
            
        
// Borrar post //    
            
public function delete_post($id){
            
$this->db->where('id'$id);
            
$this->db->delete('blog');
            return 
true;
        }
        
// Actualizar Post //
        
public function update_post(){
            
$slug url_title($this->input->post('titulo'));
            
$data = array(
                
'titulo' => $this->input->post('titulo'),
                
'slug' => $slug,
                
'imagen' => $this->input->post('imagen'),
//                'autor' => $this->input->post('autor'),
                
'fecha' => $this->input->post('fecha'),
                
'contenido' => $this->input->post('contenido'),
                
'categoria_id' => $this->input->post('categoria_id'),

            );
            
$this->db->where('id'$this->input->post('id'));
            return 
$this->db->update('blog'$data);
        }

        
// Categorias //
        
//        public function get_categorias(){
//            $this->db->order_by('nombre');
//            $query = $this->db->get('categorias');
//            return $query->result_array();
//        }
            

        
        


Here is my view(views/news/blog/index):
Code:
<nav class="breadcrumb">
    <div class="container">
     <a class="breadcrumb-item" id="a" href="<?php echo base_url(); ?>">Home</a>
     <span class="breadcrumb-item active">Blog</span>
   </div>
</nav>

<div class="container" id="main">
<div class="row">
<!-- Article -->
<div class="col-md-8 animated fadeIn">
<?php foreach($blog as $blog) : ?>
<div class="card text-xs-left"><!-- FIRST ARTICLE DEMO -->
<div class="card-header" id="article-header">
<h4><a href="<?php echo site_url('/blog/'.$blog['slug']); ?>"><?php echo ucfirst($blog['titulo']); ?></a></h4>
</div>
<img src="<?php echo $blog['imagen']; ?>" width="750" heihgt="350" class="img-fluid">
<!--<div class="card-body">
<p> <?php  echo word_limiter($blog['contenido'],5); ?></p>
</div>-->
<div class="card-footer" id="article-footer">
<div class="row">
<div class="col-lg-12 col-md-9 col-sm-8">
<i class="fa fa-calendar" aria-hidden="true"></i> <?php echo ucfirst($blog['fecha']); ?>

<i class="fa fa-folder" aria-hidden="true"></i> <?php echo $blog['categoria_id']; ?>

<a href="<?php echo site_url('/blog/'.$blog['slug']); ?>" class="pull-right">Read more »</a>
</div>
</div>
</div>
</div>
<?php endforeach ?>
<!-- /Article -->
<?php echo $pagination ?>
</div>

<div class="col-md-4 animated fadeIn">
<!-- Search -->
<form class="form-inline-fluid">
<input class="form-control" type="text" placeholder="Search">
<button type="submit" class="btn btn-success" style="border-radius: 0;position: absolute;z-index: 99;top: 0;right: 1rem;"><i class="fa fa-search" aria-hidden="true"></i></button>
</form>
<!-- Categorias -->
<hr>
<div class="list-group" id="">
<a href="" class="list-group-item active" id="dashboard-active"><i class="fa fa-tachometer" aria-hidden="true"></i> Categorias</a>
<?php foreach ($categorias as $blog) : ?>
<a class="list-group-item list-group-item-action" href="#" role="button"><i class="fa fa-folder" aria-hidden="true"></i> <?php echo $blog['nombre']; ?></a>
<?php endforeach ?>
</div>
<hr>Tiempo en cargar <strong>{elapsed_time}</strong><hr>
<!-- Social media tabs -->
<br>
<h5>Social Media</h5>
<ul class="nav nav-tabs" role="tablist" id="tablist">
<li class="nav-item" id="social-tabs">
<a class="nav-link btn btn-danger active" id="social-facebook" data-toggle="tab" href="#facebook" role="tab" aria-expanded="true">Facebook</a>
</li>
<li class="nav-item" id="social-tabs">
<a class="nav-link btn btn-danger" id="social-twitter" data-toggle="tab" href="#twitter" role="tab" aria-expanded="false">Twitter</a>
</li>
</ul>
<!-- Social media content -->
<div class="tab-content" id="tab-content">
<!-- Facebok -->
<div class="tab-pane active" id="facebook" role="tabpanel" aria-expanded="true">
<div class="tags">
<div class="panel">
<div class="tab-pane" id="Facebook">
<div class="fb-page" data-href="https://www.facebook.com/blogpersonal.net" data-tabs="timeline" data-height="500" data-small-header="false" data-adapt-container-width="true" data-hide-cover="false" data-show-facepile="true"></div>
</div>
</div>
</div>
</div>
<!-- /Facebok -->
<!-- Twitter -->
<div class="tab-pane" id="twitter" role="tabpanel" aria-expanded="false"><!-- Twitter -->
<a class="twitter-timeline" href="https://twitter.com/kirasiris" data-chrome="noheader nofooter noborders "  data-tweet-limit="3">Tweets by @Kirasiris</a>
</div>
<!-- /Twitter -->
</div>
<hr>
</div>
</div>
</div>

eveytime that hit the next button, last button, first button, previous button , it shows me alll the entries that I have on database, any suggestion?

Here is my route file:
PHP Code:
<?php
defined
('BASEPATH') OR exit('No direct script access allowed');
//* Admin Pages *//  Can be found in the admin controller
// Edit global pages ////
$route['admin/actualizarpagina_terms'] = 'admin/actualizarpagina_terms';
$route['admin/actualizarpagina_privacy'] = 'admin/actualizarpagina_privacy';
$route['admin/actualizarpagina_cookies'] = 'admin/actualizarpagina_cookies';
$route['admin/actualizarpagina_about'] = 'admin/actualizarpagina_about';
$route['admin/actualizarskills'] = 'admin/actualizarskills';
$route['admin/actualizarcategory'] = 'admin/actualizarcategory';
$route['admin/crearcategory'] = 'admin/crearcategory';
$route['admin/categorias'] = 'admin/categorias';
$route['admin/crearpagina'] = 'admin/crearpagina';
$route['admin/crearpost'] = 'admin/crearpost';
$route['admin/crearskill'] = 'admin/crearskill';
$route['admin/crearportfolio'] = 'admin/crearportfolio';
$route['admin/actualizarpagina'] = 'admin/actualizarpagina';
$route['admin/actualizarpost'] = 'admin/actualizarpost';
$route['admin/actualizarportfolio'] = 'admin/actualizarportfolio';
$route['admin/paginas'] = 'admin/paginas';
$route['admin/posts'] = 'admin/posts';
$route['admin/portfolio'] = 'admin/portfolio';
$route['admin/skills'] = 'admin/skills';
$route['admin'] = 'admin/index';
$route['admin/(:any)'] = 'admin/view/$1';
//* Global pages *// Can be found in the news controller
$route['portfolio/(:any)'] = 'portfolio/view/$1';
$route['portfolio'] = 'news/portfolio/index';
$route['blog'] = 'news/blog/index';
$route['blog/(:any)'] = 'blog/view/$1';
$route['contact'] = 'news/contact';
$route['cookies'] = 'news/cookies';
$route['about'] = 'news/about';
$route['terms'] = 'news/terms';
$route['privacy'] = 'news/privacy';
$route['news'] = 'news';
$route['news/(:any)'] = 'news/view/$1';
//* Default *//
$route['default_controller'] = 'news';
$route['(:any)'] = 'pages/view/$1';
$route['404_override'] = '';
$route['translate_uri_dashes'] = TRUE



RE: Can you check my pagination? - Wouter60 - 02-08-2017

Did you check if the uri segment 3 is really the one that refers to the page number?
You pagination base_url is base_url() . 'blog'. Does this refer to the controller or the method 'blog' inside the controller? If your controller is also named "Blog", the url should be: base_url() . 'blog/blog'. Or base_url('blog/blog').
Or do you have a route that handles the blog/blog url to just blog? In that case, the page segment must be 2 instead of 3.


RE: Can you check my pagination? - kirasiris - 02-09-2017

(02-08-2017, 11:48 PM)Wouter60 Wrote: Did you check if the uri segment 3 is really the one that refers to the page number?
You pagination base_url is base_url() . 'blog'. Does this refer to the controller or the method 'blog' inside the controller? If your controller is also named "Blog", the url should be: base_url() . 'blog/blog'. Or base_url('blog/blog').
Or do you have a route that handles the blog/blog url to just blog? In that case, the page segment must be 2 instead of 3.

My controller is called News, and the function is called blog, and I have in my route file the next routes, should I change something?
PHP Code:
$route['blog'] = 'news/blog/index';
$route['blog/(:any)'] = 'blog/view/$1'



RE: Can you check my pagination? - Wouter60 - 02-09-2017

Try it with:
PHP Code:
$config['uri_segment'] = 2;
$page = ($this->uri->segment(2)) ? $this->uri->segment(2) : 0



RE: Can you check my pagination? - kirasiris - 02-09-2017

(02-09-2017, 05:42 AM)Wouter60 Wrote: Try it with:
PHP Code:
$config['uri_segment'] = 2;
$page = ($this->uri->segment(2)) ? $this->uri->segment(2) : 0

I did as you said, but it is still showing me all the 6 entries that I have in DB in all the pages


RE: Can you check my pagination? - Wouter60 - 02-09-2017

In your controller, you first have $data['blog'] = ... to get all records from the blog via the model.
In the pagination part, you have $data['results'] = .... to get only the records for the specified page.
The foreach { } part in the view refers to $blog. That's why it shows all records, regardless of the pagination settings. Replace it with $results.
Besides that, don't loop through a foreach with foreach ($blog as $blog). Better is:
PHP Code:
Foreach($results as $result) {
 
 

$results: prural (is a collection of objects or arrays)
$result: single (is a single object or array)


RE: Can you check my pagination? - kirasiris - 02-09-2017

(02-09-2017, 08:56 AM)Wouter60 Wrote: In your controller, you first have $data['blog'] = ... to get all records from the blog via the model.
In the pagination part, you have $data['results'] = .... to get only the records for the specified page.
The foreach { } part in the view refers to $blog. That's why it shows all records, regardless of the pagination settings. Replace it with $results.
Besides that, don't loop through a foreach with foreach ($blog as $blog). Better is:
PHP Code:
Foreach($results as $result) {
 
 

$results: prural (is a collection of objects or arrays)
$result: single (is a single object or array)

I did that on my view as you said, but nows it shows another error:

Home Blog

A PHP Error was encountered
[/url]Severity: Error
Message: Cannot use object of type stdClass as array
Filename: blog/index.php
Line Number: 15
Backtrace:


Here is my view as you wanted me to do:

PHP Code:
<?php foreach($results as $result ?>
<div class="card text-xs-left"><!-- FIRST ARTICLE DEMO -->
<div class="card-header" id="article-header">
<h4><a href="<?php echo site_url('/blog/'.$result['slug']); ?>"><?php echo ucfirst($result['titulo']); ?></a></h4>
</div>
<img src="<?php echo $result['imagen']; ?>" width="750" heihgt="350" class="img-fluid">
<!--<div class="card-body">
<p> <?php  echo word_limiter($result['contenido'],5); ?></p>
</div>-->
<div class="card-footer" id="article-footer">
<div class="row">
<div class="col-lg-12 col-md-9 col-sm-8">
<i class="fa fa-calendar" aria-hidden="true"></i> <?php echo ucfirst($result['fecha']); ?> 

<i class="fa fa-folder" aria-hidden="true"></i> <?php echo $result['categoria_id']; ?> 

<a href="<?php echo site_url('/blog/'.$result['slug']); ?>" class="pull-right">Read more »</a>
</div>
</div>
</div>
</div>
<?php ?>

[url=http://localhost/demo6/blog/%3Cbr%20/%3E%3Cb%3EFatal%20error%3C/b%3E:%20%20Cannot%20use%20object%20of%20type%20stdClass%20as%20array%20in%20%3Cb%3EC:/xampp2/htdocs/demo6/application/views/news/blog/index.php%3C/b%3E%20on%20line%20%3Cb%3E15%3C/b%3E%3Cbr%20/%3E%3Cdiv%20style=]Bythe way, thanks for wanting to help, I'm grateful that somebody is actually trying to help me with this.


RE: Can you check my pagination? - Wouter60 - 02-09-2017

I think the problem is in your model. The get_paginas function is returning an array of objects, not arrays.
You can solve this in two different ways.

1. In your view. Instead of array keys, use the object notation, like $result->field_name.

2. In the model. Let the function get_paginas return the records as a set of arrays:

PHP Code:
public function get_paginas($limit$start) {
 
   $this->db->limit($limit$start);
 
   $query $this->db->get("blog");

 
   if ($query->num_rows() > 0) {
 
       return $query->result_array();
 
   }

 
   return false;




RE: Can you check my pagination? - kirasiris - 02-10-2017

Ok, I did that as well, but it now shows just one entry, and apparently the same id. Basically it just showing one item rather than one different per page


RE: Can you check my pagination? - pdthinh - 02-10-2017

(02-10-2017, 12:50 PM)kirasiris Wrote: Ok, I did that as well, but it now shows just one entry, and apparently the same id. Basically it just showing one item rather than one different per page

Comment these two lines in your blog() may work:
PHP Code:
    // $config['query_string_segment'] = 'paginas';
 
   // $config['page_query_string'] = TRUE;