comment paginer un objet ?
Bonjour,
je suis en train de remplacer une pagination qui fonctionnait très bien avec un array_chunk sur un $tableau...par une grosse bouse avec un compteur $i sur un objet.
j'avais commencé par indexer les éléments (solution1) mais des valeurs supprimées dans le .xml source me bouffent une partie des résultats.
donc solution2...mais là je sèche !
Merci à toute aide
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
|
if(!isset($_GET['offset']) or $offset=='' or $offset!="all"){ # si pagination activée
# solution 1 : marche mais bouffe la dernière page si trou entre les id (300 à 400)
/*
for($i=$offset; $i<=($offset+$perPage); ++$i){
$temp = $obj->xpath('//data[@number='.$i.']'); # passe de l'objet au tableau
if (array_key_exists(0,$temp)){
$id = $temp[0]['number'];
$slice[(string)$id]=$temp[0]; #remplissage du tableau paginé
}
}*/
#solution2 : foreach avec compteur
$i=0;
foreach($obj as $v ){
if($i<=$perPage){ # pas bien
$temp = $v->xpath('//data[@number='.$i.']'); # passe de l'objet au tableau
if (array_key_exists(0,$temp)){
$id = $temp[0]['number'];
$slice[(string)$id]=$temp[0]; #remplissage du tableau paginé
}
}
++$i;
}
}else{ $slice = $obj;} # si aucune pagination ( show "all")
# construction pagination
$temp=$offset+$perPage;
if($offset!=0 or $offset="all" or $offset>$this->XoffsetSizof){ # début pagination; ''="all"
$html.= '<a href="'.$string.'&offset=0">⏮</a>
<a href="'.$string.'&offset='.($offset-$perPage).'">◀</a>';
}
$html.=' page '.(round($offset/$perPage)+1).'/'.(round($this->XoffsetSizof/$perPage)+1). " ";
if($offset <= $this->XoffsetSizof){
$html.= '<a href="'.$string.'&offset='.$temp.'">►</a>
<a href="'.$string.'&offset='.$this->XoffsetSizof.'">⏭</a>';
}
$html.= '<a href="'.$string.'&offset=all" title="désactiver la pagination (tout afficher)"> ▽</a>
</div>';
# construction $out
$out['slice']=$slice; #le tableau paginé
$out['navigation']=$html;
$out['nbresult']=$offset;
$out['sizeof']=$this->XoffsetSizof;
return $out;
} |
PS: l'objet est fourni par simpleXML, vaut-il mieux travailler avec DOM pour faire un CRUD à base de fichiers XML ?
Un grand merci aux contributeurs et une belle fin de journée à tout le monde ;)