codeigniter复选框值

时间:2013-02-26 07:52:01

标签: php codeigniter checkbox

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

}
?>

2 个答案:

答案 0 :(得分: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;
    }
}

}
?>

第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

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

答案 1 :(得分:0)

 <?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="<?php echo $survay->qA; ?>"></li>
<li><?php echo $survay->qB; ?><input type="checkbox" name="q2[]" value="<?php echo $survay->qB; ?>"></li>
<li><?php echo $survay->qC; ?><input type="checkbox" name="q3[]" value="<?php echo $survay->qC; ?>"></li>
<?php endforeach; ?>
<input type="textarea" value='a' name="comment">
<br>
<input type="submit" value="Submit">
</ul>

您需要将检索到的数据作为复选框的值。在您的控制器上,您可以var_dump($this->input->post())查看已提交的数据,然后您可以根据需要对其进行操作。

扩展了一点点:

简而言之,您尝试将数据库中的值分配给值attr,当用户选中复选框时,该值将作为值发送。

在表单上是否在控制器中使用form_open()或标准<form>标记类型来接收您的数据。

function dosurvay($arrData){

    // you're get survay things here


    if($this->input->post())// will execute only if a post happens
    {
       echo '<pre>';
       print_r($this->input->post()); // just print out everything
       echo '</pre>';
    }
}