Reputation: 311
I am doing an Employees Evaluation System, I am now at the evaluation sheet management.
To display the criteria to rate, I used do while loop [radio button were included to the loop]. Here's my code:
<fieldset>
<table border="1">
<tr><br />
<th width="700px">AREAS TO RATE</th>
<th width="100px">1</th>
<th width="100px">2</th>
<th width="100px">3</th>
<th width="100px">4</th>
</tr>
<?php
$rates="select * from tbl_torateareas;";
$raters=mysqli_query($con,$rates);
$raterows=mysqli_fetch_array($raters);?>
<?php do{?>
<tr>
<td><?php echo $raterows['torateareas'];?></td>
<th><input type="radio" name="rad" /></th>
<th><input type="radio" name="rad" /></th>
<th><input type="radio" name="rad" /></th>
<th><input type="radio" name="rad" /></th>
</tr>
<?php }while($raterows=mysqli_fetch_array($raters)); ?>
</table>
</fieldset>
To select one radio button, I named all the radio buttons with the same name [name is: rad]. When I did this, only one radio button is selected for the entire table. As in, for out of 10 rows in my table--only 1 radio button was selected which was supposedly, one radio button for each row. Hmmmmmmm.
Help with this please. Thanks!
Upvotes: 0
Views: 5142
Reputation: 311
This is the solution. Thanks for further elaboration sir Vladkras!
<input type="radio" name="rad[<?php echo $raterows['id'];?>]" />
Upvotes: 0
Reputation: 17218
@MarcoMura is right. E.g. create adittional variable $i=0, increment it each loop and add it to your names like rad0 in first row, rad1 in second, etc.
While @Pupil expanded my answer and asked to mark it as a solution, I know more usefull one. The idea is to use not incremental number, but id
(or maybe element_id
, or how this column is called in your DB) that is likely fetched from your DB too with select * from tbl_torateareas;
So the specific input will look like
<input type="radio" name="rad[<?php echo $raterows['id'];?>]" />
It gives your an advantage, that you will exactly know which record in you DB was rated, not just the serial number of row in your html table. This will help to avoid errors when table in DB could change before form submitted.
Another pieces of advice:
1.You can likely use <?=
instead of <?php echo
to make your code more readable if you have PHP 5.4+ or short_open_tag = yes
Click to look the echo() docs.
2. mixing td
and th
here is unreasonable, unless you make first cell th
because it's a heading, and all others td
, though it's still valid
3. your loop is exessive, you don't need to fetch result first, then to wait if it fetches in while()
in the end, just use common while() syntax
while($raterows=mysqli_fetch_array($raters)) {
// do all the stuff
}
instead of
$raterows=mysqli_fetch_array($raters);
do {
// stuff
}
while($raterows=mysqli_fetch_array($raters));
I know your question was alredy answered, but maybe this will help to make better code in future.
Upvotes: 2
Reputation: 23948
Add a counter in your loop.
You are assigning same name
to all radio buttons, that is why they are the same.
<?php do{
$i=0;
?>
<tr>
<td><?php echo $raterows['torateareas'];?></td>
<th><input type="radio" name="rad[<?php echo $i;?>]" /></th>
<th><input type="radio" name="rad[<?php echo $i;?>]" /></th>
<th><input type="radio" name="rad[<?php echo $i;?>]" /></th>
<th><input type="radio" name="rad[<?php echo $i;?>]" /></th>
</tr>
<?php
++$i
}while($raterows=mysqli_fetch_array($raters)); ?>
<tr>
<td><?php echo $raterows['torateareas'];?></td>
<th><input type="radio" name="rad[<?php echo $i;?>]" /></th>
And in your submit radio code, loop over the posted values.
Like:
<?php
if (! empty($_POST['rad'])) {
$j=0;
foreach ($_POST['rad'] as $rad) {
// Access your radio buttons code here.
++$j;
}
}
?>
Upvotes: 2