limit the for, php

Asked

Viewed 189 times

6

Good is the following, I am generating pagination 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 wonder if it is possible to limit the for in php so that it generates the links dynamically not taking up too much space on page, or if there is a smarter way to improve my code, thank you

veja como está sendo gerado

the code I currently use is this

$qtdPag = gets the total record value divided by results per page

$pg = receives via GET the current page

 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>";
                    }
            }
  • 2

    You too can give one LIMIT in the query coming from the bank, this would limit the amount of records coming from the bank and consequently the pagination

  • the big problem is that using sql server, it does not accept the limit (alias this has brought me big problems)

  • Alias... why don’t you work with Jpaginate?

  • well I am 'novice' let’s say so, I don’t want to use tools that facilitate the way before really learning the general logic

  • Look... regarding SQL Server, see if this link helps you: http://www.w3schools.com/sql/sql_top.asp

  • If SQL Server has LIMIT solves your problem? You see that you have the TOP?

  • Yes, it’s on the link I sent the TOP @bigow, in relation to limit the amount of links, Voce would have to bring all records, would continue appearing all page links, what you would do is a treatment via JS and/ or via CSS

  • because I’m going to have to deal with JS anyway, the problem is that I’m really horrible, terrible, with javascript, I don’t understand the logic in general and I’m always avoiding, but I’ll try your solution Mario, from Jpaginate..

  • I’ll try to give you a hand, brother

  • All right Marty, obg by the help

Show 5 more comments

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 placing [...] in these spaces. The variables $dottedBefore and $dottedAfter are used to verify that the string has already been displayed [...].

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

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

1

A possible solution without using the 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 take some manipulations of logic for when it is in values of type 1, 2, 3 and/or 320, 321, 322 it continue showing ten, instead of only 5... if you want I implement here

Browser other questions tagged

You are not signed in. Login or sign up in order to post.