我正在尝试使用codeigniter编写一个简单的调查应用程序我有一个每个答案的复选框,我想将每个答案我存储到数据库的表格我想问的是我将如何存储所有这些信息在一个桌子上,我是编码世界的新手,一个简单的解决方案就可以做到。现在我所拥有的是这里设置的视图:
<html>
<body>
<?php form_open('survay'); ?>
<?php foreach($survay_data as $survay):?>
<ul>
<li><h1><?php echo $survay->Question; ?></h1></li>
<li><?php echo $survay->qA; ?><input type="checkbox" name = "q1" value = "qA"></li>
<li><?php echo $survay->qB; ?><input type="checkbox" name = "q2" value = "qB"></li>
<li><?php echo $survay->qC; ?><input type="checkbox" name = "q3" value = "qC"></li>
<?php endforeach; ?>
<input type="textarea" value='a' name="comment">
<br>
<input type="submit" value="Submit">
</ul>
</body>
</html>
答案 0 :(得分:1)
它在这里找到答案:https://stackoverflow.com/a/15086172/827224
这应该做你想要的。基于我们的Stackover,我们已经为您提供了答案。您的问题不如您在聊天中提出的要求那么清晰。在下面找到可以解决问题的代码
我正在为您提供此代码,以减少我们的评论量。 肯定你对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
现在这肯定会奏效。替换你已经拥有的一切。让我知道这些新代码是否能更有效地取代我上面的代码。