<br>
<a href="../coding/coding_main.php" title="Lihat Keuangan Hari ini"><button style="padding : 10px; width:100%; background-color:#546E7A;">CODING</button></a>
<br>
<br>
<?php
    include('../loginsession.php');
    error_reporting(E_ALL);
    $filename = basename(__FILE__);
    $save = mysqli_query($conn, "INSERT INTO novia_abc_user_logs (userid,filename) VALUES ('$userini_id','$filename')");
//-------------

// ambil halaman dari URL, default = 1
$page = isset($_GET['page']) ? (int)$_GET['page'] : 1;
$limit = 50;
$offset = ($page - 1) * $limit;

// ambil kata pencarian
$cari = isset($_GET['cari']) ? $_GET['cari'] : '';

// query data
$sql = "SELECT * FROM novia_abc_biodata 
        WHERE name LIKE '%$cari%' 
           OR mr LIKE '%$cari%' 
           OR phone_wa LIKE '%$cari%' 
           OR phone_home LIKE '%$cari%' 
           OR street_address LIKE '%$cari%' 
        ORDER BY mr DESC 
        LIMIT $offset, $limit";
$result = $conn->query($sql);

// tampilkan data
while ($row = $result->fetch_assoc()) {
    echo $row['mr'] . " - " . $row['name'] . "<br>";
}

// hitung total data
$sql_count = "SELECT COUNT(*) as total FROM novia_abc_biodata 
              WHERE name LIKE '%$cari%' 
                 OR mr LIKE '%$cari%' 
                 OR phone_wa LIKE '%$cari%' 
                 OR phone_home LIKE '%$cari%' 
                 OR street_address LIKE '%$cari%'";
$count_result = $conn->query($sql_count);
$total_rows = $count_result->fetch_assoc()['total'];

$total_pages = ceil($total_rows / $limit);

// navigasi halaman
echo "<div style='margin-top:20px;'>";

// tombol previous
if ($page > 1) {
    echo "<a href='?page=" . ($page - 1) . "&cari=$cari'>Previous</a> ";
}

// nomor halaman
for ($i = 1; $i <= $total_pages; $i++) {
    if ($i == $page) {
        echo "<strong>$i</strong> "; // halaman aktif ditebalkan
    } else {
        echo "<a href='?page=$i&cari=$cari'>$i</a> ";
    }
}

// tombol next
if ($page < $total_pages) {
    echo "<a href='?page=" . ($page + 1) . "&cari=$cari'>Next</a>";
}

echo "</div>";
?>
