faroskalin faroskalin - 2 months ago 8
PHP Question

Converting a PHP/HTML drop-down menu to radio buttons

Currently I have a loop of drop-down menus (loop is dependent on the results per page that I can define, you can see why I would use a loop), and I would like to turn the drop-down menus to radio buttons. Below is my code. Could you give me some pointers on how I could convert it?

if (isset($_POST['formSubmit'])){
$rating = $_POST['rating'];
$accountID = $_POST['accountID'];

$query = mysql_query("UPDATE Spreadsheet SET rating='$rating' WHERE accountID='$accountID'");
}

while ($row = mysql_fetch_array($query)){

?>

<form name ="rating" method ="POST" action ="" > <?

echo "<input type = 'hidden' name = 'accountID' value = '" . $row['accountID'] . "' >";

?>
<select name="rating">

<?

$values = array('0 - No rating','1 - Very Bad','2 - Bad','3 - Average','4 - Above Average');

for ($i =0; $i < count($values); $i++){
echo "<option value = \"$i\"";

if ($row['rating'] == $i) {
echo "selected=\"selected\"";
}

echo ">" . $values[$i] . "</option>";

}

?>

</select>
<input type ="Submit" name ="formSubmit" value ="Submit" />
</form>

Dev Dev
Answer
     $values = array('0 - No rating','1 - Very Bad','2 - Bad','3 - Average','4 - Above Average');   


    <form name ="rating" method ="POST" action ="" >
     <?
    echo "<input type = 'hidden' name = 'accountID' value = '" . $row['accountID'] . "' >";
     for($i =0; $i < count($values); $i++)
    {
    ?>
     <input type="radio" name="rbl" value="<?php echo $row['rating']; ?>" id="rbl_<?php echo $i; ?>" <? if($row['rating'] == $i) echo "checked='checked'"; ?>/>

<label for="rbl_<?php echo $i; ?>"><?php echo $value[$i]; ?></label>


     <? } ?>

            <input type ="Submit" name ="formSubmit" value ="Submit" />
            </form>
Comments