如何为查询的每一行使用单选按钮

时间:2015-03-22 10:16:59

标签: jquery codeigniter radio

我的查询的每一行都有单选按钮。我不知道如何创建他们的名字,因此对于查询的每一行,都可以检查单选按钮。现在我只能检查所有的一个收音机,但不仅仅是每个查询行。 我正在使用Codeigniter。这是我的观点:



?php
echo validation_errors();

foreach ($question as $row)
{
    echo form_open();
    echo "<tr><td>";
    echo "$row->question";
    echo "</td></tr>";
    echo "<tr><td>";
    
    $data=array(
        'name' => 'answer',
        'value' => '1'
    );
    
    echo form_radio($data);
     $data=array(
        'name' => 'answer',
        'value' => '2'
    );
    
    echo form_radio($data);
    $data=array(
        'name' => 'answer',
        'value' => '3'
    );
    
    echo form_radio($data);
    echo "</td></tr>";
    echo form_submit($data);

    echo form_close();
&#13;
&#13;
&#13;

我在模型中的查询返回此结果:

&#13;
&#13;
public function survey_show() {
       
        $this->db->select('question_id, question');
        $this->db->from('questions');
        $result=$this->db->get();
            return $result->result();
    }
&#13;
&#13;
&#13;

&#13;
&#13;
public function survey_fill() 
    {
        if (null !==($this->input->post('submit'))) {
          $date = new DateTime("now"); 
        
        $data = array(
            'user_id'=>$this->session->userdata['user_id'],
            'question_id'=>$this->input->post('question_id'),  
             'answer'=>$this->input->post('answer_1'),               
            'created_at'=>$date->format('Y-m-d H:i:s')
        );

        $this->db->insert('survey_answers', $data);
&#13;
&#13;
&#13;

1 个答案:

答案 0 :(得分:2)

我不知道您的HTML是否正常。 但这将解决您的问题

foreach ($question as $key=>$row)
    {
        echo form_open();
        echo "<tr><td>";
        echo "$row->question";
        echo "</td></tr>";
        echo "<tr><td>";

        $data=array(

            'name' => 'answer['.$key.']',//or this 'name' => 'answer_'.$key,
            'value' => '1'
        );

        echo form_radio($data);
        $data=array(
            'name' => 'answer['.$key.']',
            'value' => '2'
        );

        echo form_radio($data);
        $data=array(
            'name' => 'answer['.$key.']',
            'value' => '3'
        );

        echo form_radio($data);

但请记住,对所有'name' => 'answer['.$key.']','name' => 'answer_'.$key,使用其中之一并不要将两者合并。