尝试使用 codeigniter 获取数据时出错
Error trying to fetch data using codeigniter
我得到了 3 table;我的 table 是“cursadas”、“usuarios”和“materias”
"cursadas" includes:(id, user_id[is the foreign key to the column "id" of the table "usuarios"], subject_id[is the foreign key to the column "id" of the table "materias"], grade, date)
"usuarios" includes:(id,username,name,lastname,password,type,status,date)
"materias" includes:(id, career_id, name, description, hours)
这是我最后的table“cursadas”(数据来自tables“materias”和“usuarios”)
看一看,我需要这样的东西:
我收到这个错误:
我认为我的查询有误,我不知道我应该怎么做才能完成这项工作: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>
我的控制器文件(“登录”):
<?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);
}
和模型文件("m_login")- 与查询!
<?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];
}
}
?>
更改查询:
$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 作为 date int 作为 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++;
}
}
?>
我得到了 3 table;我的 table 是“cursadas”、“usuarios”和“materias”
"cursadas" includes:(id, user_id[is the foreign key to the column "id" of the table "usuarios"], subject_id[is the foreign key to the column "id" of the table "materias"], grade, date)
"usuarios" includes:(id,username,name,lastname,password,type,status,date)
"materias" includes:(id, career_id, name, description, hours)
这是我最后的table“cursadas”(数据来自tables“materias”和“usuarios”)
看一看,我需要这样的东西:
我收到这个错误:
我认为我的查询有误,我不知道我应该怎么做才能完成这项工作: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>
我的控制器文件(“登录”):
<?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);
}
和模型文件("m_login")- 与查询!
<?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];
}
}
?>
更改查询:
$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 作为 date int 作为 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++;
}
}
?>