Reputation: 95
I am fetching values from database and the values are successfully shown when i use print_r but i get an error when i try to echo the values out it gives me an error of Message:
Undefined index: return_date
my code for controller is as follows
public function latebooks(){
$now = date('m/d/Y');
$this->load->model('Time');
$id=$this->session->userdata('userid');
$this->load->model('Department');
$table=$this->Department->selecttable($id);
foreach($table as $q){}
$table = $q->department_name;
$table = strtolower($table);
$run=$this->Time->timecalculations($table);
print_r($run);
$this->load->view('Books/datetime',['query'=>$run]);
}
and my code for my model is as follows:
public function timecalculations($table){
$query= $this->db->get_where('issue_books',array('department_id'=>$table));
return $query->result_array();
}
and the code in the view is as follows
<?php
/**
* Created by PhpStorm.
* User: workspace
* Date: 05-10-2016
* Time: 13:46
*/
include 'header.php';
?>
<br /><br />
<?php
echo $query['return_date'];
?>
<?php
include 'footer.php';
?>
please forgive as i am novice here!Thanks in advance
Upvotes: 1
Views: 1017
Reputation: 16117
You are using result()
function in your MODEL for getting records, this will return data into object form not in array.
This:
echo $query['return_date'];
Should be:
echo $query->return_date;
After your modification in your question, you must need to check what are you getting in print_r($run)
.
If you have data in multi-dimensional array than you can use loop here, this will print all data either having one record or multiple.
<?php
foreach ($query as $key => $value) {
echo $value->return_date;
}
?>
If you want to use result_array()
function than you can use like:
<?php
foreach ($query as $key => $value) {
echo $value['return_date'];
}
?>
This post will help you to understand both functions: codeigniter, result() vs. result_array()
Upvotes: 1
Reputation: 1519
In your controller make these following changes.
$run['data']=$this->Time->timecalculations($table);
$this->load->view('Books/datetime',$run);
In your view page
echo $data[0]['return_date'];
Upvotes: 0
Reputation: 57
$query->result()
returns an array of objects, because it doesn't know whether there's supposed to be 1 result or 100,000 results. To just fetch a single result, you'd use $query->row()
instead.
But that would still give you the result as an object instead of an array, so if you want to use $query['return_date']
in your view, you'd use $query->row_array()
to get the row as an array.
Upvotes: 1
Reputation: 5398
Change this return $query->result();
in your model as follows
return $query->result_array();
** return $query->result();
this returns as object. If want to use this then access data like echo $query->return_date;
Upvotes: 1