IdentifiantMot de passe
Loading...
Mot de passe oublié ?Je m'inscris ! (gratuit)
Navigation

Inscrivez-vous gratuitement
pour pouvoir participer, suivre les réponses en temps réel, voter pour les messages, poser vos propres questions et recevoir la newsletter

PHP & Base de données Discussion :

aide pagination avec group by et class


Sujet :

PHP & Base de données

  1. #1
    Membre éprouvé
    Inscrit en
    Janvier 2008
    Messages
    1 159
    Détails du profil
    Informations forums :
    Inscription : Janvier 2008
    Messages : 1 159
    Par défaut 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 : Sélectionner tout - Visualiser dans une fenêtre à part
    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 : 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
     
    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 : 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
    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 : 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
    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

  2. #2
    Membre éprouvé
    Inscrit en
    Janvier 2008
    Messages
    1 159
    Détails du profil
    Informations forums :
    Inscription : Janvier 2008
    Messages : 1 159
    Par défaut
    merci je joins les functions :

    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
    public function countGroupByPivot()
    	{
    		return $this->db->query('SELECT COUNT(DISTINCT pivot) FROM service')->fetchColumn();
    	}
     
    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;
     
    }


    pour la fonction getListGroupByPivotLocal() c'est getListGroupByPivot() excuse moi c'est une erreur de tappe.

    j'utilise ainsi deux fonctions.j'ai modifier dans le code aussi au cas ou.

    Alors j'ai verifier avec phpmyadmin la fonction me retourne qu'il y'a 119 resultat de pivot local avec le group by mais plusieurs enregistrement.

    COUNT(*)
    43
    151
    198
    53
    211
    367
    83
    39
    3
    245
    250
    82
    353
    9
    1640
    3
    27
    284
    181
    282
    43
    94
    161
    183
    201
    165
    159
    1753
    589
    122
    j'ai donc utiliser distinct dans le countpivot() par contre j'ai fait cela pour le moment pour la 1er boucle.

    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
     
    $nbElements = $managerService->countGroupByPivot();
     
    $PaginationFinal = new Pagination();
    $PaginationFinal->setCurrentPage($_GET['page']);
    $PaginationFinal->setInnerLinks(12);
    $PaginationFinal->setNbElementsInPage(12);
    $PaginationFinal->setNbMaxElements($nbElements);
    $paginationFinal = $PaginationFinal->renderBootstrapPagination();
     
    /**si pas de get affichage de base des categorie**/
    								foreach($managerServicePublic->getListGroupByPivot(1,12) 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>';
     
    										}
    										$body.=''.$paginationFinal.'';
     
    							}

    maitenant a l'affichage il affiche le bon nombre de page mais quand je clic sur la page 2 cela ne change pas le contenu afficher.

    merci pour ce que je souhaite :

    j'ai dans ma base de données 1290 enregistrement de servicepublic tel que ex mairie,...

    Dans plusieurs villes.J'ai un champ pivot m'affiche les type EX "mairie",..

    je peux donc avoir le pivot mairie au moins 200 ou 300 fois dans la base.

    Je souhaite afficher une page qui liste les enregsitrements par pivot.

    ex :

    maririe / prefecture / ...

    Donc je fais un group by pour cela.

    les visiteurs clic ensuite sur le pivot desiré et tombe sur tout les enregistrements de ce pivot

    ex il a cliquer sur mairie il tombe sur une page avec tout les enregistrements contenant mairie en pivot.

    J'ai donc fais la fonction getListGroupByNamePivot($pivot).

    et après il peut cliquer sur un pivot maririe de perpignan et redirigier sur une page détails.

    concernant les erreurs de tables exemple c'est parce que je retravaille avant d'envoyer pour envoyer les vrais nom de la table, on me l'avait recommander desole.

    pense tu qu'il y' a des changement a faire selon ce que j'ai explique ?

    merci pour ton aide

    -

Discussions similaires

  1. [MySQL] Pagination avec class pdo / mysql
    Par kate59 dans le forum PHP & Base de données
    Réponses: 14
    Dernier message: 15/03/2019, 00h16
  2. Pagination avec une classe qui hérite de DataSource
    Par Kilik75 dans le forum Angular
    Réponses: 0
    Dernier message: 17/12/2018, 22h39
  3. [Python 3.X] AIDE PROJET AVEC CLASSES
    Par Miirage25191 dans le forum Général Python
    Réponses: 3
    Dernier message: 22/12/2014, 09h39
  4. [MySQL] Class pagination avec problème d'affichage
    Par eltyty dans le forum PHP & Base de données
    Réponses: 11
    Dernier message: 09/07/2011, 11h48
  5. [aide]probléme avec l'instruction group by
    Par diden138 dans le forum Bases de données
    Réponses: 4
    Dernier message: 25/05/2007, 07h09

Partager

Partager
  • Envoyer la discussion sur Viadeo
  • Envoyer la discussion sur Twitter
  • Envoyer la discussion sur Google
  • Envoyer la discussion sur Facebook
  • Envoyer la discussion sur Digg
  • Envoyer la discussion sur Delicious
  • Envoyer la discussion sur MySpace
  • Envoyer la discussion sur Yahoo