Não pode escolher mais do que 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.
 
 
 
 
 
 

111 linhas
2.8 KiB

<?php
/**
* Ambil data masing masing calon beserta suara yang didapatkan
* dengan format layaknya hasil Query Builder di Laravel. Seperti:
*
* Calon 1 => [
* nama => ...
* suara => [
* valid => ...
* tidak valid => ...
* ]
* ]
*/
require_once('../conn/db.php');
require_once('../utils/helper.php');
$result = [];
// Ambil profil calon
$queryCandidates = "SELECT
id_calon,
nama_calon,
photo,
id_status
FROM
tb_calon
ORDER BY photo";
$execCandidates = odbc_exec($koneksi, $queryCandidates);
// Looping untuk menyatukan setiap record menjadi sebuah Array
// while ($row = odbc_fetch_array($execCandidates)) {
// // Push id
// array_push($pluckedCandidateId, $row['id_calon']);
// // Push candidate profile
// array_push($candidates, $row);
// }
// $candidates = dbGet($execCandidates, function ($row) use (&$pluckedCandidateId) {
// array_push($pluckedCandidateId, $row['id_calon']);
// });
$candidates = dbGet($execCandidates);
$pluckedCandidateId = colPluck($candidates, 'id_calon');
// Ambil semua suara yang didapatkan
$stringifyCandidateId = implode("','", $pluckedCandidateId);
$queryVotes = "SELECT
b.id_calon,
COUNT(a.id_pemilu) AS count
FROM
(SELECT
MIN(a.id_pemilu) AS id_pemilu,
b.id_status,
MIN(a.create_at) as create_at
FROM
tb_pemilu a
JOIN
tb_calon b ON a.id_calon = b.id_calon
WHERE
a.id_status IN ('S9af54pY', 'KTU3vVnd')
GROUP BY
a.id_status,
nim_mhs,
b.id_status) a
JOIN tb_pemilu b
ON b.id_pemilu = a.id_pemilu
WHERE
b.id_calon IN ('$stringifyCandidateId')
GROUP BY
b.id_calon
ORDER BY
b.id_calon";
$execVotes = odbc_exec($koneksi, $queryVotes);
$votes = dbGet($execVotes);
$keyedVotes = colKeyBy($votes, 'id_calon');
// Pasang hasil vote ke setiap calon
foreach ($candidates as &$candidate) {
$candidate['votes'] = $keyedVotes[$candidate['id_calon']];
}
// Ambil sema
$semaTotalVotes = 0;
$sema = array_filter($candidates, function ($item) {
return $item['id_status'] === 'KTU3vVnd';
});
array_reduce($sema, function ($prev, $cur) use (&$semaTotalVotes) {
$prevVotes = (int) $prev['votes']['count'];
$curVotes = (int) $cur['votes']['count'];
$semaTotalVotes += $prevVotes + $curVotes;
});
// Ambil balma
$balmaTotalVotes = 0;
$balma = array_filter($candidates, function ($item) {
return $item['id_status'] === 'S9af54pY';
});
array_reduce($balma, function ($prev, $cur) use (&$balmaTotalVotes) {
$prevVotes = (int) $prev['votes']['count'];
$curVotes = (int) $cur['votes']['count'];
$balmaTotalVotes += $prevVotes + $curVotes;
});
dump($votes);