Hello Guys, I have a radio button has a different value i dont know how to get the sum of this. can you give me some ideas? or sample code?

thanks guys. this is my code.

<tr>
                                                <td width="500"><?php echo $behavioral; ?>
                                                </td>
                                                <td  align="center" >
                                                <div>
                                                <input type='radio' class='css-checkbox' id='1radio<?php echo ($id); ?>' name="<?php echo ($id); ?>" value="<?php echo ($count1); ?>"><label for="1radio<?php echo ($id); ?>" class="css-label">1</label>
                                                <input type='radio' class='css-checkbox' id='2radio<?php echo ($id); ?>' name="<?php echo ($id); ?>" value="<?php echo ($count2); ?>"><label for="2radio<?php echo ($id); ?>" class="css-label">2</label>
                                                <input type='radio' class='css-checkbox' id='3radio<?php echo ($id); ?>' name="<?php echo ($id); ?>" value="<?php echo ($count3); ?>" checked='checked'><label for="3radio<?php echo ($id); ?>" class="css-label">3</label>
                                                <input type='radio' class='css-checkbox' id='4radio<?php echo ($id); ?>' name="<?php echo ($id); ?>" value="<?php echo ($count4); ?>"><label for="4radio<?php echo ($id); ?>" class="css-label">4</label>
                                                <input type='radio' class='css-checkbox' id='5radio<?php echo ($id); ?>' name="<?php echo ($id); ?>" value="<?php echo ($count5); ?>" ><label for="5radio<?php echo ($id); ?>" class="css-label">5</label>
                                                </td>
                                                </div>
                                            </tr>

Recommended Answers

All 7 Replies

<?php
$id = 3; // e.g. value of checked
for($i=1; $i<=5; $i++){
$checked = ($id==$i?" checked=\"checked\"":""); ?>
    <input type="radio" class="css-checkbox" id="1radio<?=($id);?>"<?=$checked;?> name="<?=($id);?>" value="<?=$i;?>">
    <label for="1radio<?=($id);?>" class="css-label"><?=$i;?></label>
<?php } ?>

What is the value of your "$count" sequential or not?

for example. like this
First Option

<input type="radio" class="css-checkbox" id="1radio1" name="1" value="0.8">1
<input type="radio" class="css-checkbox" id="2radio1" name="1" value="1.6">2
<input type="radio" class="css-checkbox" id="3radio1" name="1" value="2.4" checked="checked">3
<input type="radio" class="css-checkbox" id="4radio1" name="1" value="3.2">4
<input type="radio" class="css-checkbox" id="5radio1" name="1" value="4">5

Second Option A

<input type="radio" class="css-checkbox" id="1radio6" name="6" value="2">1
<input type="radio" class="css-checkbox" id="2radio6" name="6" value="4">2
<input type="radio" class="css-checkbox" id="3radio6" name="6" value="6" checked="checked">3
<input type="radio" class="css-checkbox" id="4radio6" name="6" value="8">4
<input type="radio" class="css-checkbox" id="5radio6" name="6" value="10">5

Firts option B

<input type="radio" class="css-checkbox" id="1radio6" name="6" value="2">1
<input type="radio" class="css-checkbox" id="2radio6" name="6" value="4">2
<input type="radio" class="css-checkbox" id="3radio6" name="6" value="6" checked="checked">3
<input type="radio" class="css-checkbox" id="4radio6" name="6" value="8">4
<input type="radio" class="css-checkbox" id="5radio6" name="6" value="10">5

Second Option B

<input type="radio" class="css-checkbox" id="1radio6" name="6" value="2">1
<input type="radio" class="css-checkbox" id="2radio6" name="6" value="4">2
<input type="radio" class="css-checkbox" id="3radio6" name="6" value="6" checked="checked">3
<input type="radio" class="css-checkbox" id="4radio6" name="6" value="8">4
<input type="radio" class="css-checkbox" id="5radio6" name="6" value="10">5

Output. like this
First Option A: 0.8
First Option B:4

Second Option A: 4
Second Option B:8

Overall Total : 16.88

But in My Code. I dont know to to get the SUm.. HElp me please.

EDIT----
In my Radio Name i used by id of each question. like this <?php echo $id; ?>

Use checkbox instead of radio
Something like this:

<?php
$array = array( array(0.8, 1.2, 2.4, 3.2, 4), array(2, 4, 6, 8, 10) );
echo "<form action=\"".$_SERVER['PHP_SELF']."\" method=\"post\">";
for($a=1; $a<=5; $a++){
    $arr = ($a>1?$array[1]:$array[0]);
    foreach($arr as $key => $val){
        echo "
        <input type=\"checkbox\" id=\"ch_".$a."_".$key."\" name=\"ch_".$a."[]\" value=\"".$val."\" />
        <label for=\"ch_".$a."_".$key."\">".$val."</label>";
        }
    echo "<br/>";
    }
echo "<input type=\"submit\" name=\"submit\" value=\"submit\" /></form>";

if(isset($_POST['submit'])){
$total = 0;
for($a=1; $a<=5; $a++){
    if(isset($_POST['ch_'.$a])){
        $cur = array_sum($_POST['ch_'.$a]);
        echo $a."-sum = ".$cur."<br/>";
        $total += $cur;
        }
    }
echo "Total: ".$total;
}
?>
<?php
$array = array( array(0.8, 1.2, 2.4, 3.2, 4), array(2, 4, 6, 8, 10) );
echo "<form action=\"".$_SERVER['PHP_SELF']."\" method=\"post\">";
for($a=1; $a<=5; $a++){
    $arr = ($a>1?$array[1]:$array[0]);
    foreach($arr as $key => $val){
    $checked = ((isset($_POST['radiobutton_'.$a])&&$_POST['radiobutton_'.$a]==$val)?" checked=\"checked\"":"");
    echo "
    <input type=\"radio\" id=\"ch_".$a."_".$key."\"".$checked." name=\"radiobutton_".$a."\" value=\"".$val."\" />
        <label for=\"ch_".$a."_".$key."\">".($key+1)."</label>";
        }
    echo "<br/>";
    }
echo "<input type=\"submit\" name=\"submit\" value=\"submit\" /></form>";

if(isset($_POST['submit'])){
    $total = 0;
    for($a=1; $a<=5; $a++){
        if(isset($_POST['radiobutton_'.$a])){
            $cur = $_POST['radiobutton_'.$a];
            echo $a."-sum = ".$cur."<br/>";
            $total += $cur;
            }
        }
    echo "Total: ".$total;
    }
?>

Let me Try Your Code Sir, Ill Update you later for what will happen.. thanks sir.

And by the way ID and NAME tokens must begin with a letter ([A-Za-z]) and may be followed by any number of letters, digits ([0-9]), hyphens ("-"), underscores ("_"), colons (":"), and periods (".").
http://www.w3.org/TR/html4/types.html

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.