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] =>
<?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.