尝试使用codeigniter

时间:2017-04-08 23:35:38

标签: php codeigniter fetch

我有3张桌子;我的桌子是" cursadas"," usuarios"和" materias"

  

" cursadas"包括:(id,user_id [是列的外键" id"表" usuarios"],subject_id [是列的外键&#34 ;表"表" materias"],成绩,日期)

     

" USUARIOS"包括:的(ID,用户名,名,姓,密码,类型,状态,日期)

     

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

这是我的决赛桌" cursadas"(来自表格" materias"以及" usuarios")的数据。

enter image description here

看一看,我需要这样的东西:

enter image description here

我收到了这个错误:

enter image description here

enter image description here

enter image description here

我认为我的查询存在错误,我不知道该怎么做才能使这项工作:S

这是我的代码: 我的视图文件(" usuario"):

            <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>
                </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['name']."</td>
                      <td>".$record['grade']."</td>
                      <td>".$record['date']."</td>
                  </tr>";
        }

       }
    ?>

</tbody>

</body>
</html>

我的控制器文件(&#34;登录&#34;):

    <?php

        Class Login extends CI_Controller{


        public function index(){


           $this->load->view('login_form');


        }

 public function do_login()
        {
         // load the form_validation library
         $this->load->library('form_validation');

         $this->form_validation->set_rules('usuario', 'Username', 'trim|required|min_length[3]|alpha_numeric');
         $this->form_validation->set_rules('contrasena', 'Password', 'trim|required|min_length[6]');

           // if there is errors
         if ($this->form_validation->run() == FALSE) { 
            // this will load your form with the errors

               $this->load->view('login_form'); 

         } else {
           // if no errors we will hit the database
            $user=$this->input->post('usuario', true);
            $pass=$this->input->post('contrasena', true);
            $cek = $this->m_login->proceso_login($user,$pass);
            $hasil=count($cek);

                if($hasil > 0)
                {

                   $pelogin =$this->db->get_where('usuarios',array('username' => $user, 'password' => $pass))->row();
                  // here $pelogin has the id of the user 
                 // create session like this 
                  $this->session->set_userdata(array('id' => $pelogin->id));

                  if($pelogin ->type == 0)
                  {
                    // here goes  the admin data 
                     redirect('login/admin');
                  }

                  else{
                           //call here usuario method which has user data who logged in like
                             redirect('login/usuario');
                          // OR
                          // Call some method which has user data in $records and 
                      }
                }
            redirect('login/index');
        }
    }



            public function admin (){

                  $data['records']=$this->m_login->getDetails();
                  $this->load->view('admin',$data);
                }


            public function usuario(){

                    $data['records']=$this->m_login->getDetails();
                    $this->load->view('usuario',$data);
                }

模型文件(&#34; m_login&#34;) - 带有查询!

    <?php

        class m_login extends CI_Model{

            public function proceso_login($user,$pass){

                $this->db->where('username', $user);
                $this->db->where('password', $pass);
                return $this->db->get('usuarios')->row();

            }



            public function getDetails()
                {
                    $st=$this->db->SELECT('cursadas.*, usuarios.name as usuarios, materias.name as materias_name')->from('cursadas')
                        ->join('usuarios','usuarios.id=cursadas.user_id')
                        ->join('materias','materias.id=cursadas.subject_id')
                        ->WHERE('cursadas.user_id=',$this->session->userdata['id'])
                        ->get()->result_array();
                    return $st[0]; 
                }



}
?>

1 个答案:

答案 0 :(得分:0)

更改查询:

$st = $this->db->SELECT('cursadas.date as date, cursadas.grade as grade, usuarios.username as user, materias.name as subject')->from('cursadas')
                    ->join('usuarios','usuarios.id=cursadas.user_id')
                    ->join('materias','materias.id=cursadas.subject_id')
                    ->WHERE('cursadas.user_id=',$this->session->userdata('id'))
                    ->get()->result_array();
                return $st; 

请检查数据库字段类型使用datetime for date int for id和所需

在视图中:

<tbody>
<?php

if (count($records) > 0 && $records != false) {
    $id = 1;
    foreach($records as $record) {

        echo "<tr>
                  <td>".$id."</td>
                  <td>".$record['user']."</td>
                  <td>".$record['subject']."</td>
                  <td>".$record['grade']."</td>
                  <td>".$record['date']."</td>
              </tr>";
       $id++;
    }

   }
?>