2016-07-28 41 views
2

對於一個投票系統,我有一個有幾個無線電輸入和值的表格。 形式的一個片段是這樣的:如何創建一個有幾個無線電輸入的高效表格

<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 

管理員可以設置在settings.php的選項的數目,看起來像這樣:

// 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... 

的數據將被存儲在平面文件,並計算我用這段代碼:

//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 

/* 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 

至少,這裏是生成輸出:

<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... 

這種形式我給管理員的機會,爲表單創建10個選項。我怎樣才能使這個效率更高,這樣就很容易創建50個選項,而不需要用很長的表單和代碼來計算以及輸出呢?

回答

1

如果您使用數組來指定設置中的選項,則可以更輕鬆地完成此操作。

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

這將使用foreach循環讓你輸出你的選擇。

<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> 

然後得到結果:

$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 (isset($_POST['radiovote'])) { 
    $results[$_POST['radiovote']]++; 
    file_put_contents('vote_result.txt', implode(',', $results)); 
} 

並顯示出來:

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

我省略了大部分的HTML/CSS將其保持簡短並適用於poi NT。希望這會顯示出總體思路。

+0

工程太棒了!謝謝你的解釋 –

+0

不客氣。 –

相關問題