Bonjour à vous,
je suis un peu (beaucoup) débutant en code, mais j'aimerais faire un tableau avec pagination.
Du coup, j'ai essayé d'adapter un code à mon tableau généré à partir d'une requête SQL. J'arrive bien à n'afficher que les 10 premiers éléments de mon tableau avec ce code, mais ce sont toujours les mêmes 10 premiers éléments qui s'affichent sur toutes les pages et le lien "page précédente" ne s'affiche jamais.
En regardant ce bout de code, est-ce que quelqu'un pourrait m'aider ?
merci d'avance !
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96 //connexion au serveur $serveur = "localhost"; $login = "root"; $pass = ""; $dbname = "archeodunum_local"; $chemin_image="http://archeodunum/"; $hsc = function($p) { return htmlspecialchars($p, ENT_QUOTES, 'utf-8'); }; // échappement des caractères dangereux try{ $connexion = new PDO("mysql:host=$serveur;dbname=$dbname;charset=UTF8",$login,$pass); $connexion->setAttribute(PDO::ATTR_ERRMODE,PDO::ERRMODE_EXCEPTION); } catch(PDOException $e){ echo 'Echec : ' .$e->getMessage(); } //requête SQL et mise en forme en tableau $page = (!empty($_GET['page']) ? $_GET['page'] : 1); $limite=10; $debut = ($page -1) * $limite; $sql2="SELECT SQL_CALC_FOUND_ROWS * from site LEFT JOIN auteur ON site.ID_RO=auteur.ID_Auteur ORDER BY LTRIM(UPPER(site.Commune)) ASC LIMIT :limite OFFSET :debut"; $sql2 = $connexion->prepare($sql2); $sql2->bindValue('debut', $debut,PDO::PARAM_INT); $sql2->bindValue('limite',$limite,PDO::PARAM_INT); $sql2->execute(); $resultFoundRows = $connexion->query('SELECT found_rows()'); $nombredElementsTotal = $resultFoundRows->fetchColumn(); ?> <style> th{ text-align: center; } td{ border: 1px solid lightgrey; padding: 5px !important; vertical-align: middle; } </style> <table class="table table-hover" > <thead> <tr> <th width=25%> Commune - Lieu-dit </th> <th width=10%> Département </th> <th width=5%> Année </th> <th width= 15%> Type de site </th> <th width=10%> Etat d'avancement </th> <th width=15%> RO </th> <th width=5%> Latitude </th> <th width=5%> Longitude </th> <th width=5%> Notice </th> <th width= 10%> </th> </tr> </thead> <?php while ($row = $sql2->fetch()) { $row = array_map($hsc, $row); // échappement de toutes les valeurs $notice = (!empty($row['Notice']))? '<a href='.$row['Notice'].'><img src='.$chemin_image.'lien alt=notice width=30% /></a>' : ''; $fiche_RO = (!empty($row['Fiche']))? '<a href='.$chemin_image.'collaborateur/'.$row['Nom'].'-'.$row['Prénom'].'>'.$row['Nom_Auteur'].'</a>' : ''.$row['Nom_Auteur'].''; echo "<tr> <td>".$row['Commune']." - ".$row['Lieu_dit']."</td> <td>".$row['Departement']."</td> <td>".$row['Annee']."</td> <td>".$row['Type']."</td> <td>".$row['Etat_fouille']."</td> <td>".$fiche_RO."</td> <td>".$row['Lat']."</td> <td>".$row['Lon']."</td> <td Style='text-align:center !important'>".$notice."</td> <td><a href='modifier_site?id=".$row['ID_Site']."&o=u'>Modifier</a></td> </tr>\n"; } ?> </table> <?php $nombreDePages = ceil($nombredElementsTotal / $limite); if ($page > 1): ?><a href="?page=<?php echo $page - 1; ?>">Page précédente</a> <?php endif; for ($i = 1; $i <= $nombreDePages; $i++): ?><a href="?page=<?php echo $i; ?>"><?php echo $i; ?></a> <?php endfor; if ($page < $nombreDePages): ?> <a href="?page=<?php echo $page + 1; ?>">Page suivante</a><?php endif; ?>
Clément
Partager