codeigniter复选框值

Edv*_*tis 1 php checkbox codeigniter

大家好我刚开始使用codeigniter和php.我正在制作一个简单的调查类型网站惠特复选框,问题将是多项选择,如果选中复选框,结果将存储在数据库中.我的问题是我将如何做到这一点.我的表格和感谢每个人提前帮助.

视图

<?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>
Run Code Online (Sandbox Code Playgroud)

调节器

<?php
    class Survay extends CI_Model{

        function dosurvay($arrData){

            $this->db->select('QID, Question, qA, qB, qC');
            $this->db->from('tblquestions');
            $this->db->where('Question', $arrData['Question']);
            $this->db->where('qA', $arrData['qA']);
            $this->db->where('qB', $arrData['qB']);
            $this->db->where('qC', $arrData['qC']);
            $this -> db -> limit(1);

            $query = $this -> db -> get();

            if($query -> num_rows() == 1)
            {
                return $query->result();
            }
            else
            {
                return false;
            }
        }
    }
?>
Run Code Online (Sandbox Code Playgroud)

模型

<?php
class Survaycontroller extends CI_Controller{
   // 'QID, Question, qA, qB, qC'

    function index()
    {

            $arrData = array();
            $arrData["qA"] = $this->input->post("qA");
            $arrData["qB"] = $this->input->post("qB");
            $arrData["qC"] = $this->input->post("qC");
            $arrData["Question"] = $this->input->post("Question");

            $this->load->model('survay');

            $survay_data = $this->survay->dosurvay($arrData);

            $viewData['survay_data'] = $survay_data;

            $this->load->view('survay_view', $viewData);
    }

}
?>
Run Code Online (Sandbox Code Playgroud)

Ste*_*sen 5

这应该做你想要的.基于我们的Stackover,我们已经为您提供了答案.您的问题不如您在聊天中提出的要求那么清晰.在下面找到可以解决您的问题的代码

COIDIGNITER的基本用法:

我正在为您提供此代码,以减少我们的评论量.肯定你对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;
    }
}

}
?>
Run Code Online (Sandbox Code Playgroud)

第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);
    }

}
?>
Run Code Online (Sandbox Code Playgroud)

第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; ?>
Run Code Online (Sandbox Code Playgroud)

测试它:

http://localhost/Surva/index.php/survaycontroller/index/2
Run Code Online (Sandbox Code Playgroud)

检索问题2

现在这肯定会起作用.替换你已经拥有的一切.让我知道这些新代码是否能更有效地取代我上面的代码.