Code Igniter pagination 404 error - php

I am having problems with CodeIgniter pagination module.
When I go to click on the links I get a 404 error. As I understand and I may be incorrect, but it would seem that the URI segment which determines the offset is not being read inside the index function of my controller and that code igniter is actually looking for this as a function inside the controller hence throwing the 404 when it cant find it.
I have googled around and checked on the forum to find a solution but have not had any success. If anyone has any hints or tips that would be great I have included the relevant code from my controller, model and view below.
//Controller
$this->load->library('pagination');
$config['base_url'] = '/yourhistory/';
$config['total_rows'] = $this->db->get('gallery')->num_rows();
$config['per_page'] = 3;
$config['uri_segment'] = 2;
$this->pagination->initialize($config);
$data['galleryitems'] = $this->pages_model->get_gallery($config['per_page'], $config['uri_segment']);
$this->load->view('/gallery', $data);
//Model
function get_gallery($limit, $segment)
{
$query = $this->db->get('gallery', $limit, $this->uri->segment($segment));
if ($query->num_rows > 0)
{
foreach($query->result() as $row)
{
$data[] = $row;
}
return $data;
}
}
//View
echo $this->pagination->create_links();

Well its going to fail first off because of your base_url:
$config['base_url'] = 'http://example.com/index.php/test/page/';
You need the full path, altho I am just guessing here as you don't provide the URL that is throwing the 404.
Notes on pagination here: http://codeigniter.com/user_guide/libraries/pagination.html

Your base_url is not correct:
//should be something like this
$config['base_url'] = base_url().'/controllername/functionname/';
See: http://codeigniter.com/user_guide/libraries/pagination.html

Related

$this->uri->segment(2) giving me 404 error when in Index function in controller

I'm creating a users profile section for my website, and the URL would be /users/handle/
for some reason when using $this->uri->segment(2) it is just sending me to my 404 error page.
The weird thing is, if i change the function to profiles and then make it segment(3) and go to the url /users/profile/handle the code works fine and loads up the right view.
I've tried changing the segment number to 0,1,2 & 3 and neither seem to work when in my users Index function.
Here is the function for my index below.
public function index() {
$username = trim(strip_tags($this->uri->segment(2)));
if (!$username) {
$data['error'] = _('User not found');
$this->load->view('user-profiles', $data);
} else {
$profile = $this->db->get_where("users", ["handle" => $username]);
$profile = $profile->row();
$data['profile'] = $profile;
$this->load->view('user-profiles', $data);
}
}
Any help would be greatly appreciated! thanks
I seemed to of worked it out.
For anyone else who runs into a similar problem, i edited my routes.php file and added
$route['users/(:any)'] = "users/index/$1";

Redirect user to a self-made 404 page when users input wrong arguments

I'm using Codeigniter version 3.0.6 and now I have a controller named modifyfiles. The index function of this controller accepts an argument.
Using the given argument, the index controller will search a desired data in a model. The following code is my index function :
public function index($id){
$_SESSION['logged_in']['idmod'] = $id;
$this->data['modullist'] = $this->__getmodulbyid($id);
$this->data['lecturelist'] = $this->__getlecturelist();
if(count($this->data['modullist']) <= 0){
show_404();
} else {
$this->load->view($this->layout, $this->data);
}
}
It works very well for now, when I try to insert wrong number arguments it directs me to the codeigniter built-in 404 page.
But when I try to insert some non numbers arguments, say some characters, it directs me to my own 404 page.
Here's my routing rules :
$route['404_override'] = 'home/pagemissing';
$route['dosen/modifymodul'] = 'dosen-tools/modifymodul';
$route['dosen/modifymodul/(:num)'] = 'dosen-tools/modifymodul/index/$1';
I want when users input wrong arguments, no matter they were numbers or characters, the function will redirect to my own 404 page, not the built-in one. How can I achieve that?
I'm pretty sure the problem is in my conditional block
if(count($this->data['modullist']) <= 0){
show_404();
} else {
$this->load->view($this->layout, $this->data);
}
What should I write to replace the show_404() ?
Or if I'm not wrong, how can I replace the show_404() with an 404_override trigger?
Solved
I did a little research and ended up creating this callback in one of my routing rules
$route['dosen/modifymodul/(:num)'] =
function ($id){
if(is_numeric($id)){
return 'dosen-tools/modifymodul/index/'.$id;
} else {
return 'home/pagemissing';
}
};
Thanks everybody

A folder inside views folder, Routing and/or Controller error in CI?

I'm new to CI, I barely managed to start my own website and had it running for a while now, I had all the views structure as the CI guide, but then I want to put a php file called igniel.php inside a folder called "vcard" inside the pages folder.
The problem is, when I try to load it using a full path like :
http://example.com/application/views/pages/vcard/igniel.php
its showing up nicely, but once I use
http://example.com/igniel (without the php)
it will show a 404 error.
here is my Pages.php content :
<?php
class Pages extends CI_Controller {
public function view($page = 'home')
{
if ( ! file_exists(APPPATH.'/views/pages/'.$page.'.php'))
{
// Whoops, we don't have a page for that!
show_404();
}
$data['title'] = ucfirst($page); // Capitalize the first letter
$this->load->view('templates/header');
$this->load->view('templates/nav');
$this->load->view('pages/'.$page);
$this->load->view('templates/footer');
}
}
and here is my routes.php content :
$route['default_controller'] = 'pages/view';
$route['(:any)'] = 'pages/view/$1/$2';
$route['404_override'] = '';
$route['translate_uri_dashes'] = FALSE;
I've tried to googling and use a few suggestion around but still no luck...
many thanks in advance.
Best,
You are not sopposed to call view file. Instead you should make request through controllerwhich is meant to call specific file. So you never want to call (nor I am sure how you succeeded in request with that URL):
http://example.com/application/views/pages/vcard/igniel.php
but
http://example.com/pages/view/igniel
(withouth the extension).
You should follow basic example from CI documentation. In your case it means you are trying to use subdirectory vcard with same code in controller that doesn't fit your needs.
class Pages extends CI_Controller
{
public function view($page = 'home')
{
if ( ! file_exists(APPPATH.'/views/pages/'.$page.'.php'))
{
// Whoops, we don't have a page for that!
show_404();
}
elseif ( $page == 'igniel' )// your page is in subdirectory
{
$data['title'] = ucfirst($page); // Capitalize the first letter
$this->load->view('templates/header');
$this->load->view('templates/nav');
$this->load->view('pages/vcard/'.$page);
$this->load->view('templates/footer');
}
else
{
$data['title'] = ucfirst($page); // Capitalize the first letter
$this->load->view('templates/header');
$this->load->view('templates/nav');
$this->load->view('pages/'.$page);
$this->load->view('templates/footer');
}
}
}
But, if you want to call it like:
http://example.com/igniel
You have to change line in controller to include that file within it's path as described in elseif line.
Routing should take place like:
$route['(:any)'] = 'pages/view/$1';
If you have more subdirectories, you have to fit code in controller to aproach those as I describe it here for you.

CodeIgniter : Routing URL on Codeigniter

I just learned about routing in CodeIgniter and now I am lil bit confuse.
I have an URL like this : http://localhost/norwin/list_group/get_product_by_group/1
Which is:
list_group is controller,
get_product_by_group is method,
and '1' is passing parameter.
I want to minimize the URL with route. So I can have an URL like :
http://localhost/norwin/group/'group_name'
And this is my code on route.php :
$route['group/(:any)'] = 'list_group/get_product_by_group/$1';
this is my controller :
public function get_product_by_group($group_name)
{
if($this->uri->segment(3))
{
$this->load->database();
$data['product_data'] = $this->group_model->list_product_by_group($group_name);
$this->load->view('fend/list_product', $data);
}
else
{
redirect('list_group');
}
}
And this is my code on view which call the controller :
<?= base_url('group/'. $value->group_name);?>
my problem is : I can not get any result of the route, it always send me to 'list_group'.
Maybe someone here have a good approach to do this.
Thanks for your help.
You are being redirected because $this->uri->segment(3) is not returning anything as your URL http://localhost/norwin/group/'group_name' have only 2 segments.
Codeigniter $this->uri->segment(n); works in the following way.
URL: http://localhost/norwin/group/group_name/some_test
it will return
$this->uri->segment(1); // group
$this->uri->segment(2); // group_name
$this->uri->segment(3); // some_test
you have to change $this->uri->segment(3) to $this->uri->segment(2)

codeigniter 404 page not found trying to view one item

This is my first question on here so please be kind! I'm setting up a Codeigniter program for the first time, and while I have my index view working, I can't get the individual view page to work. It returns a 404 page not found error.
I have code in my foreach for the index view pointing to where I think the url should be:
<a href=<?= site_url("kittens/view/" . $item->id)?>>Details</a>
This takes me to localhost:8888/index.php/kittens/view/1 if I click on the kitten with an id of 1. But I get a 404 error.
Here is my controller function for the view:
function view(){
$this->load->model("kittensmodel");
$this->load->view('_header');
$this->load->view('detailview', $data);
$data['kitten_item']= $this->kittensmodel->details();
}
And here is the function in my model:
function details(){
$id = $this->url->segment(3);
$this->db->select('*')
->from('kittens')
->where('id', $id);
$data = $this->db->get();
return $data->result();
}
And my detailview file just has this, to test it:
<?php
print_r($kitten_item);
?>
I've been playing around with the routes but haven't had any luck with it. Here's what I have right now:
$route['default_controller'] = 'welcome';
$route['404_override'] = '';
$route['translate_uri_dashes'] = FALSE;
$route['kittens/(:any)'] = 'kittencontroller/view/$1';
I'm new to php and codeigniter and the answer might be really simple, but I would really appreciate the help!
try to use this link
<a href=<?= site_url("kittens/".$item->id)?>>Details</a>
try it
<a href=<?= site_url("kittens/view?q=" . $item->id)?>>Details</a>
in controller
function view(){
$q = $this->input->get('q',True);
$this->load->model("kittensmodel");
$data['kitten_item']= $this->kittensmodel->details($q);
$this->load->view('_header');
$this->load->view('detailview', $data);
}
in model
function details($q){
$this->db->select('*')
->from('kittens')
->where('id', $q);
$data = $this->db->get();
return $data->result();
}
and in detailview
<?php
print_r($kitten_item);
?>
I think it is because your site uri link should be base_url and
Details
<a href=<?= base_url("kittens" .'/'. $this->url->segment(3))?>>Details</a>
Routes:
$route['kittens/(:any)'] = 'folder/controller-name/$1';
$route['kittens/(:any)'] = 'controller-name/$1';
Or
$route['kittens/(:any)'] = 'folder/controller-name/index/$1';
$route['kittens/(:any)'] = 'controller-name/index/$1';
Always make sure your link matches your routes that you set.
If your Controller is actually called kittenscontroller, then your route should be
$route['kittens/(:any)'] = 'kittenscontroller/view/$1';
CI will generate a 404 Page Not Found if your controller does not exist.
I noticed you use plural kittensxxxx everywhere else except in your controller used in the above route.
You Should always check this sort of thing by manually entering the actual URL to test it.
IF you are going to use that route, then your Links should become
Details

Categories