Reputation: 29
How can I call the result in my view?
print_r
gives me Array ( [0] => 2017-09-25 [1] => 2017-09-27 )
In my view file
echo $coachact->session_date
gives me "non-object"
echo $session_date
gives me "undefined variable"
Controller code
public function editview($user_fname)
{
$returnData = array();
$this->load->model('User_model');
$this->User_model->checkIfLoggedIn();
$this->load->view('incf/header');
$this->load->view('schedulef/sced_tools');
$this->load->model('Schedule_model');
$coachact = $this->Schedule_model->get($user_fname);
if(!empty($coachact)){
$returnData['coachact'] = $coachact;
$returnData['user_fname'] = $user_fname;
}
$this->load->view('schedulef/Schedule_manage',$returnData);
//print_r($coachact);exit();
}
Model
public function get($user_fname)
{
$returnArray = array();
$query = $this->db->query("SELECT * FROM sessions WHERE user_fname = ?", ['user_fname' => $user_fname]);
foreach ($query->result() as $row) {
array_push($returnArray, $row->session_date);
}
return $returnArray;
}
Upvotes: 2
Views: 2034
Reputation: 16997
In my view file echo $coachact->session_date gives me "non-object" echo $session_date gives me "undefined variable"
As you can see below, in your code you are sending array to view
$coachact = $this->Schedule_model->get($user_fname);
if(!empty($coachact)){
$returnData['coachact'] = $coachact;
$returnData['user_fname'] = $user_fname;
}
$this->load->view('schedulef/Schedule_manage',$returnData);
So in your view you will have, 2 variables namely:
$coachact
$user_fname
To print the contents of $user_fname
, you just have to
echo $user_fname;
To view contents of $coachact
array you have access like below
echo $coachact[0]; // gives first element
To loop through results
<?php foreach($coachact as $key => $value): ?>
<p><?php echo $key. '=>' . $value;?></p>
<?php endforeach;?>
Upvotes: 1
Reputation: 697
In your controller code make change like this :
public function editview($user_fname)
{
$returnData = array();
$this->load->model('User_model');
$this->User_model->checkIfLoggedIn();
$this->load->view('incf/header');
$this->load->view('schedulef/sced_tools');
$this->load->model('Schedule_model');
$coachact = $this->Schedule_model->get($user_fname);
if(!empty($coachact)){
$returnData['coachact'] = $coachact;
$returnData['user_fname'] = $user_fname;
}
$this->load->view('schedulef/Schedule_manage',$returnData);
}
And in the view file directly access the value as
<?php echo $coachact; ?>
And
<?php echo $user_fname; ?>
And the output you are getting in the upper left corner is due to the echo
statement in the model file.
Here is your Modal Code :
public function get($user_fname)
{
$returnArray['sesdate'] = array();
$query = $this->db->query("SELECT * FROM sessions WHERE user_fname = ?", ['user_fname' => $user_fname]);
foreach ($query->result() as $row)
{
$sesdate = strtotime($row->session_date);
$sesdate = date("Fj Y",$sesdate);
array_push($returnArray['sesdate'], $sesdate);
}
return $returnArray;
}
And since there is more than one result you have to use a loop in the view file.
Upvotes: 1