php - Error trying to fetch data codeigniter -
i got 2 tables , gotta fetch information, tables:
cursadas" includes:(id, user_id[is foreign key column "id" of table "usuarios"], subject_id[is foreign key column "id" of table "materias"], grade, date)
"usuarios" includes:(id,username,name,lastname,password,type,status,date)
"materias" includes:(id, career_id, name, description, hours)
i need this:
this error get:
my view file:
<html> <head> </head> <body> <div class="container"> <div class="row"> <div class="col-md-12"> <h2 align="center">table:study</h2> <input id="busqueda_tabla" type="text"> <table class="table table-hover" align="center" border="1" cellspacing="0" cellpadding="0" width="700" id="tabla_busqueda"> <thead> <th>id</th> <th>user</th> <th>subject</th> <th>grade</th> <th>date</th> <th>action</th> </thead> <tbody> <?php if (count($records) > 0 && $records != false) { foreach($records $record) { echo "<tr> <td>".$record['id']."</td> <td>".$record['user']."</td> <td>".$record['subject']."</td> <td>".$record['grade']."</td> <td>".$record['date']."</td> <td align='center'> <button type='button' class='btn btn-primary'>editar</button></a> | <button type='button' class='btn btn-danger'>borrar</button></a> </tr>"; } } ?> </tbody> </table> </div> </div> </div> </body> </html> my controller file (home):
<?php class home extends ci_controller{ public function __construct(){ parent::__construct(); $this->load->model("crudmodel"); } public function index(){ # data in study table $selectstudys = $this->crudmodel->selectstudys(); foreach ($selectstudys $key => $study) { # usernames $user = $this->crudmodel->getname($study['user_id']); #get subject names $subject = $this->crudmodel->getsubname($study['subject_id']); #append both new values same array if(!empty($user[0]['username'])){ $data[$key]['user_id'] = $user[0]['username']; // main problem can this. may not getting value query why have put validation on model function , error handler condition here }else{ $data[$key]['user_id'] = ''; // or else condition can use error handler } if(!empty($subject[0]['name'])){ $data[$key]['subject_id'] = $subject[0]['name']; // main problem can this. may not getting value query why have put validation on model function , error handler condition here }else{ $data[$key]["subject_id"] = ""; // or can use error handler } } $data['records'] = $selectstudys; $this->load->view('home', $data); } } ?> crudmodel:
class crudmodel extends ci_model{ public function __construct(){ parent::__construct(); $this->load->database(); } function selectstudys() { $query= $this->db->query("select * cursadas"); if($query->num_rows()>0){ $result = $query->result_array(); }else{ $result = ""; // or can use error handler return $result; } } function getname($name) { $query= $this->db->query("select username usuarios id = $name "); if($query->num_rows()>0){ $result = $query->result_array(); }else{ $result = ""; // or can use error handler return $result; } } hope can me :/
i have updated controller , model:
updated controller file (home):
<?php class home extends ci_controller{ public function __construct(){ parent::__construct(); $this->load->model("crudmodel"); } public function index(){ # data in study table $selectstudys = $this->crudmodel->selectstudys(); $data['records'] = $selectstudys; $this->load->view('home', $data); } } updated model:
class crudmodel extends ci_model { public function __construct(){ parent::__construct(); $this->load->database(); } function selectstudys() { $this->db->select('cursadas.id, cursadas.user_id, usuarios.username user, cursadas.subject_id, materias.name subject, cursadas.grade, cursadas.date'); $this->db->from('cursadas'); $this->db->join('usuarios', 'usuarios.id=cursadas.user_id', 'left'); $this->db->join('materias', 'materias.id=cursadas.subject_id', 'left'); $query = $this->db->get(); return $query->result_array(); } function getname($name) { $query= $this->db->query("select username usuarios id = $name "); if($query->num_rows()>0){ $result = $query->result_array(); } else { $result = ""; // or can use error handler return $result; } } } 

Comments
Post a Comment