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('varian t_model');
$data['results']=$this->va riant_mode l->getvari ant($varia ntid);
$this->load->view('viewvar iant_view' ,$data);
}
Model:
function getvariant($variantid)
{
$this->db->select('questio ns.questio nid,questi ons.questi onname');
$this->db->from('questions ');
$this->db->join('variant_q uestion', 'questions.questionid = variant_question.questioni d');
$this->db->where('variant_ question.v ariantid', $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</l abel>
<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>
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('varian
$data['results']=$this->va
$this->load->view('viewvar
}
Model:
function getvariant($variantid)
{
$this->db->select('questio
$this->db->from('questions
$this->db->join('variant_q
$this->db->where('variant_
$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"><
<label style="font-weight:bold"><
<label style="font-weight:bold"><
<label style="font-weight:bold"><
<label style="font-weight:bold"><
</div>
<textarea name="question1_text" rows="3" cols="73"></textarea>
<?php } ?>
</div>
ASKER
@Ray Paseur
This is what I got in $results after using var_export():
I have attached the screenshot of my result.
-screenshot.PNG
This is what I got in $results after using var_export():
I have attached the screenshot of my result.
-screenshot.PNG
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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