How to Paginate Data with PHP

时间:2015-10-06 09:00:29

标签: php twitter-bootstrap-3 pagination

What I'm trying to achieve:
Display five schools per set/pages, use Bootstrap Pagination to navigate through each set/pages.

Current Problems:
I have only been able to find solutions using PHP/SQL Databases (see links below), I want to be able to just use data from an Array to begin Pagination.

Techumber Simple PHP Pagination
Code Tutsplus Example
Simple PHP Pagination
PHPFreaks Basic Pagination

Help and advice is needed,
Thank you in advance!

Display:

enter image description here

PHP:

<?php
  /*
  Template Name: Leaderboard
  */
  get_header();
?>
<div id="primary" class="content-area">
  <main id="main" class="site-main" role="main">
    <?php
      /* Current User ID */
      $currentUserID = get_current_user_id();
      /* Current User School Name */
      $currentSchoolName = get_the_author_meta('School-Name', $user_ID);
      /* Current User School Region */
      $currentSchoolRegion = get_the_author_meta('School-Region', $user_ID);
      /* Current School Position */
      $currentSchoolPosition = array();
      /* Get Subscribers */
      $subscribers = get_users('role=subscriber');
      /* Subscriber School Array */
      $schoolArray = array();
      /* Foreach Subscribers as Subscriber */
      foreach ($subscribers as $subscriber) {
        /* Subscriber School Name */
        $schoolName = $subscriber->get('School-Name');
        /* Subscriber School Region */
        $schoolRegion = $subscriber->get('School-Region');
        /* If Subscriber School Region matches Current User School Region PUSH School Name to Array */
        if ($schoolRegion === $currentSchoolRegion) {
          array_push($schoolArray, $schoolName);
        }
      }
      /* Reverse School Array Order - High to Low */
      arsort($subscriberValues);
      /* Count Matching Schoole Array Values */
      $subscriberValues = array_count_values($schoolArray);
      /* Foreach Get Current School Position */
      $i = 1;
      foreach ($subscriberValues as $key => $value) {
        $counter = $i++;
        if ($key === $currentSchoolName) {
          array_push($currentSchoolPosition, $counter);
        }
      }
      /* Header Message */
      echo '<p class="text-center">'.$currentSchoolRegion.' Leaderboard</p><p class="text-center">Your school <strong>'.$currentSchoolName.'</strong> is currently in '.ordinal($currentSchoolPosition[0]).' position</p>';

      /* School Leaderboard Table */
      echo '<table class="table">';
      $i = 1;
      foreach ($subscriberValues as $key => $value) {
        $counter = $i++;
          if ($key === $currentSchoolName){
            echo '<tr class="active">';
          } else {
            echo '<tr>';
          }
          /* If 'counter' = '1' display 'Champion School' else display 'counter' value */
          if ($counter === 1) {
            echo '<th scope="row">Champion School</th><td>'.$key.'</td><td>'.$value.' Signups</td>';
          } else {
            echo '<th scope="row">'.$counter.'</th><td>'.$key.'</td><td>'.$value.' Signups</td>';
          }
          echo '</tr>';
      }
      echo '</table>';
      ?>
      <nav class="text-center">
        <ul class="pagination">
          <li>
            <a href="#" aria-label="Previous">
              <span aria-hidden="true">&laquo;</span>
            </a>
          </li>
          <li class="active"><a href="#">1</a></li>
          <li><a href="#">2</a></li>
          <li><a href="#">3</a></li>
          <li><a href="#">4</a></li>
          <li><a href="#">5</a></li>
          <li>
            <a href="#" aria-label="Next">
              <span aria-hidden="true">&raquo;</span>
            </a>
          </li>
        </ul>
      </nav>
      <?php

      /* Current School Position - Format */
      function ordinal($number){
        $ends = array('th','st','nd','rd','th','th','th','th','th','th');
        if ((($number % 100) >= 11) && (($number % 100) <= 13)) {
          return $number.'th';
        } else {
          return $number.$ends[$number % 10];
        }
      }
    ?>
  </main><!-- #main -->
</div><!-- #primary -->
<?php get_footer(); ?>

1 个答案:

答案 0 :(得分:0)

使用array_slice$subscribers数组缩减为数组的一部分,使用$offset作为起始记录编号,使用$recordsPerPage返回固定数量的结果

$offset = $pageNumber * $recordsPerPage;

$subscribers = array_slice($subscribers,$offset,$recordsPerPage);

/* Foreach Subscribers as Subscriber */
foreach ($subscribers as $subscriber) {
....