如何从codeigniter中的分页类开始

时间:2017-02-08 09:01:45

标签: php sql codeigniter pagination codeigniter-3

这是我的问题: 我正在尝试为我的博客页面创建一个分页,但由于某种原因,我尝试将我想要显示的条目数量,但它不起作用。我不知道为什么?;任何帮助都会有所帮助,谢谢。

这是我的控制器:

        public function blog()
    {

            // Pagination for Blog //

            $data['blog'] = $this->blog_model->get_blog();
            $data['categorias'] = $this->categorias_model->get_categorias();
            $data['title'] = 'Blog';


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

            $config['base_url'] = site_url('/blog/');
            $config['total_rows'] = 200;
            $config['per_page'] = 1;
            $config['uri_segment'] = 3;
            $config['num_links'] = 2;
            $config['use_page_numbers'] = TRUE;
            $config['page_query_string'] = TRUE;

            $this->pagination->initialize($config);




            $this->data['pagination'] = $this->pagination->create_links();
            $this->load->view('templates/head',$data);
            $this->load->view('templates/navbar',$data);
            $this->load->view('news\blog\index.php',$this->data);
            $this->load->view('templates/footer',$data);


    }

我的模特:

<?php
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');


            $query=$this->db->get('blog');
            return $query->result_array();
            }

            $query=$this->db->get_where('blog', array('slug'=>$slug));
            return $query->row_array();
        }


    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'),
            'fecha' => $this->input->post('fecha'),
            'contenido' => $this->input->post('contenido'),
            'categoria_id' => $this->input->post('categoria_id'),

        );

        return $this->db->insert('blog',$data);

        }


        public function delete_post($id){
        $this->db->where('id', $id);
        $this->db->delete('blog');
        return true;
    }

    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'),
            '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);
    }





    }

以下是我的观点:

<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>

1 个答案:

答案 0 :(得分:0)

又来了:

我的控制员:

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);
}

然后又是我的模特:

    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();
//      }



        }