无法在Codeigniter PHP中获取数据
在Codeigniter PHP中开发测验,无法获取其显示为这样的数据,每个显示为这样的问题的字段名为“quizID”,“question” “quizID”?>。“问题”?> 测验模型文件代码无法在Codeigniter PHP中获取数据,php,mysql,database,codeigniter,Php,Mysql,Database,Codeigniter,在Codeigniter PHP中开发测验,无法获取其显示为这样的数据,每个显示为这样的问题的字段名为“quizID”,“question” “quizID”?>。“问题”?> 测验模型文件代码 <?php if ( ! defined('BASEPATH')) exit('No direct script access allowed'); class quizmodel extends CI_Model { public function getQuestions() {
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class quizmodel extends CI_Model {
public function getQuestions()
{
$this->db->select("quizID, question, choice1, choice2, choice3,
answer");
$this->db->from("QuizQuestion");
$query = $this->db->get();
return $query->result();
$num_data_returned = $query->num_rows;
if ($num_data_returned < 1) {
echo "There is no data in the database";
exit();
}
}
}
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Questions extends CI_Controller {
function __construct() {
parent::__construct();
$this->load->database();
}
public function quizdisplay()
{
$this->load->model('quizmodel');
$this->data['questions'] = $this->quizmodel->getQuestions();
$this->load->view('play_quiz', $this->data);
}
public function resultdisplay()
{
$this->data['checks'] = array(
'ques1' => $this->input->post('quizid1'),
'ques2' => $this->input->post('quizid2'),
'ques3' => $this->input->post('quizid3'),
'ques4' => $this->input->post('quizid4'),
'ques5' => $this->input->post('quizid5'),
'ques6' => $this->input->post('quizid6'),
'ques7' => $this->input->post('quizid7'),
'ques8' => $this->input->post('quizid8'),
'ques9' => $this->input->post('quizid9'),
'ques10' => $this->input->post('quizid10'),
);
$this->load->model('quizmodel');
$this->data['results'] = $this->quizmodel->getQuestions();
$this->load->view('result_display', $this->data);
}
}
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Play Quiz</title>
</head>
<body>
<div id="container">
<h1>Play the Quiz!</h1>
<form method="post" action="<?php echo base_url();?
>index.php/Questions/resultdisplay">
<?php foreach($questions as $row) { ?>
<?php $ans_array = array($row->choice1, $row->choice2, $row->choice3, $row-
>answer);
shuffle($ans_array); ?>
<p><?=$row->quizID?>.<?=$row->question?></p>
<input type="radio" name="quizid<?=$row->quizID?>" value="<?=$ans_array[0]?
>" required> <?=$ans_array[0]?><br>
<input type="radio" name="quizid<?=$row->quizID?>" value="<?=$ans_array[1]?
>"> <?=$ans_array[1]?><br>
<input type="radio" name="quizid<?=$row->quizID?>" value="<?=$ans_array[2]?
>"> <?=$ans_array[2]?><br>
<input type="radio" name="quizid<?=$row->quizID?>" value="<?=$ans_array[3]?
>"> <?=$ans_array[3]?><br>
<?php } ?>
<br><br>
<input type="submit" value="Submit!">
</form>
</div>
</body>
</html>
控制器有一个错误:
而不是$this->data['questions']=$this->quizmodel->getQuestions()代码>
它应该说data['questions']=$this->quizmodel->getQuestions()代码>
这同样适用于:$this->data['results']=$this->quizmodel->getQuestions()代码>
应该是:data['results']=$this->quizmodel->getQuestions()代码>
$this
是一个singleton Codeigniter实例,请阅读更多:
模型也有一个错误,类声明必须是大写,请阅读更多
您可能还注意到,在模型中,return$query->result()之后代码>剩余的代码无法访问,您可能希望将其更改为:
$num_data_returned = $query->num_rows();
if ($num_data_returned < 1) {
echo "There is no data in the database";
exit();
}else{
return $query->result();
}
$num_data_returned=$query->num_rows();
如果($num\u data\u返回值<1){
echo“数据库中没有数据”;
退出();
}否则{
返回$query->result();
}
像这样更改您的型号,然后再试一次
class quizmodel extends CI_Model {
public function getQuestions()
{
$this->db->select("quizID, question, choice1, choice2, choice3,
answer");
$this->db->from("QuizQuestion");
$query = $this->db->get();
$result = $query->result_array();
$num_data = $query->num_rows();
if ($num_data ==0) {
return false;
}else{
return $result;
}
}
}
类quizmodel扩展了CI_模型{
public function getQuestions()
{
$this->db->select("quizID, question, choice1, choice2, choice3,
answer");
$this->db->from("QuizQuestion");
$num_data = $this->db->get()->num_rows();
if ($num_data == 0) {
return false;
}else{
return true;
}
}
}
显示错误无法修改标题信息数据['questions']=$this->quizmodel->getQuestions();在此行中并分析错误:语法错误,意外请参见我的编辑:类名的第一个字母必须大写,其余部分必须小写。请确保您的类扩展了基本模型类。立即在此文件上显示错误
public function getQuestions()
{
$this->db->select("quizID, question, choice1, choice2, choice3,
answer");
$this->db->from("QuizQuestion");
$num_data = $this->db->get()->num_rows();
if ($num_data == 0) {
return false;
}else{
return true;
}
}
}