Bonjour,
Si la table est assez importante, la pagination est trop longue, Cmt faire pour renvoyer dans le model de la requete SQL les 2 parametres LIMIT de pagination ?
Bonjour,
Si la table est assez importante, la pagination est trop longue, Cmt faire pour renvoyer dans le model de la requete SQL les 2 parametres LIMIT de pagination ?
Il faut pour cela créer une méthode
dans votre classe modèle
Code php : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4 public function findAllLimit($offset,$limit){ return $this->findMany('SELECT * FROM maTache LIMIT '.(int)$offset.','.(int)$limit); }
return $this->findMany('SELECT * FROM maTache LIMIT '.(int)$offset.','.(int)$limit);
cmt faire pour envoyer les 2 paramètres dans le model SQL ?.
Ces parametres se trouvent dans le main du module pagination, ils ont comme noms iMin et ipart
public function findAllLimit($iMin,$iPart){
Dans votre action (methode _list) du module article
vous faites actuellemetn quelque chose comme
Code php : Sélectionner tout - Visualiser dans une fenêtre à part
1
2 $tArticle=model_article::getInstance()->findAll();
remplacez le par
où $iMin et $iPart auront préalablement été récupéré
Code php : Sélectionner tout - Visualiser dans une fenêtre à part
1
2 $tArticle=model_article::getInstance()->findAllLimit($iMin,$iPart);
je l'avais fait, les param n'etaient reconduits.
En fait si c'est aussi long (15s pour afficher page) dans mon sql il se trouve un where et ma table est assez conséquente.
si j'enleve se where cad de charger mon tableau complet, puis ensuite de trier seulement les enregs sélectionnés.
cmt faire pour trier sur mes champs à la 2e étape ?
ce n'est pas le WHERE mais ORDER BY
postez le code de votre fichier main (methode _list) + la classe model_article et je vous indiquerai
utilsez de préférence les balises codes
Model
-----
// Si tous les enregs
public function findAll(){
return $this->findMany('
SELECT * FROM '.$this->sTable.'
LEFT OUTER JOIN c_not
ON par_che_id=not_che_id
LEFT OUTER JOIN c_che
ON par_che_id=che_id
LEFT OUTER JOIN c_fav
ON par_dtm = fav_dtm
AND par_reu = fav_reu
AND par_crs = fav_crs
WHERE par_jec in(1,2,3,4)
ORDER BY par_dtm DESC,fav_heu ASC
');
}
// selection d'une journée avec le calendrier
public function findByDate($datSel){
return $this->findMany('
SELECT * FROM '.$this->sTable.'
LEFT OUTER JOIN c_not
ON par_che_id=not_che_id
LEFT OUTER JOIN c_che
ON par_che_id=che_id
LEFT OUTER JOIN c_fav
ON par_dtm = fav_dtm
AND par_reu = fav_reu
AND par_crs = fav_crs
AND fav_dtm=?
WHERE par_jec in(1,2,3,4)
AND par_dtm=?
ORDER BY par_dtm DESC,fav_heu ASC '
,$datSel,$datSel);
}
Main
----
public function _list(){
$iAnnee=_root::getParam('annee');
$iMois=_root::getParam('mois');
$iJour=_root::getParam('jour');
if(!$iAnnee){ // si pas de date
$tC_par=model_c_par::getInstance()->findAll();
}else{ // si une date est selectionne
$datSel=$iAnnee.$iMois.$iJour;
$tC_par=model_c_par::getInstance()->findByDate($datSel);
}
$oView=new _view('c_par::list');
$oView->tC_par=$tC_par;
$oModulePagination=new module_pagination;
$oModulePagination->setModuleAction('c_par::list');
$oModulePagination->setParamPage('page');
$oModulePagination->setLimit(5);
$oModulePagination->setPage( _root::getParam('page') );
$oModulePagination->setTab( $tC_par );
$oView=new _tpl('c_par::list');
$oView->tC_par=$oModulePagination->getPageElement();
$oView->oModulePagination=$oModulePagination->build();
//Calendrier
$oModuleCalendrier=new module_calendrier;
$oModuleCalendrier->sModuleAction='c_par::list'; //indiquer le couple module::action de la page parente
$oView->oModuleCalendrier=$oModuleCalendrier->build(); //la methode build() doit retourner un objet _view
$this->oLayout->add('main',$oView);
}
Cmt je passe la date(datSel) sur les liens quand une date est sélectionnée ?
le list du pagination
------------------
<div class="pagination">
<ul>
<?php
if($this->iPage==0) $this->iPage=1;
$pagEnCours=_root::getParam('page');
if($pagEnCours==0) $pagEnCours=1;
//_root::getParam('annee');
echo "pagEnCours:$pagEnCours - iMin:$this->iMin - iPart:$this->iPart - iPagMaxPag:$this->iPagMaxPag - iPagMaxTot:$this->iPagMaxTot - iPage:$this->iPage - iLigLimitPag:$this->iLigLimitPag";
echo "<br>";
// --Affichage page Début
if($this->iMin >0) { $i=1?>
<?php $tParam=$this->tParam ?>
<?php $tParam[$this->sParamPage]=$i?>
<li <?php if($i==1)>class="selectionne"<?php endif;?>><a href="<?php echo _root::getLink($this->sModuleAction,$tParam) ?>">Début </a></li>
<?php if($this->iPage>=$this->iPagMaxPag) $this->iPage=$this->iPage-($this->iPagMaxPag/2);
}
// --Affichage page Précédent
if($this->iMin >0) { $i=$pagEnCours-1?>
<?php $tParam=$this->tParam ?>
<?php $tParam[$this->sParamPage]=$i?>
<li <?php if($i==$pagEnCours-1)>class="selectionne"<?php endif;?>><a href="<?php echo _root::getLink($this->sModuleAction,$tParam) ?>"> Précédent</a></li>
<?php }?>
<?php
// ----Pages de x à y
for($i=$this->iPage;$i<=($this->iPagMaxPag+$this->iPage)and($i<=$this->iPagMaxTot);$i++)>
<?php $tParam=$this->tParam ?>
<?php $tParam[$this->sParamPage]=$i?>
<li <?php if($i==$pagEnCours)>class="selectionne"<?php endif;?>><a href="<?php echo _root::getLink($this->sModuleAction,$tParam) ?> "> <?php echo $i?></a></li>
<?php endfor?>
<?php
// ---Affichage page Suite
if($pagEnCours < $this->iPagMaxTot) { $i=$pagEnCours+1?>
<?php $tParam=$this->tParam;?>
<?php $tParam[$this->sParamPage]=$i?>
<li <?php if($i==$pagEnCours+1)>class="selectionne"<?php endif;?>><a href="<?php echo _root::getLink($this->sModuleAction,$tParam) ?>"> Suite</a></li>
<?php }?>
<?php
// ---Affichage page Fin
if($pagEnCours < $this->iPagMaxTot) { $i=$this->iPagMaxTot?>
<?php $tParam=$this->tParam;?>
<?php $tParam[$this->sParamPage]=$i?>
<li <?php if($i==$this->iPagMaxTot)>class="selectionne"<?php endif;?>><a href="<?php echo _root::getLink($this->sModuleAction,$tParam) ?>"> Fin</a></li>
<?php }?>
</ul>
</div>
<!-- Affichage des éléments pages -->
Page <?php if($pagEnCours==1) {
echo '1//'. $this->iPagMaxTot;
}else{
echo $pagEnCours .'/'. $this->iPagMaxTot;
}
Essayez ceci:
Code php : 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 <?php // Si tous les enregs public function findAllLimit($offset,$limit){ return $this->findMany(' SELECT * FROM '.$this->sTable.' LEFT OUTER JOIN c_not ON par_che_id=not_che_id LEFT OUTER JOIN c_che ON par_che_id=che_id LEFT OUTER JOIN c_fav ON par_dtm = fav_dtm AND par_reu = fav_reu AND par_crs = fav_crs WHERE par_jec in(1,2,3,4) ORDER BY par_dtm DESC,fav_heu ASC LIMIT '.(int)$offset.','.(int)$limit.' '); } public function findAllCount(){ $oRow=$this->findOneSimple('SELECT count(*) as total FROM '.$this->sTable.' LEFT OUTER JOIN c_not ON par_che_id=not_che_id LEFT OUTER JOIN c_che ON par_che_id=che_id LEFT OUTER JOIN c_fav ON par_dtm = fav_dtm AND par_reu = fav_reu AND par_crs = fav_crs WHERE par_jec in(1,2,3,4) '); return (int)$oRow->total; } // selection d'une journée avec le calendrier public function findByDateLimit($datSel,$offset,$limit){ return $this->findMany(' SELECT * FROM '.$this->sTable.' LEFT OUTER JOIN c_not ON par_che_id=not_che_id LEFT OUTER JOIN c_che ON par_che_id=che_id LEFT OUTER JOIN c_fav ON par_dtm = fav_dtm AND par_reu = fav_reu AND par_crs = fav_crs AND fav_dtm=? WHERE par_jec in(1,2,3,4) AND par_dtm=? ORDER BY par_dtm DESC,fav_heu ASC LIMIT '.(int)$offset.','.(int)$limit.' ' ,$datSel,$datSel); } public function findByDateCount($datSel){ $oRow=$this->findOneSimple('SELECT count(*) as total FROM '.$this->sTable.' LEFT OUTER JOIN c_not ON par_che_id=not_che_id LEFT OUTER JOIN c_che ON par_che_id=che_id LEFT OUTER JOIN c_fav ON par_dtm = fav_dtm AND par_reu = fav_reu AND par_crs = fav_crs AND fav_dtm=? WHERE par_jec in(1,2,3,4) AND par_dtm=? ',$datSel,$datSel); return (int)$oRow->total; } //----------------------- //MAIN public function _list(){ $iAnnee=_root::getParam('annee'); $iMois=_root::getParam('mois'); $iJour=_root::getParam('jour'); //pagination $limit=5; $offset=_root::getParam('page',0)*$limit; if(!$iAnnee){ // si pas de date $tC_par=model_c_par::getInstance()->findAllLimit($offset,$limit); $iMax=model_c_par::getInstance()->findAllCount(); }else{ // si une date est selectionne $datSel=$iAnnee.$iMois.$iJour; $tC_par=model_c_par::getInstance()->findByDateLimit($datSel,$offset,$limit); $iMax=model_c_par::getInstance()->findByDateCount($datSel); } $oView=new _view('c_par::list'); $oView->tC_par=$tC_par; $oModulePagination=new module_pagination; $oModulePagination->setModuleAction('c_par::list'); $oModulePagination->setParamPage('page'); $oModulePagination->setLimit(5); $oModulePagination->setPage( _root::getParam('page') ); $oModulePagination->setMax( $iMax ); $oView->oModulePagination=$oModulePagination->build(); //Calendrier $oModuleCalendrier=new module_calendrier; $oModuleCalendrier->sModuleAction='c_par::list'; //indiquer le couple module::action de la page parente $oView->oModuleCalendrier=$oModuleCalendrier->build(); //la methode build() doit retourner un objet _view $this->oLayout->add('main',$oView); }
J'oublais, dans le module pagination, dans le fichier main.php
ajoutez cette méthode:
Code php : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3 public function setMax($iMax){ $this->iMax=$iMax; }
le temps d'affichage est le même et je n'ai plus les autres pages en pagination !!!
cmt faire pour ajouter la date dans ce lien dans le list de pagination ?
<li <?php if($i==$pagEnCours)>class="selectionne"<?php endif;?>><a href="<?php echo _root::getLink($this->sModuleAction,$tParam) ?> "> <?php echo $i?></a></li>
Code php : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3 <?php $tParam['dateSel']=_root::getParam('dateSel');?> <li <?php if($i==$pagEnCours):?>class="selectionne"<?php endif;?>><a href="<?php echo _root::getLink($this->sModuleAction,$tParam) ?> "> <?php echo $i?></a></li>
merci, je vais réviser le code pour mieux comprendre.
exemple a quoi sert les count :
findByDateCount($datSel). et findAllCount(){
le count est déja fait dans le main du pagination:
public function getPageElement(){
$tPartElement=array();
$this->iPagMaxTot=count($this->tElement);
Bonne soiree
Par defaut oui, mais dans le code que je vous ai fourni, il n'y a plus d'appel a getPageElement()
Il y a a la place setMax()
Pour éviter de recuperer tous les enregistrements
Après, il vous faut peut etre ajouter des index sur votre table (sur le champ date, les fk et les champs a trier)
Pour le temps réponse maintenant il est immédiat, mes index étaient bien créés mais je ne sais pas pk, le nombre correspondait pas à la réalité, en supprimant un index et le recréant dans chaque table tout c'est réorganisé correctement.
Dans votre génération de pagination, tous les liens des pages sont affichés en même temps, le pb si la table est importante trop de liens sont affichés.
J'ai modifié la pagination de sorte à afficher comme ceci:
Début Précédent 1 2 3 4 5 6 7 Suite Fin
Cela marche très bien quand je ne choisis pas une date du calendrier.
quand je choisis une date avec le calendrier la 1ère page est correct mais quand je veux choisir la 2ème page par exemple il affiche toutes les dates, je n'arrive pas à passer cette fameuse date choisie en paramètre pour passer dans le bon SQL !!!
Vous avez bien modifié la vue de pagination comme ceci:
Code php : Sélectionner tout - Visualiser dans une fenêtre à part
1
2 <?php $tParam['dateSel']=_root::getParam('dateSel');?> <li <?php if($i==$pagEnCours):?>class="selectionne"<?php endif;?>><a href="<?php echo _root::getLink($this->sModuleAction,$tParam) ?> "> <?php echo $i?></a></li>
1) quand je clique la date du 20131103 (voici le résultat)
datSel:20131103- - pagEnCours:1 - iMin:0 - iPart:4 - iPagMaxPag:6 - iPagMaxTot:6 - iPage:1 - iLigLimitPag:4
1 2 3 4 5 6 Suite Fin
Page 1/6
----------------------------
2) quand je clique sur la 2e page (datSel n'a pas de date)
datSel:- - pagEnCours:2 - iMin:4 - iPart:8 - iPagMaxPag:6 - iPagMaxTot:177 - iPage:1 - iLigLimitPag:4
Début Précédent 1 2 3 4 5 6 7 Suite Fin
Page 2/177
--------------
voici le list de pagination
<div class="pagination">
<ul>
<?php
if($this->iPage==0) $this->iPage=1;
$pagEnCours=_root::getParam('page');
if($pagEnCours==0) $pagEnCours=1;
$datSel=_root::getParam('annee')._root::getParam('mois')._root::getParam('jour');
// echo pour controle
echo " datSel:$datSel- - pagEnCours:$pagEnCours - iMin:$this->iMin - iPart:$this->iPart - iPagMaxPag:$this->iPagMaxPag - iPagMaxTot:$this->iPagMaxTot - iPage:$this->iPage - iLigLimitPag:$this->iLigLimitPag";
echo "<br>";
//---------
// --Affichage page Début
if($this->iMin >0) { $i=1?>
<?php $tParam=$this->tParam ?>
<?php $tParam[$this->sParamPage]=$i?>
<?php $tParam['datSel']=_root::getParam('datSel');?>
<li <?php if($i==1)>class="selectionne"<?php endif;?>><a href="<?php echo _root::getLink($this->sModuleAction,$tParam) ?>">Début </a></li>
<?php if($this->iPage>=$this->iPagMaxPag) $this->iPage=$this->iPage-($this->iPagMaxPag/2);
}
// --Affichage page Précédent
if($this->iMin >0) { $i=$pagEnCours-1?>
<?php $tParam=$this->tParam ?>
<?php $tParam[$this->sParamPage]=$i?>
<?php $tParam['datSel']=_root::getParam('datSel');?>
<li <?php if($i==$pagEnCours-1)>class="selectionne"<?php endif;?>><a href="<?php echo _root::getLink($this->sModuleAction,$tParam) ?>"> Précédent</a></li>
<?php }?>
<?php
// ----Pages de x à y
for($i=$this->iPage;$i<=($this->iPagMaxPag+$this->iPage)and($i<=$this->iPagMaxTot);$i++)>
<?php $tParam=$this->tParam ?>
<?php $tParam[$this->sParamPage]=$i?>
<?php $tParam['datSel']=_root::getParam('datSel');?>
<li <?php if($i==$pagEnCours)>class="selectionne"<?php endif;?>><a href="<?php echo _root::getLink($this->sModuleAction,$tParam) ?> "> <?php echo $i?></a></li>
<?php endfor?>
<?php
// ---Affichage page Suite
if($pagEnCours < $this->iPagMaxTot) { $i=$pagEnCours+1?>
<?php $tParam=$this->tParam;?>
<?php $tParam[$this->sParamPage]=$i?>
<?php $tParam['datSel']=_root::getParam('datSel');?>
<li <?php if($i==$pagEnCours+1)>class="selectionne"<?php endif;?>><a href="<?php echo _root::getLink($this->sModuleAction,$tParam) ?>"> Suite</a></li>
<?php }?>
<?php
// ---Affichage page Fin
if($pagEnCours < $this->iPagMaxTot) { $i=$this->iPagMaxTot?>
<?php $tParam=$this->tParam;?>
<?php $tParam[$this->sParamPage]=$i?>
<?php $tParam['datSel']=_root::getParam('datSel');?>
<li <?php if($i==$this->iPagMaxTot)>class="selectionne"<?php endif;?>><a href="<?php echo _root::getLink($this->sModuleAction,$tParam) ?>"> Fin</a></li>
<?php }?>
</ul>
</div>
<!-- Affichage des éléments pages -->
Page <?php if($pagEnCours==1) {
echo '1/'. $this->iPagMaxTot;
}else{
echo $pagEnCours .'/'. $this->iPagMaxTot;
}
J'ai peut etre une piste:
Code php : Sélectionner tout - Visualiser dans une fenêtre à part $datSel=_root::getParam('annee')._root::getParam('mois')._root::getParam('jour');
Lors de la seconde page, les variables annees/mois/jour sont vide
il faut d'abord verifier que datSel n'est pas deja renseigné
Code php : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8 if(_root::getParam('dateSel')){ //si deja dans l'url on l'utilise $datSel=_root::getParam('dateSel'); }else{ //sinon on concatene annee/mois/jour $datSel=_root::getParam('annee')._root::getParam('mois')._root::getParam('jour'); }
j'ai changé dans le list et le main du calendier de façon a fusionné les 3 champs(annee.mois.jour) dans datSel , elle devient une variable d'URL.
j'ai changé ces 2 lignes dans le list de c_par:
$datSel=_root::getParam('annee')._root::getParam('mois')._root::getParam('jour');
<?php $tParam['datSel']=_root::getParam('datSel');?>
par:
$datSel=_root::getParam('datSel');
$tParam['datSel']=$datSel;
et ca marche.
Je pense que cette ligne en rouge , tParam était vide.
Qu'est-ce que vous en pensé ?
Vous avez un bloqueur de publicités installé.
Le Club Developpez.com n'affiche que des publicités IT, discrètes et non intrusives.
Afin que nous puissions continuer à vous fournir gratuitement du contenu de qualité, merci de nous soutenir en désactivant votre bloqueur de publicités sur Developpez.com.
Partager