0
votes

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!

4
From where $data comes over here in $this->load->view('detailview', $data); and you are calling view before getting $data valueNarendrasingh Sisodia

4 Answers

0
votes

try to use this link

<a href=<?= site_url("kittens/".$item->id)?>>Details</a>
0
votes

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);
?>
0
votes

I think it is because your site uri link should be base_url and

<a href="<?php echo base_url('kittens'.'/'. $this->url->segment(3))?>">Details</a>

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

0
votes

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

<a href="<?= site_url("kittens/" . $item->id)?>">Details</a>