0
I am consulting an API that provides me data formatted in JSON, I collect it through file_get_contents and then print all the values using a foreach. However there are many values, more than 5,000 items, I need to page them, I tried to limit them using an array_slice but I cannot display the rest of the values when changing the page number.
I am using the following code for data collection:
$url = 'URL_DA_API';
$servers_zip = file_get_contents($url);
$servers_raw = gzdecode($servers_zip);
$json = json_decode($servers_raw, true);
To create the loop use the following:
foreach (array_slice($json['GET'], 0, 50) as $index=>$sv)
Finally my pagination is like this:
function paginacao($quantreg,$numreg,$url){
global $paginas;
global $pg;
$quant_pg = ceil($quantreg/$numreg);
$quant_pg++;
// Verifica se esta na primeira página, se nao estiver ele libera o link para anterior
if ($pg > 0) {
echo '<li class="prev"><a href="'.$url.'&pg='.($pg-1).'">Anterior</a></li>';
}
// Faz aparecer os numeros das página entre o ANTERIOR e PROXIMO
for($i_pg=1;$i_pg<$quant_pg;$i_pg++) {
// Verifica se a página que o navegante esta e retira o link do número para identificar visualmente
if ($pg == ($i_pg-1)) {
echo '<li class="active">'.$i_pg.'</li>';
} else {
$i_pg2 = $i_pg-1;
echo '<li><a href="'.$url.'&pg='.$i_pg2.'">'.$i_pg.'</a></li>';
}
}
// Verifica se esta na ultima página, se nao estiver ele libera o link para próxima
if (($pg+2) < $quant_pg) {
echo '<li class="next"><a href="'.$url.'&pg='.($pg+1).'">Próximo</a></li>';
}
}
The first page is displaying everything perfect, number of items, page numbers, by clicking on a number "page 58" it returns the url of page 58 however with page 1 values. :(
Somebody help me?
Didn’t work Júlio, confirms me which values will be in $pag * $reg_por_pag, $reg_por_pag
– W. Santana
In $pag you should go the page number, starting with zero, in $reg_per_page the number of records on each page. If I understand the way you’re doing.
– Júlio Neto
That’s right, it worked. I noticed that the error was not in the values I was entering, but in the item identifier of the array. I tried to index them through $index, but when I change the $index Zera page again. How could I index these results?
– W. Santana