-1

In my routes.php file I have added the following code for routing:

$route['report/:num'] = "home/reportcard/$1";

Here is my controller code:

<?php
    defined('BASEPATH') OR exit('No direct script access allowed');

    class Home extends CI_Controller
    {

        function __construct() {
            parent::__construct();

        if(empty($this->session->userdata('id_user'))){
            $this->session->set_flashdata('flash_data', 'You cannot access');
                    redirect('login');
        }
        }
        public function index(){
            $this->load->model("item_model");
            $data['records'] = $this->item_model->getAllItems();
            $this->load->view('home',$data);
        }

       function reportcard($id){
            $this->load->model("item_model");
            $data['report'] = $this->item_model->getReport($id);
            $this->load->view('report', $data);  
        }

        function logout(){
            $data=['id_user','username'];
            $this->session->unset_userdata($data);
            redirect('login');

        }
    }

Here is my model code:

<?php
    class Item_model extends CI_Model
    {
        function getAllItems()
        {
            $this->load->database();
            $q = $this->db->get("item");
            if($q->num_rows() > 0)
            {
                return $q->result();
            }
            return array();
        }

        public function getReport($id){
            $this->db->select('*');
            $this->db->from('item');
            $this->db->where('item.id', $id);  
            $query = $this->db->get();    
            if($query->num_rows() > 0)
                return $data->result();
        }

}
?>

this is the code of view. I just print the array in my report_view for testing purpose:

<?php
defined('BASEPATH') OR exit('No direct script access allowed');
print_r($report);
?>

this is my home_view code:

<div class="row">
                <ul class="home-grid">
                    <?php foreach ($query->result() as $row): ?>
                    <li>
                        <a  href="<?php echo base_url() ?>report/<?=$row->item ?>/<?=$row->id ?>" class="btn btn-lg btn-warning view-report"><span class="glyphicon glyphicon-list-alt"></span> <br/>
                        <?=$row->item ?><br/>
                         <small>Click here for see report</small>
                        </a>
                    </li>
                    <?php endforeach; ?>
                </ul>

I have tried multiple times to view the array. But failed to get array. How can I load the view? When I click my items from home, it will create a url like http://localhost/super_shop_register/report/1

But didn't show any data. Show "Object not found!". Here is the image of my view: Home View After Clicking item, it will show like this: after clicking item

How can I solve this?

Abanoub Makram
  • 463
  • 2
  • 13
Tanzila Islam
  • 93
  • 1
  • 12
  • 1
    Is super_shop_register part of your base_url? And in the view code you posted it looks like their should be two parameters after report/, $row->item and $row->id, why does only the id appear? – Pacio Apr 03 '17 at 08:57
  • I think it matches with another route can you post your full route – ARIF MAHMUD RANA Apr 03 '17 at 08:59
  • base_url is ok. When I took a snapshot, the code was only $row-id. That's why showed only id in the url. It's not a big deal at all. $config['base_url'] = 'http://localhost/super_shop_register'; – Tanzila Islam Apr 03 '17 at 09:00
  • Here is my route.php code: $route['default_controller'] = 'login'; $route['report/:num'] = "home/reportcard/$1"; $route['404_override'] = ''; $route['translate_uri_dashes'] = FALSE; – Tanzila Islam Apr 03 '17 at 09:02
  • I don't think the problem is in your codeigniter routing because that 404 wasn't generated by codeigniter but by your server. My guess is it is an .htaccess problem, but it's strange that it would only affect this one page. – Pacio Apr 03 '17 at 09:10
  • I think I didn't connect the page properly. But didn't get the exact problem as I am new in CodeIgniter. I have tried many times. But couldn't find any solution. – Tanzila Islam Apr 03 '17 at 09:14
  • is your file name of controller Home.php not home.php –  Apr 03 '17 at 09:20
  • file name is home.php – Tanzila Islam Apr 03 '17 at 09:22
  • 2
    No one notice this why ? In your model this is wrong return $data->result(); actually $query->result(); is right. – Dinesh Kumar Apr 03 '17 at 09:29
  • Wolfgang is correct that is should be Home.php. Depending on your enviornment it may or may not fix your problem, but if your site ever gets places in a Linux server this will create major problems, always title your files in Codeigniter with Capitalized names. @Nobita - OP responded to your post. It's not relevant to the problem anyway. – Pacio Apr 03 '17 at 09:31
  • I have tried both. home.php and Home.php didn't work – Tanzila Islam Apr 03 '17 at 09:34
  • Did you removed index.php from config and updated the .htaccess in your root? – Giri Annamalai M Apr 03 '17 at 13:56
  • Yeah. Now its working perfectly. – Tanzila Islam Apr 04 '17 at 05:44

5 Answers5

2

change your root from $route['report/:num'] = "home/reportcard/$1";
to

$route['report/(:num)'] = "home/reportcard/$1";

and

<a  href="<?php echo base_url() ?>report/<?=$row->item ?>/<?=$row->id ?>" class="btn btn-lg btn-warning view-report">

this line of code will create href="localhost/super_shop_register/report/Chips/1". You need to remove <?=$row->item ?>/ from it.

M Hussain
  • 381
  • 2
  • 14
1

In your model this is wrong return $data->result(); actually $query->result(); is right.

<?php
    class Item_model extends CI_Model
    {
        function getAllItems()
        {
            $this->load->database();
            $q = $this->db->get("item");
            if($q->num_rows() > 0)
            {
                return $q->result();
            }
            return array();
        }

        public function getReport($id){
            $this->db->select('*');
            $this->db->from('item');
            $this->db->where('item.id', $id);  
            $query = $this->db->get();    
            if($query->num_rows() > 0)
                return $data->result();
        }

}
?>
Dinesh Kumar
  • 649
  • 3
  • 8
1

Come on man, your code works perfect in my workout example

my route

$route['report/(:num)'] = "welcome/workout/$1";

My Welcome controller

public function workout($id)
    {

        echo $id;

    }

my url is

http://localhost/Code/report/1

my output is

1  

enter image description here

make sure you done the following

  1. removed the index.php on the config like this ($config['index_page'] = '';).
  2. Check .htaccess in root folder with index.php file

    and make it below

    RewriteEngine On

    RewriteCond %{REQUEST_FILENAME} !-f

    RewriteCond %{REQUEST_FILENAME} !-d

    RewriteRule ^(.*)$ index.php/$1 [L]

Reference: CodeIgniter object not found only the index function works

when i removed my .htaccess code, your error occurs

enter image description here

hope this helps :)

Community
  • 1
  • 1
Dinesh Kumar
  • 649
  • 3
  • 8
0

your route should be like this:

$route['report/(:num)'] = "home/reportcard/$1";

and make sure your htaccess file is there and have code:

RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule .* index.php?/$0 [PT,L]  
ram kumar
  • 104
  • 6
0

Simple you can try this

$route['report/(:num)'] = "home/reportcard";

then you can get ID with below method in controller and nothing pass in function reportcard argument

$report_id = $this->uri->segment(2);
ImBhavin95
  • 1,494
  • 2
  • 16
  • 29