0

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(); ?>
Community
  • 1
  • 1
  • `array_chunk` might be of interest in your bid to paginate using only an array as data source. http://php.net/manual/en/function.array-chunk.php – Professor Abronsius Oct 06 '15 at 09:05

1 Answers1

0

Use array_slice to reduce your $subscribers array to a section of the array, using $offset as a starting record number, and $recordsPerPage to return a fixed number of results.

$offset = $pageNumber * $recordsPerPage;

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

/* Foreach Subscribers as Subscriber */
foreach ($subscribers as $subscriber) {
....
MaggsWeb
  • 3,018
  • 1
  • 13
  • 23