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

How to use radio buttons for each row of a query

查询的每一行都有单选按钮。我不知道如何创建它们的名称,以便对于查询的每一行,都可以检查单选按钮。现在我只能检查所有无线电中的一个,但不仅针对每个查询行。 我正在使用代码点火器。这是我的观点:

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

我在模型中查询,returns这个结果是:

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

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

我不知道你的 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, 使用其中之一,不要将两者结合使用。