<?php
include 'config.php'; // koneksi database
date_default_timezone_set('Asia/Jakarta');
$now = date("H:i");

$sql = "SELECT * FROM lottery_results WHERE draw_date = CURDATE()";
$result = mysqli_query($conn, $sql);

while ($row = mysqli_fetch_assoc($result)) {
    $id = $row['id'];

    function generate_number() {
        return sprintf("%04d", rand(0, 9999));
    }

    if ($now == substr($row['generate_time_2nd'], 0, 5)) {
        $num = generate_number();
        mysqli_query($conn, "UPDATE lottery_results SET second_prize = '$num' WHERE id = $id");
    }

    if ($now == substr($row['generate_time_3rd'], 0, 5)) {
        $num = generate_number();
        mysqli_query($conn, "UPDATE lottery_results SET third_prize = '$num' WHERE id = $id");
    }

    if ($now == substr($row['generate_time_starter'], 0, 5)) {
        $num = generate_number();
        mysqli_query($conn, "UPDATE lottery_results SET starter_prize = '$num' WHERE id = $id");
    }

    if ($now == substr($row['generate_time_consolation'], 0, 5)) {
        $num = generate_number();
        mysqli_query($conn, "UPDATE lottery_results SET consolation_prize = '$num' WHERE id = $id");
    }
}
?>
