The error "Undefined array key 0" occurs when the $data array is empty and the code tries to access its first element. To fix this, you can add a check to make sure that $data is not empty before trying to access its elements in the for loop. Here's the updated code:
<?php include('api.php');?>
<div class="table-list">
<?php
if (empty($data)) {
// handle empty data
} else {
$valuesPerPage = 8;
$totalPages = ceil(count($data) / $valuesPerPage);
if (isset($_GET['page'])) {
$currentPage = $_GET['page'];
} else {
$currentPage = 1;
}
$startIndex = ($currentPage - 1) * $valuesPerPage;
$endIndex = $startIndex + $valuesPerPage;
$count = ($currentPage - 1) * $valuesPerPage + 1;
if (!empty($data)) {
for ($i = $startIndex; $i < $endIndex && $i < count($data); $i++) {
$game = $data[$i];
?>
<div class="row list-item">
<div class="col-4">
<h4 class="mb-0 font-d-bld text-white"><?php echo $count++; ?>. <?php echo $game['name']; ?></h4>
</div>
<div class="col-2">
<p class="mb-0 font-d-reg text-white"><?php echo $game['bet_size']; ?></p>
</div>
<div class="col-2">
<p class="mb-0 font-d-reg text-white"><?php echo $game['multiplier']; ?></p>
</div>
<div class="col-2">
<p class="mb-0 font-d-reg text-white"><?php echo $game['payout']; ?></p>
</div>
</div>
<?php
}
}
}
?>
<div class="table-pagination-btn d-flex justify-content-between">
<div class="left-btn">
<?php if ($currentPage > 1) { ?>
<a href="javascript:void(0)" onclick="loadData(<?php echo $currentPage - 1; ?>)"><i class="fa-solid fa-angle-left"></i></a>
<?php } ?>
</div>
<div class="pagination-number">
<ul class="ps-0 mb-0 d-flex">
<?php for ($i = 1; $i <= $totalPages; $i++) { ?>
<li><a href="javascript:void(0)" class="font-d-bld <?php echo $i == $currentPage ? 'active' : ''; ?>" onclick="loadData(<?php echo $i; ?>)"><?php echo $i; ?></a></li>
<?php } ?>
</ul>
</div>
<div class="right-btn">
<?php if ($currentPage < $totalPages) { ?>
<a href="javascript:void(0)" onclick="loadData(<?php echo $currentPage + 1; ?>)"><i class="fa-solid fa-angle-right"></i></a>
<?php } ?>
</div>
</div>
</div>
In the updated code, we added an if statement to check if $data is not empty before entering the for loop. This will prevent the "Undefined array key 0" error from occurring.