所以这是错误:
A PHP Error was encountered
Severity: Notice
Message: Undefined variable: idnum
Filename: models/model_teacher.php
Line Number: 8
这是我的控制者:
public function teacher(){
$this->load->model('model_teacher');
$data['result'] = $this->model_teacher->scoreboard();
$this->load->view('teacher/teacher', $data);
}
和我的模特:
class Model_teacher extends CI_Model {
public function scoreboard() {
$this->db->where('login_id', $this->input->post('idnum'));
$query = $this->db->query("SELECT * FROM teacher WHERE login_id = '".$idnum."'");
return $query->result();
}
}
和视图:
<?php
foreach ($result as $row) {
echo $row->login_id."<br>";
echo $row->lname."<br>";
}
?>
并且不知道这段代码有什么问题。请耐心等待我,我仍然是使用codeigniter的新手。多谢你们。
答案 0 :(得分:1)
更改如下:
在控制器
中 public function teacher(){
$this->load->model('model_teacher');
$idNum = $this->input->post('idnum');
$data['result'] = $this->model_teacher->scoreboard($idNum);
$this->load->view('teacher/teacher', $data);
}
在模型中
public function scoreboard($idNum) {
$this->db->where('login_id', $idNum);
$query = $this->db->query("SELECT * FROM teacher WHERE login_id = '".$idnum."'");
return $query->result();
}
答案 1 :(得分:0)
如果将它从控制器传递给模型
会更好$id = $this->input->post('idnum');
$data['result'] = $this->model_teacher->scoreboard($id);
和型号: -
public function scoreboard($id) {
//$this->db->where('login_id', $id);
$query = $this->db->query("SELECT * FROM teacher WHERE login_id = '".$id."'");
return $query->result();
}
此外,您还无法定义任何地方$idnum
,因此会出现通知
答案 2 :(得分:0)
你不能直接进入模型,你必须首先进入控制器 的控制器强>
public function teacher(){
$id = $this->input->post('idnum');
$this->load->model('model_teacher');
$data['result'] = $this->model_teacher->scoreboard($id);
$this->load->view('teacher/teacher', $data);
}
<强>模型强>`
public function scoreboard($idnum) {
$query = $this->db->query("SELECT * FROM teacher WHERE login_id = '".$idnum."'");
return $query->result();
}`
这是您可以使用的代码