Php 数组\u切片加载剩余项

Php 数组\u切片加载剩余项,php,Php,我返回了大量产品,并使用array_slice仅获取前8项 我将实现一个“查看更多”按钮,它将为用户加载前端上的其余项目 <?php $split_output = array_slice($_associatedProducts, 0, 8); // returns set number of products in array (8), for more button ?> 我的问题是,在显示8之后,如何返回数组中的剩余项?当用户单击“查看更

我返回了大量产品,并使用array_slice仅获取前8项

我将实现一个“查看更多”按钮,它将为用户加载前端上的其余项目

  <?php 
    $split_output = array_slice($_associatedProducts, 0, 8);       // returns set number of   products in array (8), for more button 
  ?>
我的问题是,在显示8之后,如何返回数组中的剩余项?当用户单击“查看更多”时,将显示这些项目


提前谢谢

你可以从你离开的地方开始切剩下的部分。第九项为抵销8。如果不向array_slice提供长度,它只会返回所有剩余项

$remaining_items = array_slice($_associatedProducts, 8);
如果你不想在用户点击一个链接后再这样做,那么有很多方法可以解决这个问题

使用JS异步获取数据 多页,查询中第一页限制为0,8,查看更多页不限制。 只需将所有数据发送到页面,并使剩余的产品最初隐藏起来,然后用按钮显示它们。 还有很多。。。 下面是一个简单地将所有数据发送到页面的示例,并使剩余的产品最初隐藏起来,然后用按钮显示它们

这也可以通过很多方式实现,这只是一个例子

然后单击“查看更多”时。。。您可以使用javascript显示其余项目

这样,你甚至不需要切片

例如:

css:

php/html


使用此按钮获取剩余的项目:

$more_output = array_slice($_associatedProducts, 8);
然后将它们放在隐藏的div中:

<div class="moreProducts">
  Place your hidden Products here
</div>
HTML链接:

<a href="javascript:;" class="showMore">More Products</a>
上面的代码只是一个示例。您必须根据需要对其进行更改。

不要使用array\u slice,而是将数组的所有值输出到页面中,但隐藏从第九个值开始的值,这可以通过foreach循环和计数器变量轻松实现。单击按钮即可应用Javascript取消隐藏这些值:

<?php
$_associatedProducts = array(); // then add values to the array
$num = 0;
foreach($_associatedProducts as $prod){
    if(++$num <= 8){
    print("<div>$prod</div>");
    }
    else{
    print("<div class=\"more\" style=\"display:none;\">$prod</div>");
    }
}
?>
<button type="button" id="myButton">See More</button>
<script type="text/javascript">
document.getElementById("myButton").onclick = function(){
var divs = document.getElementsByClassName("more");
var len = divs.length;
    for(var i = 0; i < len; i++){
    divs[i].style.display = "block";
    }
this.style.display = "none";
}
</script>
在查询字符串中传递起始索引,并将其用作数组_切片的起始位置。例如,在链接的查询字符串中提供start和show:mysite.php?start=10&show=10。我建议用JSON发送整个数组,并允许JavaScript管理表示,如果这是一个选项的话,正如@TimDev所建议的那样。
$more_output = array_slice($_associatedProducts, 8);
<div class="moreProducts">
  Place your hidden Products here
</div>
.moreProducts {
  display: none;
}
<a href="javascript:;" class="showMore">More Products</a>
$('a.showMore').bind('click', function() {
  $('.moreProducts').show();
});
<?php
$_associatedProducts = array(); // then add values to the array
$num = 0;
foreach($_associatedProducts as $prod){
    if(++$num <= 8){
    print("<div>$prod</div>");
    }
    else{
    print("<div class=\"more\" style=\"display:none;\">$prod</div>");
    }
}
?>
<button type="button" id="myButton">See More</button>
<script type="text/javascript">
document.getElementById("myButton").onclick = function(){
var divs = document.getElementsByClassName("more");
var len = divs.length;
    for(var i = 0; i < len; i++){
    divs[i].style.display = "block";
    }
this.style.display = "none";
}
</script>