-3
\$\begingroup\$
for ($i = ($pagcurrent >= 5 ? $pagcurrent - 4 : $pagcurrent); $i <= ($paglast < $pagcurrent + 4 ? $paglast : $pagcurrent + 4); $i++) {
    if ($i == $pagcurrent) {
        echo "<li class='current'>" . $i . "</li>";
    }
    else {
        echo "<li><a href='?page=" . $i . "&searchPlayername=" . $searchPlayername . "&searchSteamid=" . $searchSteamid . "&searchServer=" . $searchServer . "'>" . $i . "</a></li>";
    }
}

So, this is basically what I have managed to do so far, but I was concerned whether i was skipping pages and whether the page with the "current" class is not actually the real page, idk, just concerned a bit, and since Im not very good at loops, i wanted someone to check if the code is good or not, and any suggestions if you have.

\$\endgroup\$
5
  • 1
    \$\begingroup\$ figure out how you can implement a modulus here instead of a ternary,, \$\endgroup\$ Commented Jul 24, 2017 at 18:43
  • \$\begingroup\$ Im not sure what a modulus and a ternary have in common \$\endgroup\$
    – Klajdi
    Commented Jul 24, 2017 at 18:57
  • \$\begingroup\$ The question is, what does ternary have to do with pagination. Look for some code samples and you will surely see how the modulus is used. You will likely not find any examples using ternary in a for loop like thag. \$\endgroup\$ Commented Jul 24, 2017 at 18:59
  • \$\begingroup\$ well, the example I used showed all the pages, instead I wanted it to show only the first few \$\endgroup\$
    – Klajdi
    Commented Jul 24, 2017 at 19:03
  • \$\begingroup\$ I've provided an example in the answers. If that is what you were looking for please remember ot hit the check button thing. \$\endgroup\$ Commented Jul 24, 2017 at 19:17

2 Answers 2

0
\$\begingroup\$

Here's an example of a neater way to do what you're trying to do..

// Number of pages to display
$pagesToDisplay = 10;

// First page to display
$start = $pagcurrent;
if($start >= 1) $start = $start - ($paglast % $pagesToDisplay);

// Last page to display (if it exists)
$end = $start + $pagesToDisplay;

// Loop until there is nothing left to loop thru OR the maximum number of pages has been looped
for ($i = $start; $i < $end && $i <= $paglast; $i++) { ... }

Here's a fully working example: https://3v4l.org/N7bXj

This is neater and much easier to manage.

\$\endgroup\$
3
  • \$\begingroup\$ well, it shows -2, -1, 0 when im on first page. \$\endgroup\$
    – Klajdi
    Commented Jul 24, 2017 at 19:23
  • 1
    \$\begingroup\$ i wasn't trying to re-write the whole thing for you i was just suggesting a neater approach. you'll obviously have to make sure $i is >= 0. i went ahead and added that to the answer and updated the link. \$\endgroup\$ Commented Jul 24, 2017 at 19:27
  • 1
    \$\begingroup\$ well, I ended up sticking with the code I posted below, but yours will probably serve for another day. Thanks. \$\endgroup\$
    – Klajdi
    Commented Jul 24, 2017 at 19:45
1
\$\begingroup\$

slightly changed a few conditions from another code

$dots = false;
for ( $i=1; $i<=$paglast; $i++ ) {
    if( $i == $pagcurrent ) {
        echo "<li class='current'><a>" . $i . "</a></li>";
    } else {
        if ( ( $pagcurrent < 5 && $i < 5 ) || ( ( $i > $pagcurrent-4 && $i < $pagcurrent ) || $i > $paglast-2 || $i > $pagcurrent && $i < $pagcurrent+5 ) ) {
            echo "<li><a href='?page=" . $i . "&searchPlayername=" . $searchPlayername . "&searchSteamid=" . $searchSteamid . "&searchServer=" . $searchServer . "'>" . $i . "</a></li>";
        } else {
            if ( $i > $pagcurrent && !$dots ) {
                echo "...";
                $dots = true;
            }
        }
    }
}
\$\endgroup\$

Not the answer you're looking for? Browse other questions tagged or ask your own question.