Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/246.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/webpack/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php codeigniter复选框值_Php_Codeigniter_Checkbox - Fatal编程技术网

Php codeigniter复选框值

Php codeigniter复选框值,php,codeigniter,checkbox,Php,Codeigniter,Checkbox,大家好,我刚开始使用codeigniter和php。我正在做一个简单的调查类型的网站与复选框的问题将是多项选择,结果将存储在数据库中,如果一个复选框被选中。我的问题是我该怎么做。这是我的表格,谢谢大家的帮助 查看 <?php foreach($survay_data as $survay):?> <ul> <li><h1><?php echo $survay->Question;?></h1>

大家好,我刚开始使用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>


控制器

<?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;
            }
        }
    }
?>

型号

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

}
?>

”; } } ”; } }
这应该是您想要的。根据我们在Stackover上的聊天,已经为您提供了答案。你的问题没有你在聊天中提出的要求那么清楚。在下面查找可以解决问题的代码

CODEIGNITER的基本用途: 我提供这些代码只是为了减少我们的评论数量。 你肯定对Codeigniter很陌生。我只能尽力帮忙

步骤1:数据库

创建数据库表“tblquestions”。字段应为QID、qA、qB和qC。如果你有那么多记录,就用多达43条的记录填充字段。只要5张唱片就行了

步骤2:模型

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

}
?>
检索问题2


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

这应该可以满足您的需要。根据我们在Stackover上的聊天,已经为您提供了答案。你的问题没有你在聊天中提出的要求那么清楚。在下面查找可以解决问题的代码

CODEIGNITER的基本用途: 我提供这些代码只是为了减少我们的评论数量。 你肯定对Codeigniter很陌生。我只能尽力帮忙

步骤1:数据库

创建数据库表“tblquestions”。字段应为QID、qA、qB和qC。如果你有那么多记录,就用多达43条的记录填充字段。只要5张唱片就行了

步骤2:模型

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

}
?>
检索问题2



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

我们可以看看您在控制器或模型中也尝试了什么吗?请阅读这些手册:和。我编辑了我的帖子,添加了控制器和模型代码。我确实看过codeigniter的用户指南,它帮助我解决了这个项目的tru maust,但我不知道如何返回复选框值。这取决于问题所在。如果你想做一个选择,我怀疑你将如何得出一个结果,所有的人只要使用问题id选择问题。如果你想将答案插入数据库,请创建一个insert语句。问题在于你的模型和那些奇怪的方括号。是否要插入或选择问题?在下面的回答中,我对如何使用数组插入进行了评论。看看我想要的帮助是否是将答案插入到数据库表中。完成后,我提供了一个新的答案,可以选择并插入到数据库中。测试一下。我还没有时间,但这应该会让你走上正轨。我们可以看看你在控制器或模型中也尝试了什么吗?请阅读这些手册:和。我编辑了我的帖子,添加了控制器和模型代码。我确实看过codeigniter的用户指南,它帮助我解决了这个项目的tru maust,但我不知道如何返回复选框值。这取决于问题所在。如果你想做一个选择,我怀疑你将如何得出一个结果,所有的人只要使用问题id选择问题。如果你想将答案插入数据库,请创建一个insert语句。问题在于你的模型和那些奇怪的方括号。是否要插入或选择问题?在下面的回答中,我对如何使用数组插入进行了评论。看看我想要的帮助是否是将答案插入到数据库表中。完成后,我提供了一个新的答案,可以选择并插入到数据库中。测试一下。我没有时间,但这会让你走上正轨。谢谢你对我的帮助,我在这个问题上有点迷茫。谢谢你对我的帮助,我在这个问题上有点迷茫。谢谢你的帮助,斯图尔特。我尝试了你的代码,我发现了几个错误。以下是答案错误遇到PHP错误严重性:警告消息:缺少Survaycontroller::index()的参数1文件名:controllers/survaycontroller.php行号:7遇到php错误严重性:注意消息:未定义变量:QID文件名:controllers/survaycontroller.php行号:11遇到php错误严重性:警告消息:为foreach()提供的参数无效Filename:views/survay_view.php行号:1Okay,我希望您提供如下问题id:其中5是来自QID的问题id。这将用于筛选数据库中要显示的正确问题。请检查“我的编辑”,然后重试,前提是您知道数据库中存在一个id。这会帮你测试一些东西。这听起来可能很愚蠢,但我有点不知所措。你能再解释一下吗,让我使用以http://开头的url打开页面,该页面应显示问题以及数据库中与要显示的问题相对应的id。非常感谢您的帮助Steward我尝试了您的代码,发现了几个错误。以下是遇到的错误PHP错误严重性:警告消息:Survaycontroller::index()文件名:control缺少参数1
<?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;
    }
}

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

}
?>
<?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