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:
After Clicking item, it will show like this:
How can I solve this?