aide pagination avec group by et class
Bonjour,
je souhaite faire une pagination sur une page qui affiche des elements d"une base avec fonction pdo et un group by car dans ma base j'ai plusieurs service identique mais a differents ville.
voici ce que j'ai fait pour le moment.
je souhaite plutot utiliser la class que j'ai trouver pour la pagination que voit-ci.
avec la mise en pratique de la classe comme exemple :
Code:
1 2 3 4 5 6 7 8 9
| $nbElements = $pdo->exec("SELECT COUNT(*) FROM articles");
$PaginationFinal = new Pagination();
$PaginationFinal->setCurrentPage($_GET['page']);
$PaginationFinal->setInnerLinks(3);
$PaginationFinal->setNbElementsInPage(20);
$PaginationFinal->setNbMaxElements($nbElements);
$paginationFinal = $PaginationFinal->renderBootstrapPagination();
echo $paginationFinal; |
chez moi ca fonctionne presque.
j'ai l'affichage du bon nombre de page mais c'est pour les données ou tout s'affiche sur la 1 er page je sais que c'est en rapport avec mes fonctions.
je sais que c'est par rapport à mes boucle avec la fonction select pour l'affichage des données car je dois passer la 1er page et la dernière page mais comment faire dans mon cas.
voici ce que j'ai mit en bas de ma page et je ne sais pas quoi mettre dans innerlinks :
Code:
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
|
if(empty($_GET['pivot']))
{
/**si pas de get c'est une sous categorie**/
foreach($managerService->getListGroupByPivot() as $lstAll)
{
$body.='
<div class="col-md-4">
<a href="services-publics-annuaire-details.php?sub_pivot='.$lstAll->getPivot().'">
<div class="list-mig-like-com com-mar-bot-30">
<div class="list-mig-lc-con">
<h5>'.$lstAll->getPivot().'</h5>
<h6>'.$lstAll->getTypeService().'</h6>
<p>'.$lstAll->getCommune().'</p>
</div>
</div>
</a>
</div>';
}
}
elseif($_GET['bymenu']="yes")
{
/**sinon get c'est une categorie principal**/
foreach($managerService->getListGroupByNamePivot($_GET['pivot']) as $lstAll)
{
$body.='a-hidden="true"></i> <i class="fa fa-star-o" aria-hidden="true"></i> </div>-->
<div class="col-md-4">
<a href="services-publics-annuaire-details.php?sub_pivot='.$lstAll->getPivot().'">
<div class="list-mig-like-com com-mar-bot-30">
<div class="list-mig-lc-con">
<h5>'.$lstAll->getPivot().'</h5>
<h6>'.$lstAll->getTypeService().'</h6>
<p>'.$lstAll->getCommune().'</p>
</div>
</div>
</a>
</div>';
}
}
else
{
/*sinon la page s'affiche au clic du menu de gauche sur les categories principales*/
foreach($managerService->getListGroupByNamePivot($_GET['pivot']) as $lstAll)
{
$body.='
<div class="col-md-4">
<a href="?pivot='.$lstAll->getPivot().'">
<div class="list-mig-like-com com-mar-bot-30">
<h5>'.$lstAll->getPivot().'</h5>
<h6>'.$lstAll->getTypeService().'</h6>
<p>'.$lstAll->getCommune().'</p>
</div>
</div>
</a>
</div>';
}
}
$body.='
<br />';
$nbElements = $managerService->countGroupByPivot();
$PaginationFinal = new Pagination();
$PaginationFinal->setCurrentPage($_GET['page']);
$PaginationFinal->setInnerLinks(3);
$PaginationFinal->setNbElementsInPage(12);
$PaginationFinal->setNbMaxElements($nbElements);
$paginationFinal = $PaginationFinal->renderBootstrapPagination();
$body.=''.$paginationFinal.''; |
voici mes function pdo au cas ou :
Code:
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
| public function getListGroupByPivot($debut = -1, $limite = -1)
{
$listeService = array();
$sql = 'SELECT id_service,id_data,code_insee,date_maj,pivot FROM service group by pivot';
if ($debut != -1 || $limite != -1)
$sql .= ' LIMIT ' . (int) $debut . ', ' . (int) $limite;
$requete = $this->db->query($sql);
while ($Service = $requete->fetch(PDO::FETCH_ASSOC))
$listeService[] = new Service ($Service);
$requete->closeCursor();
return $listeService;
}
public function getListGroupByNamePivot($pivot)
{
$listeService = array();
$sql = 'SELECT id_service,id_data,code_insee,date_maj,pivot FROM service where pivot="'.$pivot.'"';
$requete = $this->db->query($sql);
while ($Service = $requete->fetch(PDO::FETCH_ASSOC))
$listeService[] = new Service ($Service);
$requete->closeCursor();
return $listeService;
} |
la class pagination :
Code:
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 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206
|
<?php
/**
* Generate a Pagination (with Bootstrap).
*
* @version 1.0.0
* @link https://github.com/Zheness/Pagination/ Github Repo
* @author Zheness
*/
class Pagination {
private $nbMaxElements = 200;
private $nbElementsInPage = 20;
private $currentPage = 1;
private $url = '?page={i}';
private $innerLinks = 2;
private $linksSeparator = '...';
/**
* Set the maximum elements in your database. (Or other way)
*
* Example :
*
* I have 200 articles in my database, so I type :
*
* $Pagination->setNbMaxElements(200);
*
* @param int $int The maximum elements
* @author Zheness
*/
public function setNbMaxElements($int) {
$this->nbMaxElements = (int) $int;
}
/**
* Set the number of elements to display in the page.
*
* Example :
*
* I would display 20 articles per pages, so I type :
*
* $Pagination->setNbElementsInPage(20);
*
* @param int $int The number of elements
* @author Zheness
*/
public function setNbElementsInPage($int) {
$this->nbElementsInPage = (int) $int;
}
/**
* Set the current page
*
* Example :
*
* The current page is the 5, so I type :
*
* $Pagination->setCurrentPage(5);
*
* @param int $int The current page
* @author Zheness
*/
public function setCurrentPage($int) {
$this->currentPage = (int) $int;
}
/**
* Set the url in the links. You MUST include "{i}" where you want display the number of the page.
*
* Example :
*
* I would display my articles on "articles.php?page=X" where X is the number of page. So I type :
*
* $Pagination->setUrl("articles.php?page={i}");
*
* Why {i} ? Because the number of page can be placed everywhere. If you have your url like this "articles/month/08/page/X/sort/date-desc", you can place {i} instead of X.
*
* @param string $string The url in the link
* @author Zheness
*/
public function setUrl($string) {
$this->url = $string;
}
/**
* Set the number of links before and after the current page
*
* Example :
*
* The current page is the 5, and I want 3 links after and before, so I type :
*
* $Pagination->setInnerLinks(3);
*
* @param int $int The number of links before and after the current links
* @author Zheness
*/
public function setInnerLinks($int) {
$this->innerLinks = (int) $int;
}
/**
* Set the separtor between links.
*
* Example :
*
* I would display " - " between my links, so I type :
*
* $Pagination->setLinksSeparator(" - ");
*
* By default "..." is display.
*
* @param string $string The url in the link
* @author Zheness
*/
public function setLinksSeparator($string) {
$this->linksSeparator = $string;
}
/**
* This is the function to call for render the Pagination.
*
* You have just to configure the options and call this function.
*
* @return string The HTML Pagination (it use Bootstrap)
* @author Zheness
*/
public function renderBootstrapPagination() {
$array_pagination = $this->generateArrayPagination();
$html = $this->generateHtmlPagination($array_pagination);
return $html;
}
/**
* Generate the Pagination in array.
*
* @return array Each value is the link to display.
* @author Zheness
*/
private function generateArrayPagination() {
$array_pagination = array();
$keyArray = 0;
$subLinks = $this->currentPage - $this->innerLinks;
$nbLastLink = ceil($this->nbMaxElements / $this->nbElementsInPage);
if ($this->currentPage > 1) {
$array_pagination[$keyArray++] = '<a href="' . str_replace('{i}', 1, $this->url) . '">1</a>';
}
if ($subLinks > 2) {
$array_pagination[$keyArray++] = $this->linksSeparator;
}
for ($i = $subLinks; $i < $this->currentPage; $i++) {
if ($i >= 2) {
$array_pagination[$keyArray++] = '<a href="' . str_replace('{i}', $i, $this->url) . '">' . $i . '</a>';
}
}
$array_pagination[$keyArray++] = '<b>' . $this->currentPage . '</b>';
for ($i = ($this->currentPage + 1); $i <= ($this->currentPage + $this->innerLinks); $i++) {
if ($i < $nbLastLink) {
$array_pagination[$keyArray++] = '<a href="' . str_replace('{i}', $i, $this->url) . '">' . $i . '</a>';
}
}
if (($this->currentPage + $this->innerLinks) < ($nbLastLink - 1)) {
$array_pagination[$keyArray++] = $this->linksSeparator;
}
if ($this->currentPage != $nbLastLink) {
$array_pagination[$keyArray++] = '<a href="' . str_replace('{i}', $nbLastLink, $this->url) . '">' . $nbLastLink . '</a>';
}
return $array_pagination;
}
/**
* Generate the HTML pagination with the array in parameter
*
* @param array $array_pagination The array generate with previous function.
* @return string Pagination in HTML. Use Bootstrap
* @author Zheness
*/
private function generateHtmlPagination($array_pagination) {
$html = "";
$html .= '<div class="pagination">';
$html .= '<ul>';
if ($this->nbMaxElements) {
foreach ($array_pagination as $v) {
if ($v == $this->linksSeparator) {
$html .= '<li class="disabled"><span>' . $this->linksSeparator . '</span></li>';
} else if (preg_match("/<b>(.*)<\/b>/i", $v)) {
$html .= '<li class="active"><span>' . strip_tags($v) . '</span></li>';
} else {
$html .= '<li>' . $v . '</li>';
}
}
} else {
$html .= '<li class="active"><span>1</span></li>';
}
$html .= '</ul>';
$html .= '</div>';
return $html;
}
} |
merci pour votre aide