Hello i'm having problem on passing some data from my Model to my View, with this following code:
Controller:
class Welcome extends CI_Controller {
function __construct()
{
parent::__construct();
$this->load->library('session');
$this->load->helper('form');
$this->load->helper('url');
}
public function testing(){
$query = $this->Model->getEmployees();
if(isset($query)){
$data['fds'] = $query;
}
$this->load->view('UserPage', $data);
}
}
Model:
class Model extends CI_Model{
function __construct()
{
parent::__construct();
$this->load->library('session');
}
public function getEmployees(){
$this->db->select('*');
$this->db->where("idcliente='".$_SESSION['userid']."'");
$query=$this->db->get('marcacao');
return $query->num_rows() > 0 ? $query->result() : NULL;
}
}
View:
<table class="table table-striped table-bordered">
<tr>
<td><strong>Employee Id</strong></td>
<td><strong>First Name</strong></td>
<td><strong>Last Name</strong></td>
</tr>
<?php foreach($fds as $employee){?>
<tr>
<td><? =$employee->data;?></td>
<td><? =$employee->hora;?></td>
<td><? =$employee->tipo;?></td>
</tr>
<?php }?>
</table>
I've tried several things that I've seen here but none works... Hope you can help me.
first things change this :
$query = $this->Model->getEmployees();
if(isset($query)){
$data['fds'] = $query;
}
become :
$id = $_SESSION['userid'];
$data["fds"] = $this->Model->getEmployees($id);
now on your model :
public function getEmployees($id){
$this->db->select('*');
$this->db->where("idcliente=",$id);
$query=$this->db->get('marcacao');
return $query->result_array();
now in your view()
<?php
if($fds != NULL){
foreach($fds as $employee){?>
<tr>
<td><?=$employee["data"];?></td>
<td><?=$employee["hora"];?></td>
<td><?=$employee["tipo"];?></td>
</tr>
<?php
}
}?>
First of all as i can see in your code the variable $data
is initialized inside the if block so its scope will remain within that if block only. Also before calling a model you are suppose to load the model.
public function testing(){
$data = array();
$this->load->model("Model");
$query = $this->Model->getEmployees();
if(isset($query)){
$data['fds'] = $query;
}
$this->load->view('UserPage', $data);
}
Try Doing This ....
For Controller :
class Welcome extends CI_Controller {
function __construct()
{
parent::__construct();
$this->load->library('session');
$this->load->helper('form');
$this->load->helper('url');
$this->load->model('Model');
}
public function testing(){
$query = $this->Model->getEmployees(); // replace "Model_name" with the name of your model
$data = array();
if(isset($query)){
$data['fds'] = $query;
}
$this->load->view('UserPage', $data);
} }
For Model :
class Model extends CI_Model{
function __construct()
{
parent::__construct();
$this->load->library('session');
$this->load->database();
}
public function getEmployees(){
$this->db->select('*');
$this->db->where("idcliente='".$_SESSION['userid']."'");
$query=$this->db->get('marcacao');
if($query->num_rows() > 0)
{ return $query->result(); }
else{ return NULL; }
}
}
Now for the view named UserPage.php
<table class="table table-striped table-bordered">
<tr>
<td><strong>Employee Id</strong></td>
<td><strong>First Name</strong></td>
<td><strong>Last Name</strong></td>
</tr>
<?php foreach($fds as $employee){?>
<tr>
<td><? =$employee->data;?></td>
<td><? =$employee->hora;?></td>
<td><? =$employee->tipo;?></td>
</tr>
<?php }?>
</table>
Figured it out... Had to put this in the model function:
$this->load->database();