它在这里找到答案:https ://stackoverflow.com/a/15086172/827224
这应该做你想要的。根据我们在 Stackover 上的聊天,我们已经为您提供了答案。您的问题不像您在聊天中提出的要求那么清楚。在下面找到可以解决您的问题的代码
CODEIGNITER的基本使用:
我提供这些代码只是为了减少我们的评论数量。可以肯定的是,您对 Codeigniter 还是很陌生。我只能尽我所能提供帮助。
第 1 步:数据库
创建数据库表“tblquestions”。字段应为 QID、qA、qB 和 qC。如果你有那么多,用最多 43 条记录填充字段。尽可能少的 5 条记录。
第 2 步:模型
<?php
class Survay extends CI_Model {
function dosurvay($question_id = null) {
$this->db->select('QID, Question, qA, qB, qC');
$this->db->from('tblquestions');
if ($question_id) {
$this->db->where('QID', $question_id);
}
$this->db->limit(1);
$query = $this->db->get();
if ($query->num_rows() == 1) {
return $query->result();
} else {
return false;
}
}
function addsurvay($arrData) {
$this->db->insert('tblanswers', $arrData);
if ($this->db->affected_rows() > 0) {
return $this->db->insert_id();
} else {
return false;
}
}
}
?>
第 3 步:控制器
<?php
class Survaycontroller extends CI_Controller {
// 'QID, Question, qA, qB, qC'
function __construct() {
parent::__construct();
$this->load->model('survay');
}
function index() {
//This should select the survey question
$data = array();
$question_id = $this->uri->segment(3);
$data[survay_data] = $this->survay->dosurvay($question_id);
$this->load->view('survay_view', $data);
}
function addanswer() {
//The answer is submitted to this...
$arrData = array();
$userid = null;
if ($this->session->userdata("userid")) {
$userid = $this->session->userdata("userid");
}
if ($this->input->post()) {
$arrData["answerid"] = $this->input->post("QID");
$arrData["questionid"] = $this->input->post("qA");
if ($this->input->post("qA")) {
$arrData["answerA"] = $this->input->post("qA");
}
if ($this->input->post("qB")) {
$arrData["answerB"] = $this->input->post("qB");
}
if ($this->input->post("qC")) {
$arrData["answerC"] = $this->input->post("qC");
}
$arrData["userid"] = $userid;
}
$viewData[survay_data_id] = $this->survay->addsurvay($arrData); //Get the ID of the answer stored
$this->load->view('survay_view', $viewData);
}
}
?>
第 4 步:视图
<?php if(isset($survay_data)) : ?>
<form action="http://localhost/Surva/index.php/survaycontroller/addanswer/" name="myform" id="myform" method="post">
<?php foreach ($survay_data as $survay): ?>
<ul>
<li><h1><?php echo $survay->Question; ?></h1></li>
<li><?php echo $survay->qA; ?><input type="checkbox" name="qA" value="<?php echo $survay->qA; ?>"></li>
<li><?php echo $survay->qB; ?><input type="checkbox" name="qB" value="<?php echo $survay->qA; ?>"></li>
<li><?php echo $survay->qC; ?><input type="checkbox" name="qC" value="<?php echo $survay->qA; ?>"></li>
<li><input type="hidden" name="QID" value="<?php echo $survay->QID; ?>"></li>
<li><input type="submit" name="btn" value="Answer"></li>
</ul>
<?php endforeach; ?>
</form>
<?php endif; ?>
测试它:
http://localhost/Surva/index.php/survaycontroller/index/2
检索问题编号 2
现在这肯定会奏效。替换你已经拥有的一切。让我知道这些新代码集是否更有效地替换了我上面的代码集。