PHP计算数组中的所有元素

时间:2017-07-05 02:52:52

标签: php

我无法在$ total_count变量中获取pages_article数组的sizeof / count。我试过$ total_count = count($ this-> pages_articles);并且它无法统计pages_articles数组中的所有元素。我也尝试过sizeof,这也是同样的问题。数组中有8个元素,当我进行回显/打印时,它会显示0。

  <?php
  class Pagination {
  public $current_page;
  public $per_page;
  public $total_count;
  public $pages_articles;

  public function __construct($page=1, $per_page=20, $total_count=0) {
 $this->current_page = (int)$page;
 $this->per_page = (int)$per_page;
 $this->total_count = (int)$total_count;
 $this->pages_articles=array(
  '<div class="article-loop"><img src="http://i.imgur.com/CmU3tnl.jpg"></div>',
  '<div class="article-loop"><img src="http://i.imgur.com/TDdxS9H.png"></div>',
  '<div class="article-loop"><img src="http://i.imgur.com/39rpmwB.jpg"></div>',
  '<div class="article-loop"><img src="http://i.imgur.com/1lBZQ1B.png"></div>',
  '<div class="article-loop"><img src="https://i.imgur.com/Y5Ld4Qfh.jpg"></div>',
  '<div class="article-loop"><img src="http://i.imgur.com/8HumESY.jpg"></div>',
  '<div class="article-loop"><img src="http://i.imgur.com/CqCZBvk.png"></div>',
  '<div class="article-loop"><img src="http://i.imgur.com/wQVPRVp.png"></div>');
  }

  public function offset() {
return ($this->current_page - 1) * $this->per_page;
   }
  public function total_pages() {
//$this->total_count=sizeof($this->pages_articles);
return ceil($this->total_count/$this->per_page);
   }
  public function previous_page() {
return $this->current_page - 1;
   } 
  public function next_page() {
return $this->current_page + 1;
   }
  public function has_previous_page() {
return $this->previous_page() >= 1 ? true : false;
   }
  public function has_next_page() {
return $this->next_page() <= $this->total_pages() ? true : false;
   }
  }
  $page = !empty($_GET['page']) ? (int)$_GET['page'] : 1;
  $per_page = 3;
  $total_count=8;
  $pagination = new Pagination($page, $per_page, $total_count);
  ?>

  <html>
  <body>
  <div>
<?php
    $i = $pagination->offset()  ;
    $limit = $pagination->per_page;
    while($i<$pagination->total_count && $limit>0) {
        echo $pagination->pages_articles[$i]."<br>";
        $i++;
        $limit--;
    }
      ?>
  </div>
  <ul>
      <?php
    if($pagination->has_previous_page()) {
        echo '<li style="display:inline"><a  href="index.php?page='.$pagination->previous_page().'">&laquo;</a></li>';
    } else {
        echo '<li style="display:inline" class="disabled"><a href="#">&laquo;</a></li>';
    }
    for($i=1; $i<=$pagination->total_pages(); $i++) {
        echo '<a href="index.php?page='.$i.'"><li style="display:inline; margin-left:5px; margin-right:5px">'.$i.'</li></a>';
    }
    if($pagination->has_next_page()) {
        echo '<li style="display:inline"><a href="index.php?page='.$pagination->next_page().'">&raquo;</a></li>';
    } else {
        echo '<li style="display:inline" class="disabled"><a href="#">&raquo;</a></li>';
    }
   ?>
  </ul>
  </body>
  </html>

1 个答案:

答案 0 :(得分:0)

尝试count($array, COUNT_RECURSIVE)

相关问题