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

About this Thread

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

Trending Topics

Online now

Members online
1,201
Guests online
11,713
Total visitors
12,914

Forum statistics

Threads
2,334,989
Posts
29,281,176
Members
1,135,767
Latest member
mark2203
Back
Top