Alex
Alex

Reputation: 11

Unique radio name in php while loop

Well, I've following Php code which generate a table with radio button so that user can select only one radio button in EVERY row. But to select only one radio button in Every row it's must be a unique name in EVERY row, Right ? But I can't get any idea how do i set a unique name in while loop. Can you guys give me any idea or solutions ?

Php code:

$action = htmlspecialchars($_SERVER['PHP_SELF'])."?class=$class_from";  
echo "<form method='post' action='$action' name='attendence'/>";
echo "<table width='100%' cellpadding='0' cellspacing='0' border='0'>";
echo "<tr>";
echo "<td class='tdhead' valign='top' width='200'><b>Student Name</b></td>";    
echo "<td class='tdhead' valign='top' width='250'><b>Roll No</b>
</td>";             
echo "<td class='tdhead' valign='top' width='250'><b>Class Name</b>
</td>";             
echo "<td class='tdhead' valign='top' width='200'><b>Present / Not present</b>
</td>";                 
echo "<td class='tdhead' valign='top' width='200'>Check All <input type= 'checkbox' 
onclick='checkAll(this)' /></td>";                  
echo "</tr>";


while($res2 = mysql_fetch_array($sql2))
{

$sname = inputvalid($res2['sname']);
$roll = inputvalid($res2['roll']);
$class = inputvalid($res2['class']);

echo "<tr>";                
echo "<td class='tdhead2' valign='top'>$sname</td>";
echo "<td class='tdhead2' valign='top'>$roll</td>";
echo "<td class='tdhead2' valign='top'>$class</td>";        
echo "<td class='tdhead2' valign='top'>
<input type='radio' name='ch[]' value='1' />&nbsp;<input type='radio' name='ch[]' 
value='1' />
</td>";                     
echo "<td class='tdhead2' valign='top'>&nbsp;</td>";                
echo "</tr>";               

}

echo "<tr>";                
echo "<td class='tdhead2'>&nbsp;</td>";
echo "<td class='tdhead2'>&nbsp;</td>";
echo "<td class='tdhead2'>&nbsp;</td>";             
echo "<td class='tdhead2'>&nbsp;</td>";
echo "<td class='tdhead2'><input type='submit' value='Record' name='Submit' 
class='submit' /></td>";                
echo "</tr>";               

echo "</table>";
echo "</form>";

Thanks for your help.

Update:

echo "<td class='tdhead2' valign='top'>";
echo '<input type="radio" name="ch'.$counter++.'[]" value="1">';
echo "&nbsp;";
echo '<input type="radio" name="ch'.$counter.'[]" value="0">';
echo "</td>";                       

Upvotes: 0

Views: 2302

Answers (4)

hassan gul
hassan gul

Reputation: 21

you should use student name as an unique array and put that value in the name="" and it will auto increment according to the name and you can only choose single option at single row. try this :

<input type='radio' name='<?php echo $res2['sname'];?>' value='0' />&nbsp;";
<input type='radio' name='<?php echo $res2['sname'];?>' value='1' />";

this solutio will work.because it work's for me in an attendance system.

Upvotes: 0

Jonathan Kuhn
Jonathan Kuhn

Reputation: 15301

Instead of making the name just ch[] you can make the name ch[SOME_UNIQUE_VALUE] for each row. Usually something like an id number is used. Perhaps $roll if that is unique to each student. That will also allow you to know which id the radio button pertains to. On form submit, you can foreach($_POST['ch'] as $id=>$value) and get the id out.

Edit:

Example code:

//start the counter variable
$counter = 1;
while($res2 = mysql_fetch_array($sql2))
{

    $sname = inputvalid($res2['sname']);
    $roll = inputvalid($res2['roll']);
    $class = inputvalid($res2['class']);

    echo "<tr>";
    echo "<td class='tdhead2' valign='top'>$sname</td>";
    echo "<td class='tdhead2' valign='top'>$roll</td>";
    echo "<td class='tdhead2' valign='top'>$class</td>";
    echo "<td class='tdhead2' valign='top'>";

    //echo each radio with the counter for this row
    echo "<input type='radio' name='ch_{$counter}[]' value='0' />&nbsp;";
    echo "<input type='radio' name='ch_{$counter}[]' value='1' />";

    echo "</td>";
    echo "<td class='tdhead2' valign='top'>&nbsp;</td>";
    echo "</tr>";

    //add one to the counter
    $counter++;
}

Please note, unless you do like I suggested above, you will not be able to know which student each radio group goes to. You can assume that the radio buttons on group row 1 belongs to the student in row 1, but you might not know which student is in row 1. Which is why I suggest you put a unique id that goes with each student with each radio button instead of a counter.

Upvotes: 0

w00d
w00d

Reputation: 5596

You can store a counter $count and increase it after each iteration. Or if you have some unique id from the database per row (for example the primary_key), you can use that instead.$id = $res['id']

Then concat that value to the name=ch".$id."...

Upvotes: 0

tonoslfx
tonoslfx

Reputation: 3442

you can set a variable counter:

$counter = 1;
while($res2 = mysql_fetch_array($sql2)){
  echo '<input type="radio" name="ch'.$counter++.'[]" value="'.$counter++.'">';
}

Upvotes: 2

Related Questions