douchen4547 2015-05-26 10:09
浏览 100

PHP循环通过JSON多维数组并在按钮单击时加载数据

Basically I'm retrieving a JSON content from a php call using some custom API. The arrays are all social posts. The code I'm using at the moment displays all of them at once when the page is loaded. I Would like to show them 10 or 20 at time, depending on my need, I'm using a PHP foreach loop to put the data on the page. I would like to get the first 10 indexes (from [0] to [10]) and setup a button to load the next indexes [11], [12], [13]..., let's say 10 at time( from [11] to [20], from [21] to [30] ) at each click. Is this possible?

The JSON content looks like this:

 Array
(
[comments] => Array
(
[0] => Array
(
[type] => instagram
[author] => Rick B.
[message] =>
  • 写回答

1条回答 默认 最新

  • duancutan4770 2015-05-26 10:27
    关注
    <?php
    
    function doMyStuff()
    {
        // Doing all your other stuff in here.. 
        $currentIndex = 30;
        getData($currentIndex);
    }    
    
    function getData(currentIndex) {
        $maxIndex = currentIndex + 10;
        for($x = $currentIndex; $x < maxIndex; $x++) {
            echo '<div class="'. $comment['content{$x}'] .'"><p>Content..'..'</p></div>';
        }
    
    }
    

    This may or may not be what you are looking for, but basically.. pass the getData function with the current index of the array, say 30 - getData(30); then the function will echo the next 10 contents based on the current index and the max index being 10 more than the current.

    ['content{$x}'] is a neat little way of directly inserting a variable / object into a string.

    评论

报告相同问题?

悬赏问题

  • ¥15 HFSS 中的 H 场图与 MATLAB 中绘制的 B1 场 部分对应不上
  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?