limit the for, php

6

Well, this is the following, I'm generating paging links to get more lightness in my system, the problem is I have many records in the database and this pagination generates many links, thus occupying a very large part of the page, I would like to if it is possible to limit the for in the php so that it generates the links dynamically not taking up much space on page, or if there is a smarter way of improving my code, thanks

ThecodeIcurrentlyuseisthis

$qtdPag=receivestotalrecordsvaluedividedbyresultsperpage

$pg=receiveviaGETthecurrentpage

if($qtdPag>1&&$pg<=$qtdPag){for($i=1;$i<=$qtdPag;$i++){if($i==$pg){echo"<i>[".$i."]</i>";
                    } else {
                            echo "<i><a href='socios.php?pg=$i'>[".$i."]</a></i>";
                    }
            }
    
asked by anonymous 19.12.2014 / 14:38

2 answers

6

You can do it this way:

$qtdPag = 20;
$pg = 10;

$dottedBefore = false;
$dottedAfter = false;

if ($qtdPag > 1 && $pg <= $qtdPag) {
    for ($i = 1; $i <= $qtdPag; $i++) {
        if ($i == $pg) {
            echo "<i>[" . $i . "]</i>";
        } else if ($i < ($pg - 5) && $i != 1) {
            if (!$dottedBefore) {
                echo "<i>[...]</i>";
                $dottedBefore = true;
            }
        } else if ($i > ($pg + 5) && $i < $qtdPag) {
            if (!$dottedAfter) {
                echo "<i>[...]</i>";
                $dottedAfter = true;
            }
        } else {
            echo "<i><a href='socios.php?pg=" . $i . "'>[" . $i . "]</a></i>";
        }
    }
}

What this script does is limit the number of pages displayed before and after the current page by putting [...] in these spaces. The variables $dottedBefore and $dottedAfter are used to check if the [...] string has already been displayed.

If you want to change the number of pages displayed before or after the current page, simply change the corresponding condition number.

If the code was confusing leave a comment, but I believe that giving a good analysis you will understand without problems. =)

    
19.12.2014 / 15:09
1

A possible solution without using JPaginate :

HTML:

< body onload="mostraDez()">
< input type="button" onclick="primeiro()" value="<<"/>
< input type="button" onclick="primeiro()" value="<<"/>

PHP:

<?php
echo "<input type='hidden' id='pgAtual' value='".$_GET['pg']."'/>";
$qtdPag = 322; //Aqui a quantidade total de páginas

if($qtdPag > 1 && $_GET['pg'] <= $qtdPag){   
    for($i = 1; $i <= $qtdPag; $i++){
        if($i == $_GET['pg']){
            echo "<i>[".$i."]</i>";
        }else{
            echo "<i><a id='".$i."' href='teste.php?pg=".$i."' style='display:none'>[".$i."]</a></i>";
        }
    }

    echo "<input type='hidden' id='qtdEle' value='".$i."'/>";
}

insira o código aqui ? >

JS:

function mostraDez(){
    atual = document.getElementById("pgAtual").value;
    for (var i = (parseInt(atual)+5); atual < i; i--) {
        if (i >= 1){
            document.getElementById(i).style.display = "inline";
            console.log(i);
        }
    }

    for (var i = (parseInt(atual)-5); atual > i; i++) {
        if (i >= 1){
            document.getElementById(i).style.display = "inline";
            console.log(i);
        }
    }
}

function primeiro(){
    location.href = "teste.php?pg=1";
}

function ultimo(){
    location.href = "teste.php?pg=" + document.getElementById("qtdEle").value;
}

It would still require some logic manipulation for when it is in values of type 1, 2, 3 and / or 320, 321, 322 it still shows ten, instead of only 5 ... if you want to implement here

    
19.12.2014 / 17:18