I have been working on a code Igniter project and I am almost done. I finally added Pagination but have one issue. When I scroll through my pagination links on my page and then click to go to my home page, I get an error message. Trying to break this down so that others can better understand it, my blog page, which has pagination has this URL:
proj1/index.php/blog
my URL for the home page is:
proj1/index.php/home
when I click on my pagination link for "2" which shows the next set of blogs, I got to this URL:
proj1/index.php/blog/2
Now if I am on blog/2 and then click "home" it takes me here:
proj1/index.php/blog/home
if I go to my resume page I see this:
proj1/index.php/blog/work
Basically, I am not going back to my home page nor my resume page. Is anyone able to help fix this problem? Thank you for your help!!
Here is what my blog controller looks like:
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class Blog extends CI_Controller {
//This function begins to construct the controller.
public function __construct()
{
parent::__construct();
$this->load->model('Blog_model');
$this->load->helper('url_helper');
//pagination being loaded from the library
$this->load->library('pagination');
}
public function index()
{
//This line sets the page for the base URL
$config['base_url'] = base_url('index.php/blog');
$config['total_rows'] = $this->Blog_model->count_items();
$config['per_page'] = 2;
$this->pagination->initialize($config);
$data['title'] = 'Blog archive';
$data['pagination'] = $this->pagination->create_links();
$start = '';
$slug = '';
$data['blog'] = $this->Blog_model->get_items($config['per_page'], jjjj $this->uri->segment(2));
$this->load->view('templates/header', $data);
$this->load->view('blog/index', $data);
$this->load->view('templates/footer');
}
public function view($slug = NULL)
{
$data['blog_item'] = $this->Blog_model->get_blog($slug);
if (empty($data['blog_item']))
{
show_404();
}
$data['title'] = $data['blog_item']['title'];
$this->load->view('templates/header', $data);
$this->load->view('blog/view', $data);
$this->load->view('templates/footer');
}
public function create()
{
$this->load->helper('form');
$this->load->library('form_validation');
$data['title'] = 'Create a Blog Entry';
$this->form_validation->set_rules('title', 'Title', 'required');
$this->form_validation->set_rules('body', 'Body', 'required');
if ($this->form_validation->run() === FALSE)
{
$this->load->view('templates/header', $data);
$this->load->view('blog/create');
$this->load->view('templates/footer');
}
else
{
$this->Blog_model->set_blog();
redirect('blog');
}
}
}
Here is my blog model:
<?php
class Blog_model extends CI_Model {
//This function connects to the database and loads it.
public function __construct()
{
parent::__construct();
$this->load->database();
}
//This function counts all of the items in the blog table.
public function count_items()
{
return $this->db->count_all('blog');
}
//function pulls items out of the array specifically by newest date first.
function get_items($limit, $offset)
{
$data = array();
$this->db->limit($limit, $offset);
$this->db->order_by('entry_date', 'desc');
$query = $this->db->get('blog');
if ($query->num_rows() > 0){
foreach ($query->result_array() as $row){
$data[] = $row;
}
}
$query->free_result();
return $data;
}
public function set_blog()
{
$this->load->helper('url');
$slug = url_title($this->input->post('title'), 'dash', TRUE);
$data = array(
'title' => $this->input->post('title'),
'slug' => $slug,
'body' => $this->input->post('body')
);
return $this->db->insert('blog', $data);
}
}
Finally, my routes look like this:
$route['message'] = 'contact/create';
$route['contact'] = 'contact';
$route['blog/(:any)'] = 'blog/index/$1';
$route['create'] = 'blog/create';
$route['blog'] = 'blog';
$route['(:any)'] = 'pages/view/$1';
$route['default_controller'] = 'pages/view';
Thank you for any help and please let me know if you need me to post more information. I do not think that this is a hard issue to fix. However, it has stumped me! Again, thank you for the help!