Voic le script
Je l'est un peu modifier pour pouvoir l'utiliser sur n'importe quelle structure.
Il est tiré d'
ici
Le scipt:
La fonction (qui permet un utilisation multiples.
function get_list_page($page, $nb_page, $targetpage, $nb = 2)
{
if ($page == 0) $page = 1; //if no page var is given, default to 1.
$prev = $page - 1; //previous page is page - 1
$next = $page + 1; //next page is page + 1
$lastpage = $nb_page; //lastpage is = total pages / items per page, rounded up.
$lpm1 = $lastpage - 3; //last page minus 1
$adjacents = 1;
/*
Now we apply our rules and draw the pagination object.
We're actually saving the code to a variable in case we want to draw it more than once.
*/
$pagination = "";
$pagination .= "<div class=\"forum_pagination\">";
//pages
if ($lastpage < 7 + ($adjacents * 2)) //not enough pages to bother breaking it up
{
for ($counter = 1; $counter <= $lastpage; $counter++)
{
if ($counter == $page)
$pagination.= "<span class=\"current\">$counter</span>";
else
$pagination.= "<a href=\"$targetpage&page=$counter\">$counter</a>";
}
}
elseif($lastpage > 5 + ($adjacents * 2)) //enough pages to hide some
{
//close to beginning; only hide later pages
if($page < 1 + ($adjacents * 2))
{
for ($counter = 1; $counter < 4 + ($adjacents * 2); $counter++)
{
if ($counter == $page)
$pagination.= "<span class=\"current\">$counter</span>";
else
$pagination.= "<a href=\"$targetpage&page=$counter\">$counter</a>";
}
$pagination.= "...";
$pagination.= "<a href=\"$targetpage&page=$lpm1\">$lpm1</a>";
$pagination.= "<a href=\"$targetpage&page=$lastpage\">$lastpage</a>";
}
//in middle; hide some front and some back
elseif($lastpage - ($adjacents * 2) > $page && $page > ($adjacents * 2))
{
$pagination.= "<a href=\"$targetpage&page=1\">1</a>";
$pagination.= "...";
for ($counter = $page - $adjacents; $counter <= $page + $adjacents; $counter++)
{
if ($counter == $page)
$pagination.= "<span class=\"current\">$counter</span>";
else
$pagination.= "<a href=\"$targetpage&page=$counter\">$counter</a>";
}
$pagination.= "...";
$pagination.= "<a href=\"$targetpage&page=$lpm1\">$lpm1</a>";
$pagination.= "<a href=\"$targetpage&page=$lastpage\">$lastpage</a>";
}
//close to end; only hide early pages
else
{
$pagination.= "<a href=\"$targetpage&page=1\">1</a>";
$pagination.= "<a href=\"$targetpage&page=2\">2</a>";
$pagination.= "...";
for ($counter = $lastpage - (2 + ($adjacents * 2)); $counter <= $lastpage; $counter++)
{
if ($counter == $page)
$pagination.= "<span class=\"current\">$counter</span>";
else
$pagination.= "<a href=\"$targetpage&page=$counter\">$counter</a>";
}
}
}
$pagination.= "</div>\n";
return $pagination;
}
Un exemple d’utilisation
<?php
// on récupère les données (c'est du PDO ici, mais ça fonctionnerais pareil avec le système "normal")
// on compte le nombre de postes dans chaque forum
$query=$cnx->prepare('SELECT t_topic.forum_id, forum_auth_view, forum_auth_post, forum_auth_topic, forum_auth_announce, forum_auth_moderation,
(SELECT COUNT(topic_id) FROM t_topic WHERE topic_type <> "Annonce" AND t_topic.forum_id = :forum )AS Nbpost
FROM t_topic
LEFT JOIN t_forum ON t_topic.forum_id = t_forum.forum_id
WHERE t_topic.forum_id = :forum');
$query->bindValue(':forum',$forum,PDO::PARAM_INT);
$query->execute();
$data0=$query->fetch();
// on détermine le nombre de page à afficher (je pense que c'est compréhensible ^^)
$totalDesMessages = $data0['Nbpost'];
$nombreDeTopicsParPage = 25;
$nombreDePages = ceil($totalDesMessages / $nombreDeTopicsParPage);
$premierMessageAafficher = ($page - 1) * $nombreDeTopicsParPage;
//on liste les forums
$topics=$cnx->prepare('
SELECT
t_forum.forum_id, t_forum.forum_name, t_forum.forum_description,
t_topic.topic_id, t_post.post_id, t_post.topic_id,
t_post.post_date, t_post.users_id, topic_nb_view, topic_title,
(SELECT COUNT(topic_id) FROM t_topic WHERE t_topic.forum_id = t_forum.forum_id ) AS Nbtopic,
(SELECT users_id FROM t_users WHERE t_topic.users_id = t_users.users_id ) AS Creator,
(SELECT COUNT(post_id) FROM t_post WHERE t_post.topic_id = t_topic.topic_id )AS Nbpost,
(SELECT COUNT(post_id) FROM t_post WHERE t_post.topic_id = t_topic.topic_id AND t_topic.topic_last_post <> t_post.post_id )AS Nbreponse
FROM t_forum
LEFT JOIN t_topic ON t_forum.forum_id = t_topic.forum_id
LEFT JOIN t_post ON t_topic.topic_id = t_post.topic_id
WHERE topic_type <> "Annonce" AND t_topic.topic_last_post = t_post.post_id AND t_forum.forum_id = :forum
ORDER BY topic_last_post DESC
LIMIT :premier, :nombre'); // LIMIT sert ici à limiter le nombre d'affichage en fonction du système de pagination. (c'est à dire de la page où on se situe)
$topics->bindValue(':forum',$forum,PDO::PARAM_INT);
$topics->bindValue(':premier',(int) $premierMessageAafficher,PDO::PARAM_INT);
$topics->bindValue(':nombre',(int) $nombreDeTopicsParPage,PDO::PARAM_INT);
$topics->execute();
/* *** */
/* *** */
// On affiche le résultat avec la pagination
echo get_list_page($page, $nombreDePages, 'voirforum?forumid='.$forum).'</div>'; // voir au dessous)
// on passe en paramètre les informations nécessaire pour utiliser la pagination, soit la page actuelle, le nombre de pages, et l'url
?>
Préviens moi par mp si tu galére, j'essayerais de t'aider, mais une fois compris comment ça fonctionne, c'est super simple ^^
Malheureusement j'ai pas de cas moins compliquer, mais je pense que tu devrais savoir interpréter cela et le comprendre, et par défaut, on est là pour t'aider ^^