dongxieli3839 2015-10-06 09:00
浏览 36

如何用PHP分页数据

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条回答 默认 最新

  • dtmjl4427 2015-10-06 09:51
    关注

    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) {
    ....
    
    评论

报告相同问题?

悬赏问题

  • ¥15 微信会员卡接入微信支付商户号收款
  • ¥15 如何获取烟草零售终端数据
  • ¥15 数学建模招标中位数问题
  • ¥15 phython路径名过长报错 不知道什么问题
  • ¥15 深度学习中模型转换该怎么实现
  • ¥15 HLs设计手写数字识别程序编译通不过
  • ¥15 Stata外部命令安装问题求帮助!
  • ¥15 从键盘随机输入A-H中的一串字符串,用七段数码管方法进行绘制。提交代码及运行截图。
  • ¥15 TYPCE母转母,插入认方向
  • ¥15 如何用python向钉钉机器人发送可以放大的图片?