Noel Peña Blanco Noel Peña Blanco - 6 months ago 15
SQL Question

List some users of a Table in CodeIgniter

Hi I want to list some users from a table in my data base.

Model:

class listPatients {

private $list_patients;

function __construct() {
$this->list_patients = array($patient);
}
//put your code here
function list_patients() {

$this->db->from('Users');
$this->db->where('patient', 1);
$this->db->select('id, username, Email');
$patient[]=$this->db->get()->result();
return $patient[];
}


`

View

<?php echo $patient[] ?>


Control:

public function list_patients() {
$data["list_patients"] = $this->listPatients->list_patients();

$this->load->view('mainview',$data);

}


I want to show a list of the patients(Users with column patient = 1) and I am getting some problems can someone help

Answer

After retrieving data from Db dont pass the entire result_object. instead of passing the entire $patient(result object). store the result_object to a variable and pass it ie $patient=$patient[0]; change this line in your model

function list_patients() {
    $this->db->from('Users');
    $this->db->where('patient', 1);
    $this->db->select('id, username, Email');
    $patient=$this->db->get()->result();//change
    $patient = $patient[0];//
    return $patient;//
}

//controller

 public function list_patients() {
    $data["list_patients"] = $this->listPatients->list_patients();
    $this->load->view('mainview',$data);
 } 

//view in mainview.php 

foreach ($list_patients as $value) {                  
    echo $value->patient;
}