Now I generate links to the menu pages with this code:

for ($i = 0; $i < $num_of_pages; $i++) { $actual = $i + 1; if($i === intval($page)){ echo "<a id=\"now\" href=\"http://site.com/index.php?page=".$i."\">".$actual."</a>&nbsp;"; } else { echo "<a id=\"other\" href=\"http://site.com/index.php?page=".$i."\">".$actual."</a>&nbsp;"; } } 

And it displays the total number of pages in the menu. How can I make a restriction, so that each side of the actual page displays 2 previous / next, and instead of the remaining dots. Assume:

1 2 3 ...

1 2 3 4 ...

1 2 3 4 5 ...

... 2 3 4 5 6 ...

While ideas on how to track it correctly, did not come.

    1 answer 1

    id=other is not good. id assumes a unique value.

    https://www.w3.org/TR/html5/dom.html#the-id-attribute The id attribute specifies its unique identifier (ID). The IDs must be unique amongst all the IDs and must contain at least one character. The space must not contain any space characters.

    Instead of ID it is better to use Class

    And now in the case:

     // Получили $page, проверили на границы (больше 0, меньше $num_of_pages) $page= min($num_of_pages-1, max(0, intval($_GET['page']))); // Теперь рассчитаем, границы, какие будем показывать $from = max(0, $page-2); // меньше на 2 страницы, но не меньше 0 $to = min($num_of_pages-1, $page+2); // больше на 2 страницы, но не больше $num_of_pages // если начинаем не с 0, то показать многоточие if ($from!==0){ echo "&hellip;"; } for ($i = $from; $i <= $to; $i++) { echo ' <a class='.($i===$page ? "now" : "other").' href="http://site.com/index.php?page='.$i.'">'.($i+1).'</a> '; } // если закончили не последней страницей, показать многоточие if ($to!==$num_of_pages-1){ echo "&hellip;"; }