Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

devkon98's avatar

How to make paginate in JS and PHP

Hello i have this code in html and php and i want to paginate the values, this is my code until now:

<div class="bounus-hunt-list">
               <div class="row">
                  <div class="col-lg-8 mb-md-5 mb-4 mb-lg-0">
                     <div class="list-table">
                        <div class="table-header">
                           <div class="row">
                              <div class="col-4">
                                 <h4 class="mb-0 font-d-bld text-white">Game</h4>
                              </div>
                              <div class="col-2">
                                 <h4 class="mb-0  font-d-bld text-white">Bet</h4>
                              </div>
                              <div class="col-3">
                                 <h4 class="mb-0  font-d-bld text-white">X</h4>
                              </div>
                              <div class="col-3">
                                 <h4 class="mb-0  font-d-bld text-white">Win</h4>
                              </div>
                           </div>
                        </div>
                        <div class="table-list">
                           <?php
                              $count =0;
                              foreach($values->bonuses as $value)
                              {  
                              	$count +=1;		
                              	if($count <= 7){
                              	?>
                           <div class="row list-item">
                              <div class="col-4">
                                 <h4 class="mb-0 font-d-bld text-white"><?php echo $count.")".$value->name;?></h4>
                              </div>
                              <div class="col-2">
                                 <h4 class="mb-0  font-d-bld text-white"><?php echo $value->bet_size;?></h4>
                              </div>
                              <div class="col-3">
                                 <h4 class="mb-0  font-d-bld text-white"><?php echo $value->multiplier;?></h4>
                              </div>
                              <div class="col-3">
                                 <h4 class="mb-0  font-d-bld text-white"><?php echo $value->payout;?></h4>
                              </div>
                           </div>
                           <?php
                              }
                              }
                              ?>
                        </div>
                        <div class="table-pagination-btn d-flex justify-content-between">
                           <div class="left-btn">
                              <a href="#"><i class="fa-solid fa-angle-left"></i></a>
                           </div>
                           <div class="pagination-number">
                              <ul class="ps-0 mb-0 d-flex">
                                 <li><a href="#" class="font-d-bld">1</a></li>
                                 <li><a href="#" class="font-d-bld">2</a></li>
                                 <li><a href="#" class="font-d-bld">3</a></li>
                              </ul>
                           </div>
                           <div class="right-btn"><a href="#"><i class="fa-solid fa-angle-right"></i></a></div>
                        </div>
                     </div>
                  </div>
                  <div class="col-lg-4">
                     <div class="guess-balance">
                        <h3 class="mb-0 font-d-bld text-white">GUESS THE END BALANCE (COMING SOON)</h3>
                        <div class="guess-end-balance row gx-0">
                           <div class="col-6">
                              <div class="your-guess">
                                 <p class="mb-0 font-d-bld">YOUR GUESS</p>
                                 <h4 class="mb-0 font-d-bld text-white">&dollar;-----</h4>
                              </div>
                           </div>
                           <div class="col-6">
                              <div class="end-balance">
                                 <p class="mb-0 font-d-bld">END BALANCE</p>
                                 <h4 class="mb-0 font-d-bld text-white">&dollar;-----</h4>
                              </div>
                           </div>
                        </div>
                        <div class="winner-name">
                           <div class="winner col-8 mx-auto">
                              <p class="mb-0 font-d-bld">WINNER</p>
                              <h4 class="mb-0 font-d-bld text-white">-----</h4>
                           </div>
                        </div>
                     </div>
                     <div class="win-multiplier">
                        <div class="highest-win">
                           <h3 class="mb-0 font-d-bld text-white">HIGHEST WIN</h3>
                           <div class="row">
                              <div class="col-6">
                                 <p class="mb-0 font-d-bld">GAME</p>
                                 <h4 id="slot-name" class="mb-0 font-d-bld text-white"></h4>
                              </div>
                              <div class="col-6">
                                 <p class="mb-0 font-d-bld">WIN</p>
                                 <h4 id="payout" class="mb-0 font-d-bld text-white"></h4>
                              </div>
                           </div>
                        </div>
                        <div class="highest-multiplier">
                           <h3 class="mb-0 font-d-bld text-white">HIGHEST MULTIPLIER</h3>
                           <div class="row">
                              <div class="col-6">
                                 <p class="mb-0 font-d-bld">GAME</p>
                                 <h4 id="slot-name1" class="mb-0 font-d-bld text-white"></h4>
                              </div>
                              <div class="col-6">
                                 <p class="mb-0 font-d-bld">X VALUE</p>
                                 <h4 id="multiplier" class="mb-0 font-d-bld text-white"></h4>
                              </div>
                           </div>
                        </div>
                     </div>
                  </div>
               </div>
            </div>
0 likes
1 reply
LaryAI's avatar
Level 58

To paginate the values, you can use a combination of JavaScript and PHP. Here's an example of how you can do it:

  1. First, you need to modify your PHP code to accept a page number and a limit. You can use the array_slice function to get a subset of the array based on the page number and limit. Here's an example:
$page = isset($_GET['page']) ? $_GET['page'] : 1;
$limit = 7;
$offset = ($page - 1) * $limit;
$bonuses = $values->bonuses;
$bonuses = array_slice($bonuses, $offset, $limit);
  1. Next, you need to modify your HTML code to display the paginated values. You can use a loop to iterate over the $bonuses array and display the values. Here's an example:
<div class="table-list">
    <?php foreach ($bonuses as $key => $value): ?>
        <div class="row list-item">
            <div class="col-4">
                <h4 class="mb-0 font-d-bld text-white"><?php echo $key + 1; ?>) <?php echo $value->name; ?></h4>
            </div>
            <div class="col-2">
                <h4 class="mb-0 font-d-bld text-white"><?php echo $value->bet_size; ?></h4>
            </div>
            <div class="col-3">
                <h4 class="mb-0 font-d-bld text-white"><?php echo $value->multiplier; ?></h4>
            </div>
            <div class="col-3">
                <h4 class="mb-0 font-d-bld text-white"><?php echo $value->payout; ?></h4>
            </div>
        </div>
    <?php endforeach; ?>
</div>
  1. Finally, you need to add pagination links to allow the user to navigate between pages. You can use JavaScript to handle the pagination links. Here's an example:
<div class="table-pagination-btn d-flex justify-content-between">
    <div class="left-btn">
        <a href="#" class="prev-page"><i class="fa-solid fa-angle-left"></i></a>
    </div>
    <div class="pagination-number">
        <ul class="ps-0 mb-0 d-flex">
            <?php for ($i = 1; $i <= ceil(count($values->bonuses) / $limit); $i++): ?>
                <li><a href="#" class="page-link <?php echo $i == $page ? 'active' : ''; ?>"><?php echo $i; ?></a></li>
            <?php endfor; ?>
        </ul>
    </div>
    <div class="right-btn">
        <a href="#" class="next-page"><i class="fa-solid fa-angle-right"></i></a>
    </div>
</div>

<script>
    $(function() {
        $('.page-link').on('click', function(e) {
            e.preventDefault();
            var page = $(this).text();
            window.location.href = '?page=' + page;
        });

        $('.prev-page').on('click', function(e) {
            e.preventDefault();
            var page = <?php echo $page - 1; ?>;
            window.location.href = '?page=' + page;
        });

        $('.next-page').on('click', function(e) {
            e.preventDefault();
            var page = <?php echo $page + 1; ?>;
            window.location.href = '?page=' + page;
        });
    });
</script>

This code adds pagination links to the page and uses JavaScript to handle the clicks. When a user clicks on a page link, the page number is added to the URL and the page is reloaded with the new page number. When a user clicks on the previous or next button, the page number is incremented or decremented and the page is reloaded with the new page number.

Please or to participate in this conversation.