Link to home
Start Free TrialLog in
Avatar of nish darsh
nish darsh

asked on

incrementing radio button names and ids using php

Hi,

I am using Codeigniter.
For every questionname fetched from the database, I am trying to attach a few radio buttons with a fixed values and a text area for each questionname in the same page(it would look like a questionnaire). I am able to get the values from database and display with the radio buttons and textarea like how I want it.

But the problem is that in my view for every questionname, radio buttons with same name and id are getting attached and hence I am not able to choose buttons explicitly for each button. I tried using for loop to increment the values but that is not the right approach. It would be great if someone can help me with this.

Controller:

public function viewvariant($variantid){
 $this->load->model('variant_model');
    $data['results']=$this->variant_model->getvariant($variantid);
     $this->load->view('viewvariant_view',$data);    
}

Model:      
function getvariant($variantid)
      {
          
$this->db->select('questions.questionid,questions.questionname');
   $this->db->from('questions');
   $this->db->join('variant_question', 'questions.questionid = variant_question.questionid');
   $this->db->where('variant_question.variantid',$variantid);
   $records = $this->db->get('');
 return $records->result();
      }

View:

<div class="form-group">
                 
                 <label>Questions for that variant(the methods for the task gets loaded as radio buttons)</label>
                 
               <?php
             
foreach ($results as $row) {

    ?>    
   
    <input type="text" name="questions" id="questions" value="<?php $row->questionid; ?>"/><?php echo $row->questionname; ?><br>
      <div class="radio">
      <label style="font-weight:bold"><input type="radio" id="radio1" name="optradio">no</label>
      <label style="font-weight:bold"><input type="radio" id="radio1" name="optradio">probably no</label>
      <label style="font-weight:bold"><input type="radio" id="radio1" name="optradio">unknown</label>
       <label style="font-weight:bold"><input type="radio" id="radio1" name="optradio">probably yes</label>
      <label style="font-weight:bold"><input type="radio" id="radio1" name="optradio">yes</label>
    </div>
    <textarea name="question1_text" rows="3" cols="73"></textarea>
<?php } ?>
 </div>
Avatar of Ray Paseur
Ray Paseur
Flag of United States of America image

Please use var_export() and show us the contents of $results, thanks.

Also, this article may give you some ideas about how to design the HTML form.  It's about check boxes, not radio buttons, but the principles have a lot in common.
https://www.experts-exchange.com/articles/5450/Common-Sense-Examples-Using-Checkboxes-with-HTML-JavaScript-and-PHP.html
Avatar of nish darsh
nish darsh

ASKER

@Ray Paseur
This is what I got in $results after using var_export():

I have attached the screenshot of my result.
-screenshot.PNG
ASKER CERTIFIED SOLUTION
Avatar of Ray Paseur
Ray Paseur
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Avatar of Julian Hansen
Julian Hansen
Flag of South Africa image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial