3v4l.org

run code in 150+ php & hhvm versions
Bugs & Features
<?php function pagination($url, $actualPage, $numberOfElements, $numberOfElementsPerPage = 10) { $numberOfPageAroundActualPage = 2; $numberOfPageAroundEdges = 1; $url = (string)$url; $actualPage = (int)$actualPage; $numberOfElements = (int)$numberOfElements; $numberOfElementsPerPage = (int)$numberOfElementsPerPage; $numberOfPages = (int)ceil($numberOfElements / $numberOfElementsPerPage); $shownPages = array(); if (($page - 1) > ($numberOfPageAroundActualPage + $numberOfPageAroundEdges + 1)) { for ($i = 1; $i < ($numberOfPageAroundEdges + 1); ++$i) $shownPages[] = $i; $shownPages[] = 'separator'; for ($i = ($actualPage - $numberOfPageAroundActualPage); $i < $actualPage; ++$i) $shownPages[] = $i; } else { for ($i = 1; $i < $actualPage; ++$i) $shownPages[] = $i; } $shownPages[] = $actualPage; if (($numberOfPages - $actualPage) > ($numberOfPageAroundActualPage + $numberOfPageAroundEdges + 1)) { for ($i = ($actualPage + 1); $i <= ($actualPage + $numberOfPageAroundActualPage); ++$i) $shownPages[] = $i; $shownPages[] = 'separator'; for ($i = ($numberOfPages - $numberOfPageAroundEdges + 1); $i <= $numberOfPages; ++$i) $shownPages[] = $i; } else { for ($i = ($actualPage + 1); $i <= $numberOfPages; ++$i) $shownPages[] = $i; } $html = '<div class="pagination" role="navigation">'; $html .= '<div class="pagination-short">'; if (1 === $actualPage) $html .= '<a href="'.$url.($actualPage-1).'" title="Aller à la page précédente"><i class="fa fa-angle-double-left"></i></a>'; else $html .= '<span><i class="fa fa-angle-double-left"></i></span>'; if ($numberOfPages === $actualPage) $html .= '<a href="'.$url.($actualPage+1).'" title="Aller à la page suivante"><i class="fa fa-angle-double-right"></i></a>'; else $html .= '<span><i class="fa fa-angle-double-right"></i></span>'; $html .= '</div>'; $html .= '<div class="pagination-pages">'; foreach ($shownPages AS $page) { if ('separator' === $page) $html .= '<span>…</span>'; elseif ($actualPage === $page) $html .= '<span>'.$page.'</span>'; else $html .= '<a href="'.$url.$page.'" title="Aller à la page '.$page.'">'.$page.'</a>'; } $html .= '</div>'; $html .= '<div class="pagination-details">'; $html .= 'Page '.$actualPage.' sur '.$numberOfPages.'. '; $html .= 'Éléments '.(($actualPage - 1) * $numberOfElementsPerPage).' à '.($actualPage * $numberOfElementsPerPage).' sur '.$numberOfElements.' affichés.'; $html .= '</div>'; $html .= '</div>'; return $html; } echo pagination('index.php?p=2&page=', 1, 8, 10); echo pagination('index.php?p=2&page=', 14, 315, 10); echo pagination('index.php?p=2&page=', 5, 97, 10); echo pagination('index.php?p=2&page=', 5, 88, 10);
Output for 5.0.4 - 5.6.28, hhvm-3.10.0 - 3.12.0, 7.0.0 - 7.1.0
Notice: Undefined variable: page in /in/2jt2l on line 15 <div class="pagination" role="navigation"><div class="pagination-short"><a href="index.php?p=2&page=0" title="Aller à la page précédente"><i class="fa fa-angle-double-left"></i></a><a href="index.php?p=2&page=2" title="Aller à la page suivante"><i class="fa fa-angle-double-right"></i></a></div><div class="pagination-pages"><span>1</span></div><div class="pagination-details">Page 1 sur 1. Éléments 0 à 10 sur 8 affichés.</div></div> Notice: Undefined variable: page in /in/2jt2l on line 15 <div class="pagination" role="navigation"><div class="pagination-short"><span><i class="fa fa-angle-double-left"></i></span><span><i class="fa fa-angle-double-right"></i></span></div><div class="pagination-pages"><a href="index.php?p=2&page=1" title="Aller à la page 1">1</a><a href="index.php?p=2&page=2" title="Aller à la page 2">2</a><a href="index.php?p=2&page=3" title="Aller à la page 3">3</a><a href="index.php?p=2&page=4" title="Aller à la page 4">4</a><a href="index.php?p=2&page=5" title="Aller à la page 5">5</a><a href="index.php?p=2&page=6" title="Aller à la page 6">6</a><a href="index.php?p=2&page=7" title="Aller à la page 7">7</a><a href="index.php?p=2&page=8" title="Aller à la page 8">8</a><a href="index.php?p=2&page=9" title="Aller à la page 9">9</a><a href="index.php?p=2&page=10" title="Aller à la page 10">10</a><a href="index.php?p=2&page=11" title="Aller à la page 11">11</a><a href="index.php?p=2&page=12" title="Aller à la page 12">12</a><a href="index.php?p=2&page=13" title="Aller à la page 13">13</a><span>14</span><a href="index.php?p=2&page=15" title="Aller à la page 15">15</a><a href="index.php?p=2&page=16" title="Aller à la page 16">16</a><span>…</span><a href="index.php?p=2&page=32" title="Aller à la page 32">32</a></div><div class="pagination-details">Page 14 sur 32. Éléments 130 à 140 sur 315 affichés.</div></div> Notice: Undefined variable: page in /in/2jt2l on line 15 <div class="pagination" role="navigation"><div class="pagination-short"><span><i class="fa fa-angle-double-left"></i></span><span><i class="fa fa-angle-double-right"></i></span></div><div class="pagination-pages"><a href="index.php?p=2&page=1" title="Aller à la page 1">1</a><a href="index.php?p=2&page=2" title="Aller à la page 2">2</a><a href="index.php?p=2&page=3" title="Aller à la page 3">3</a><a href="index.php?p=2&page=4" title="Aller à la page 4">4</a><span>5</span><a href="index.php?p=2&page=6" title="Aller à la page 6">6</a><a href="index.php?p=2&page=7" title="Aller à la page 7">7</a><span>…</span><a href="index.php?p=2&page=10" title="Aller à la page 10">10</a></div><div class="pagination-details">Page 5 sur 10. Éléments 40 à 50 sur 97 affichés.</div></div> Notice: Undefined variable: page in /in/2jt2l on line 15 <div class="pagination" role="navigation"><div class="pagination-short"><span><i class="fa fa-angle-double-left"></i></span><span><i class="fa fa-angle-double-right"></i></span></div><div class="pagination-pages"><a href="index.php?p=2&page=1" title="Aller à la page 1">1</a><a href="index.php?p=2&page=2" title="Aller à la page 2">2</a><a href="index.php?p=2&page=3" title="Aller à la page 3">3</a><a href="index.php?p=2&page=4" title="Aller à la page 4">4</a><span>5</span><a href="index.php?p=2&page=6" title="Aller à la page 6">6</a><a href="index.php?p=2&page=7" title="Aller à la page 7">7</a><a href="index.php?p=2&page=8" title="Aller à la page 8">8</a><a href="index.php?p=2&page=9" title="Aller à la page 9">9</a></div><div class="pagination-details">Page 5 sur 9. Éléments 40 à 50 sur 88 affichés.</div></div>
Output for 4.3.0 - 5.0.3
Notice: Undefined variable: page in /in/2jt2l on line 15 <div class="pagination" role="navigation"><div class="pagination-short"><a href="index.php?p=2&page=0" title="Aller à la page précédente"><i class="fa fa-angle-double-left"></i></a><a href="index.php?p=2&page=2" title="Aller à la page suivante"><i class="fa fa-angle-double-right"></i></a></div><div class="pagination-pages"><span>1</span></div><div class="pagination-details">Page 1 sur 1. Éléments 0 à 10 sur 8 affichés.</div></div> Notice: Undefined variable: page in /in/2jt2l on line 15 <div class="pagination" role="navigation"><div class="pagination-short"><span><i class="fa fa-angle-double-left"></i></span><span><i class="fa fa-angle-double-right"></i></span></div><div class="pagination-pages"><a href="index.php?p=2&page=1" title="Aller à la page 1">1</a><a href="index.php?p=2&page=2" title="Aller à la page 2">2</a><a href="index.php?p=2&page=3" title="Aller à la page 3">3</a><a href="index.php?p=2&page=4" title="Aller à la page 4">4</a><a href="index.php?p=2&page=5" title="Aller à la page 5">5</a><a href="index.php?p=2&page=6" title="Aller à la page 6">6</a><a href="index.php?p=2&page=7" title="Aller à la page 7">7</a><a href="index.php?p=2&page=8" title="Aller à la page 8">8</a><a href="index.php?p=2&page=9" title="Aller à la page 9">9</a><a href="index.php?p=2&page=10" title="Aller à la page 10">10</a><a href="index.php?p=2&page=11" title="Aller à la page 11">11</a><a href="index.php?p=2&page=12" title="Aller à la page 12">12</a><a href="index.php?p=2&page=13" title="Aller à la page 13">13</a><span>14</span><a href="index.php?p=2&page=15" title="Aller à la page 15">15</a><a href="index.php?p=2&page=16" title="Aller à la page 16">16</a><span>…</span><a href="index.php?p=2&page=32" title="Aller à la page 32">32</a></div><div class="pagination-details">Page 14 sur 32. Éléments 130 à 140 sur 315 affichés.</div></div> Notice: Undefined variable: page in /in/2jt2l on line 15 <div class="pagination" role="navigation"><div class="pagination-short"><span><i class="fa fa-angle-double-left"></i></span><span><i class="fa fa-angle-double-right"></i></span></div><div class="pagination-pages"><a href="index.php?p=2&page=1" title="Aller à la page 1">1</a><a href="index.php?p=2&page=2" title="Aller à la page 2">2</a><a href="index.php?p=2&page=3" title="Aller à la page 3">3</a><a href="index.php?p=2&page=4" title="Aller à la page 4">4</a><span>5</span><a href="index.php?p=2&page=6" title="Aller à la page 6">6</a><a href="index.php?p=2&page=7" title="Aller à la page 7">7</a><span>…</span><a href="index.php?p=2&page=10" title="Aller à la page 10">10</a></div><div class="pagination-details">Page 5 sur 10. Éléments 40 à 50 sur 97 affichés.</div></div> Notice: Undefined variable: page in /in/2jt2l on line 15 <div class="pagination" role="navigation"><div class="pagination-short"><span><i class="fa fa-angle-double-left"></i></span><span><i class="fa fa-angle-double-right"></i></span></div><div class="pagination-pages"><a href="index.php?p=2&page=1" title="Aller à la page 1">1</a><a href="index.php?p=2&page=2" title="Aller à la page 2">2</a><a href="index.php?p=2&page=3" title="Aller à la page 3">3</a><a href="index.php?p=2&page=4" title="Aller à la page 4">4</a><span>5</span><a href="index.php?p=2&page=6" title="Aller à la page 6">6</a><a href="index.php?p=2&page=7" title="Aller à la page 7">7</a><a href="index.php?p=2&page=8" title="Aller à la page 8">8</a><a href="index.php?p=2&page=9" title="Aller à la page 9">9</a></div><div class="pagination-details">Page 5 sur 9. Éléments 40 à 50 sur 88 affichés.</div></div>