Jack Maessen Jack Maessen - 3 months ago 6
PHP Question

How to create an efficient form with several radio inputs

For a voting system I have a form with several radio inputs and values.
A snippet of the form looks like this:

<tr>
<td class="quickpollheader"><?php echo $quickpolloption1; ?></td>
<td class="quickpollradio"><input class="quickpollradio" type="radio" name="radiovote" value="1"></td>
</tr>
<tr>
<td class="quickpollheader"><?php echo $quickpolloption2; ?></td>
<td class="quickpollradio"><input class="quickpollradio" type="radio" name="radiovote" value="2"></td>
</tr>

<?php if($quickpolloptions_count >= 3) {
?>
<tr>
<td class="quickpollheader"><?php echo $quickpolloption3; ?></td>
<td class="quickpollradio"><input class="quickpollradio" type="radio" name="radiovote" value="3"></td>
</tr>
<?php
}
if($quickpolloptions_count >= 4) {
?>
<tr>
<td class="quickpollheader"><?php echo $quickpolloption4; ?></td>
<td class="quickpollradio"><input class="quickpollradio" type="radio" name="radiovote" value="4"></td>
</tr>
<?php
}
and so on...till 10


The admin can set the number of options in
settings.php
which looks like this:

// SETTINGS
$quickpolltype = "radio"; // set to "radio" or "checkbox"; radio is one choice, checkbox is multiple choices
$quickpollheader = "Wat is je favoriete browser?"; // the question or header

$quickpolloptions_count = 2; // edit the number of options you want; it starts from option 1 below

/*edit below the content of options between " and "
if number above = 2; only the first 5 options will be shown*/
$quickpolloption1 = "Mozilla:"; // option 1
$quickpolloption2 = "Crome:"; // option 2
$quickpolloption3 = "Opera:"; // option 3
$quickpolloption4 = "IE:"; // option 4
$quickpolloption5 = "Safari:"; // option 5
and so on till 10...


Data will be stored in a flat file and to calculate i use this piece of code:

//getting content of textfile
$filename = "data/vote_result.txt";
$content = file($filename);

//putting content in array
$array = explode("||", $content[0]);

$value0 = $array[0];
$value1 = $array[1];
$value2 = $array[2];
$value3 = $array[3];
$value4 = $array[4];
$value5 = $array[5];
....till 10


and

/* RADIO */
if ($_POST['view'] == 0) {
$value0 = 0;
}
if ($_POST['radiovote'] == 1) {
$value1 = $value1 + 1;
}
if ($_POST['radiovote'] == 2) {
$value2 = $value2 + 1;
}
if ($_POST['radiovote'] == 3) {
$value3 = $value3 + 1;
}
if ($_POST['radiovote'] == 4) {
$value4 = $value4 + 1;
}
if ($_POST['radiovote'] == 5) {
$value5 = $value5 + 1;
}
......
//inserting votes into the txt file
$addvote = $value0."||".$value1."||".$value2."||".$value3."||".$value4."||".$value5."||".$value6."||".$value7."||".$value8."||".$value9."||".$value10;
$totalvalues = $value1+$value2+$value3+$value4+$value5+$value6+$value7+$value8+$value9+$value10;
$total1 = (100*round($value1/($totalvalues),2));
$total2 = (100*round($value2/($totalvalues),2));
$total3 = (100*round($value3/($totalvalues),2));
$total4 = (100*round($value4/($totalvalues),2));
$total5 = (100*round($value5/($totalvalues),2));
....and so on


At least, here is where the output is generated:

<tr>
<td class="quickpollheader"><?php echo $quickpolloption1; ?></td>

<td class="quickpollgraphic"><div class="progresspoll">
<div class="percentpollgraph" style="width: <?php echo $total1; ?>%"></div>
</div>
</td>
<td class="quickpollpercent"><div class="percentpoll"><?php echo $total1; ?></div></td>
<td class="quickpollcount"><div class="count"><?php echo $value1; ?></div></td>
</tr>

<tr>
<td class="quickpollheader"><?php echo $quickpolloption2; ?></td>

<td class="quickpollgraphic"><div class="progresspoll">
<div class="percentpollgraph" style="width: <?php echo $total2; ?>%"></div>
</div>
</td>
<td class="quickpollpercent"><div class="percentpoll"><?php echo $total2; ?></div></td>
<td class="quickpollcount"><div class="count"><?php echo $value2; ?></div></td>
</tr>
<?php if($quickpolloptions_count >= 3) {
?>
<tr>
<td class="quickpollheader"><?php echo $quickpolloption3; ?></td>

<td class="quickpollgraphic"><div class="progresspoll">
<div class="percentpollgraph" style="width: <?php echo $total3; ?>%"></div>
</div>
</td>
<td class="quickpollpercent"><div class="percentpoll"><?php echo $total3; ?></div></td>
<td class="quickpollcount"><div class="count"><?php echo $value3; ?></div></td>
</tr>
<?php
}
if($quickpolloptions_count >= 4) {
?>
.... so on...


For this form I gave the admin the opportunity to create 10 options for the form. How can I make this more efficient so that it's easy to create 50 options without making a very long form and code for calculating as well as the output?

Answer

If you use an array to specify your options in settings, this can be done more easily.

$quickpolloptions = ['Mozilla', 'Chrome', 'Opera', 'IE', 'Safari'];

This will allow you to output your options using a foreach loop.

<form method="post">
    <?php foreach ($quickpolloptions as $key => $value) {
        echo "<label>$value</label>";
        echo "<input type='radio' name='radiovote' value='$key'><br>"
    }?>
    <input type="submit" value="Submit">
</form>

Then get your results:

$result_file = 'vote_result.txt';
if (file_exists($result_file)) {
    $results = explode(',', file_get_contents('vote_result.txt'));
} else {
    // start with zeros if you don't have a file yet
    $results = array_fill(0, count($quickpolloptions), 0);
}

If another vote was posted, update them:

if (isset($_POST['radiovote'])) {
    $results[$_POST['radiovote']]++;
    file_put_contents('vote_result.txt', implode(',', $results));
}

And display them:

$total = array_sum($results);
foreach (array_combine($quickpolloptions, $results) as $key => $count) {
    $percent = 100*round($count/($total),2);
    echo "$key: $percent% ($count)<br>";
}

I omitted most of the HTML/CSS to keep it brief and to the point. Hopefully this will show the general idea.