Un minimum de CSS pour habiller les liens :
ul.pagination {
text-align: center;
}
ul.pagination li {
display: inline;
margin: 10px 5px;
padding: 5px;
text-align: center;
}
ul.pagination li.page-courante {
font-weight: bold;
}
Le fichier XML de test :
Le code PHP/DOM :
9]');
$page = isset($_GET['page']) ? intval($_GET['page']) : 1;
$dom = new DomDocument;
$dom->preserveWhiteSpace = FALSE;
$dom->load(FICHIER_XML);
$xpath = new DomXPath($dom);
$count = $xpath->evaluate('count(' . XPATH_EXPR . ')');
$debut_pos = ($page - 1) * NB_PAR_PAGE + 1;
$fin_pos = $page * NB_PAR_PAGE + 1;
$derniere_page = ceil($count / NB_PAR_PAGE);
/*if ($page < 1 || $page > $derniere_page) {
die('Hors limite');
}*/
if ($count > 0) {
$noeuds = $xpath->query(XPATH_EXPR . "[position()>=$debut_pos and position()<$fin_pos]");
foreach ($noeuds as $n) {
// afficher $n
echo $n->tagName, '
'; // pour test
}
// Affichage de la barre de navigation
if ($count > NB_PAR_PAGE) {
echo '';
}
}
On pourrait parfaitement utiliser SimpleXML mais il est plus gourmand étant donné qu'il n'est pas prévu pour évaluer des expressions XPath retournant autre chose que des noeuds :
9]');
$page = isset($_GET['page']) ? intval($_GET['page']) : 1;
$sxml = simplexml_load_file(FICHIER_XML);
$count = count($sxml->xpath(XPATH_EXPR));
$debut_pos = ($page - 1) * NB_PAR_PAGE + 1;
$fin_pos = $page * NB_PAR_PAGE + 1;
$derniere_page = ceil($count / NB_PAR_PAGE);
/*if ($page < 1 || $page > $derniere_page) {
die('Hors limite');
}*/
if ($count > 0) {
$noeuds = $sxml->xpath(XPATH_EXPR . "[position()>=$debut_pos and position()<$fin_pos]");
foreach ($noeuds as $n) {
// afficher $n
echo $n->getName(), '
';
}
// Affichage de la barre de navigation
if ($count > NB_PAR_PAGE) {
echo '';
}
}