🔒 Closed Help!

Status
Not open for further replies.
vien3211 Eto lods check mu You do not have permission to view the full content of this post. Log in or register now.

PHP:
<table style="border: 1px solid black;">
    <thead >
        <tr>
            <th>Precint</th>
            <th>Candidate A</th>
            <th>Candidate B</th>
            <th>Candidate C</th>
            <th>Candidate D</th>
        </tr>
    </thead>
    <tbody>
    <?php
        $results = array (
            array(192,147,186,114,267),
            array(48,90,12,21,13),
            array(206,312,121,408,382),
            array(37,21,38,39,29),
          );
        for ($col = 0; $col < 5; $col++) {
            echo "<tr><td>" . $col+1 . "</td>";
            for ($row = 0; $row < 4; $row++) {
                echo "<td>" . $results[$row][$col] . "</td>&nbsp;";
              }
            echo "</tr>";
        }
    ?>
    </tbody>
</table>
<br>
<table style="border: 1px solid black;">
    <thead>
        <tr>
            <th>Candidate</th>
            <th>Total</th>
            <th>Percentage</th>
        </tr>
    </thead>
    <tbody>
    <?php
    $totalCandidates = count($results);
    $candidates = array("A","B","C","D");
    $total = array();
    $percentage = array();
    $winner = null;
    for ($i=0; $i < $totalCandidates; $i++) {
        $total[$i] = array_sum($results[$i]);
    }
    
    $grandTotal = array_sum($total);
    for ($i=0; $i < $totalCandidates; $i++) {
        $percentage[$i] = round($total[$i] / $grandTotal * 100);
        echo "<tr><td>" . $candidates[$i] . "</td>";
        echo "<td>" . $total[$i] . "</td>";
        echo "<td>" . $percentage[$i] . "%</td></tr>";
        if ($percentage[$i] > 50 )
        $winner = $i;
    }
    ?>
    </tbody>
</table>
<?php
    echo "<br>" . "Candidate " . $candidates[$winner] . " with the " . $percentage[$winner] . "% total votes is the winner.";
?>
 
Status
Not open for further replies.

Similar threads

About this Thread

  • 4
    Replies
  • 309
    Views
  • 5
    Participants
Last reply from:
derksd

Online now

Members online
777
Guests online
794
Total visitors
1,571

Forum statistics

Threads
2,276,088
Posts
28,967,516
Members
1,231,116
Latest member
bratata
Back
Top