PHP计算数组中的所有元素
我无法在$total\u count变量中获取pages\u article数组的sizeof/count。我试过$total\u count=count($this->pages\u articles);而且它无法计算pages\u articles数组中的所有元素。我也试过sizeof,也是同样的问题。数组中有8个元素,当我执行echo/print时,它显示0PHP计算数组中的所有元素,php,Php,我无法在$total\u count变量中获取pages\u article数组的sizeof/count。我试过$total\u count=count($this->pages\u articles);而且它无法计算pages\u articles数组中的所有元素。我也试过sizeof,也是同样的问题。数组中有8个元素,当我执行echo/print时,它显示0 <?php class Pagination { public $current_page; public $
<?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().'">«</a></li>';
} else {
echo '<li style="display:inline" class="disabled"><a href="#">«</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().'">»</a></li>';
} else {
echo '<li style="display:inline" class="disabled"><a href="#">»</a></li>';
}
?>
</ul>
</body>
</html>
试试count($array,count\u RECURSIVE)
试试count($array,count\u RECURSIVE)
OP说他已经试过了。完整阅读问题,然后在评论中得到澄清。我看不到任何地方说他尝试了递归计数。是吗?我也试过了,但也不起作用。你能给你的数组添加一个var_转储吗?这就是打印到屏幕上的内容。数组(8){[0]=>string(74)”“[1]=>string(74)”“[2]=>string(74)”“[3]=>string(74)”“[4]=>string(76)”“[5]=>string(74)”“[6]=>string(74)”“[7]=>string(74)”}OP说他已经试过了。完整阅读问题,然后在评论中得到澄清。我看不到任何地方说他尝试了递归计数。是吗?我也试过了,但也不起作用。你能给你的数组添加一个var_转储吗?这就是打印到屏幕上的内容。数组(8){[0]=>string(74)“[1]=>string(74)”[2]=>string(74)“[3]=>string(74)“[4]=>string(76)”[5]=>string(74)“[6]=>string(74)“[7]=>string(74)}使用exit(var\u dump($this->pages\u articles)
count前一行,以确保页面\u文章有内容,您尝试过吗?我认为在public function total\u pages(){}中获取数据可能有问题
因此,首先尝试在该函数中打印数组。如果我在total\u pages函数中打印数组,它就会工作。我在该函数中创建了一个新变量$total,并分配了count($this->pages\u articles);然后我尝试将此变量赋给$total_count,但它仍然显示为0。此代码按原样工作正常。其工作原理是计数计算正常,并且返回正确的数字,而不是0
。此代码的不必要复杂性令人困惑。为什么不让构造函数以$this->结尾total_count=sizeof($this->pages_articles);
在计数前使用exit(var_dump($this->pages_articles)
一行,以确保页面文章有内容,您尝试了吗?我认为在公共函数total_pages()中获取数据可能有问题{
因此,首先尝试在该函数中打印数组。如果我在total\u pages函数中打印数组,它就会工作。我在该函数中创建了一个新变量$total,并分配了count($this->pages\u articles);然后我尝试将此变量赋给$total_count,但它仍然显示为0。此代码按原样工作正常。其工作原理是计数计算正常,并且返回正确的数字,而不是0
。此代码的不必要复杂性令人困惑。为什么不让构造函数以$this->结尾total_count=sizeof($this->pages_articles);