尝试获取数据时的错误codeigniter

时间:2017-04-06 13:00:28

标签: php mysql codeigniter

我有两张桌子,我需要获取信息,我的桌子:

  

cursadas“包括:(id,user_id [是表”usuarios“的列”id“的外键],subject_id [是表”materias“列”id“的外键“],年级,日期)

     

“usuarios”包括:(ID,用户名,姓名,姓氏,密码,类型,状态,日期)

     

“materias”包括:(id,career_id,名称,描述,小时)

我需要这样的东西:

enter image description here

这是我得到的错误:

enter image description here

我的观点文件:

    <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 as $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>

我的控制器文件(主页):

    <?php

    class Home extends CI_Controller{

         public function __construct(){
             parent::__construct();

             $this->load->model("Crudmodel");

        }


 public function index(){

    # get all data in Study table
    $selectStudys = $this->Crudmodel->selectStudys();


    foreach ($selectStudys as $key => $study) 
    {
        # get UserNames
        $user = $this->Crudmodel->getName($study['user_id']);

        #get Subject Names
        $subject = $this->Crudmodel->getSubName($study['subject_id']);


        #append both NEW VALUES to same array

        if(!empty($user[0]['username'])){
        $data[$key]['user_id'] = $user[0]['username'];
        // your main problem can be this. may be it is not getting value from query this is why we have put validation on model function and error handler condition here
        }else{
         $data[$key]['user_id'] = ''; // or anything as your else condition you can use as error handler 
        }
        if(!empty($subject[0]['name'])){
        $data[$key]['subject_id'] = $subject[0]['name'];
        // your main problem can be this. may be it is not getting value from query this is why we have put validation on model function and error handler condition here
        }else{
           $data[$key]["subject_id"] = "";
          // or anything you can use as 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 * FROM cursadas");
    if($query->num_rows()>0){
       $result = $query->result_array();
     }else{
      $result = "";
          // or anything you can use as error handler
      return $result;
    }
}

function getName($name)
{
    $query= $this->db->query("SELECT username FROM usuarios WHERE id = $name ");
    if($query->num_rows()>0){
    $result = $query->result_array();
    }else{
    $result = "";
          // or anything you can use as error handler
    return $result;
  }
}

希望你能帮助我:/

1 个答案:

答案 0 :(得分:0)

我更新了控制器和型号:

更新了控制器文件(主页):

<?php

class Home extends CI_Controller{

    public function __construct(){
        parent::__construct();

        $this->load->model("Crudmodel");

    }


    public function index(){

        # get all data in Study table
        $selectStudys = $this->Crudmodel->selectStudys();


        $data['records'] = $selectStudys;
        $this->load->view('home', $data);

    }

}

更新型号:

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 as user, cursadas.subject_id, materias.name as 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 FROM usuarios WHERE id = $name ");
        if($query->num_rows()>0){
        $result = $query->result_array();
        } else {
        $result = "";
            // or anything you can use as error handler
        return $result;
      }
    }
}