Domanda

I'm trying to load a blog type page, I load the entries from my database with this controller

    public function blog($page){
    $this->load->model("model_get");
    $this->load->view("site_header");
    $this->load->view("site_nav");
    $counter = $this->model_get->getBlogcount();

    for($counter; $counter > 0; $counter --){
        $data["results"] = $this->model_get->getBlog($counter);
        $this->load->view("content_blog", $data);
        }
    }

    $this->load->view("site_footer");
}

and this model

    function getBlogcount(){
        $result = $this->db->count_all("blog");

        return $result;
    }

I count the entries in the database where I call them out by their ID. But now I'm trying to create multiple pages that expand automatically everytime I enter a new entry. So lets say I have 27 entries, and want to have no more than 5 entries on a single page, how do I make it so that it creates the necessary 6 pages to show them without loading the other 3 empty entries and stuff. I'm new to codeigniter and have always worked with ASP .NET, any help would be helpfull. Thanks in advance!

p.s. english isn't my first language

È stato utile?

Soluzione

CodeIgniter have his own pagination Class. Take a look here : http://ellislab.com/codeigniter/user-guide/libraries/pagination.html

You can try this firstly and adapt to your project :

public function blog($page = 0)
{
   $this->load->library('pagination');
   $this->load->helper('url');

   $config['base_url'] = base_url('blog/'. $page);
   $config['total_rows'] = $this->model_get->getBlogcount();
   $config['per_page'] = 5;

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

   $data['results'] = $this->model_get->getBlog($config['per_page'], $page);
   $this->load->view("content_blog", $data);
}

Edit your "getBlog" function model to get results with limit clause, like this :

function getBlog($limit, $start)
{
    $results = $this->db->limit($limit, $start)->get('your_blog_table');

    if ($results) 
    {
        return $results->result();
    }

    return FALSE;
}

And use in your view this code to create your pagination links :

echo $this->pagination->create_links();
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top