Page 1 sur 1

Problème de LIMIT avec ORDER BY et jointures multiples

Posté : 13 mars 2011, 12:11
par Lefebvresdesigns
Bonjour la communauté,
Aujourd'hui j'ai un problème je souhaite paginer le nombre d'enregistrements à afficher sur cette page : http://lefebvresbook.monespace.net/infographie.php

J'ai trois tables liées dans cette requête :
  • infographisme qui comporte les infos principales sur mes projets de créations
  • captures qui comporte les différentes captures de mes créations et qui est liée à la table infographisme par le champ parentcaptureID au champ ID de la table infographisme
  • notesinfo qui comporte les notes que les visiteurs donnent à mes création et qui est reliée par le champ IDparentnote au champ ID de la table infographisme
Lors de la pagination un problème est survenu, la close LIMIT ne fonctionne pas voici ma requête (il y a plusieurs requêtes car l'affichage est triable) :

<?php 
  $pageon = 2;
  $pageoff = 1150;
  mysql_select_db($database_portfoliodata, $portfoliodata);
	if (isset($_POST['trier'])){ 
  		if (($_POST['trier'])=='notes-decroissantes'){
  			$infographinfos = "SELECT Nom_projet, Capture, note, infographisme.ID, Date_projet, Short_description, ROUND(AVG(`note`)) AS Moyenne FROM infographisme LEFT OUTER JOIN notesinfo ON IDparentnote = infographisme.ID LEFT OUTER JOIN captures ON parentcaptureID = infographisme.ID GROUP BY infographisme.ID ORDER BY Moyenne DESC LIMIT $pageon,$pageoff";}
  		elseif (($_POST['trier'])=='notes-croissantes'){
  			$infographinfos = "SELECT Nom_projet, Capture, note, infographisme.ID, Date_projet, Short_description, ROUND(AVG(`note`)) AS Moyenne FROM infographisme LEFT OUTER JOIN notesinfo ON IDparentnote = infographisme.ID LEFT OUTER JOIN captures ON parentcaptureID = infographisme.ID GROUP BY infographisme.ID ORDER BY Moyenne LIMIT $pageon,$pageoff";}
  		elseif (($_POST['trier'])=='noms-A-Z'){
  			$infographinfos = "SELECT Nom_projet, Capture, note, infographisme.ID, Date_projet, Short_description, ROUND(AVG(`note`)) AS Moyenne FROM infographisme LEFT OUTER JOIN notesinfo ON IDparentnote = infographisme.ID LEFT OUTER JOIN captures ON parentcaptureID = infographisme.ID GROUP BY infographisme.ID ORDER BY Nom_projet LIMIT $pageon,$pageoff";}
  		elseif (($_POST['trier'])=='noms-Z-A'){
  			$infographinfos = "SELECT Nom_projet, Capture, note, infographisme.ID, Date_projet, Short_description, ROUND(AVG(`note`)) AS Moyenne FROM infographisme LEFT OUTER JOIN notesinfo ON IDparentnote = infographisme.ID LEFT OUTER JOIN captures ON parentcaptureID = infographisme.ID GROUP BY infographisme.ID ORDER BY Nom_projet DESC LIMIT $pageon,$pageoff";}
  		elseif (($_POST['trier'])=='plus-anciennes'){
  			$infographinfos = "SELECT Nom_projet, Capture, note, infographisme.ID, Date_projet, Short_description, ROUND(AVG(`note`)) AS Moyenne FROM infographisme LEFT OUTER JOIN notesinfo ON IDparentnote = infographisme.ID LEFT OUTER JOIN captures ON parentcaptureID = infographisme.ID GROUP BY infographisme.ID ORDER BY Date_projet LIMIT $pageon,$pageoff";}
  		elseif (($_POST['trier'])=='plus-recentes'){
  			$infographinfos = "SELECT Nom_projet, Capture, note, infographisme.ID, Date_projet, Short_description, ROUND(AVG(`note`)) AS Moyenne FROM infographisme LEFT OUTER JOIN notesinfo ON IDparentnote = infographisme.ID LEFT OUTER JOIN captures ON parentcaptureID = infographisme.ID GROUP BY infographisme.ID ORDER BY Date_projet DESC LIMIT $pageon,$pageoff";}
	}
	else {
		$infographinfos = "SELECT Nom_projet, Capture, note, infographisme.ID, Date_projet, Short_description, ROUND(AVG(`note`)) AS Moyenne FROM infographisme LEFT OUTER JOIN notesinfo ON IDparentnote = infographisme.ID LEFT OUTER JOIN captures ON parentcaptureID = infographisme.ID GROUP BY infographisme.ID ORDER BY infographisme.ID DESC LIMIT $pageon,$pageoff";}
  $req1 = mysql_query($infographinfos) or die('Erreur SQL !<br>'.$infographinfos.'<br>'.mysql_error());
  $totalRows_infographinfos = mysql_num_rows($req1);
  while ($row_infographinfos = mysql_fetch_assoc($req1))
  {
  ?>
[/b][/color]

La clause LIMIT n'affiche jamais le nombre d'enregistrements souhaités (tests avec plusieurs nombres sans succès),
Je remercie la communauté de bien vouloir m'aider à solutionner ce problème car l'affichage risque d'être long passé un certain nombre de créations.

Re: Problème de LIMIT avec ORDER BY et jointures multiples

Posté : 13 mars 2011, 13:16
par xTG
La clause LIMIT s'utilise de la sorte : LIMIT offset, nbEnregistrements
Donc si tu mets 2,1150 tu affiches à partir du 3eme enregistrement les 1150 suivants.

Re: Problème de LIMIT avec ORDER BY et jointures multiples

Posté : 13 mars 2011, 13:52
par Lefebvresdesigns
La clause LIMIT s'utilise de la sorte : LIMIT offset, nbEnregistrements
Donc si tu mets 2,1150 tu affiches à partir du 3eme enregistrement les 1150 suivants.
Je suis sincèrement désolais d'avoir dérangé la communauté pour rien car en fait je ne disposais que de 3 enregistrements sur ma table de test j'ai corrigé le problème et cela fonctionne à merveille (à noter au passage que certains champs tel que Short_description on été modifié par le champ entier tronqué) voici le code :
  <?php
  $setnbon = 0;
  $setnboff = 4;
  mysql_select_db($database_portfoliodata, $portfoliodata);
        if (isset($_POST['trier'])){
                if (($_POST['trier'])=='notes-decroissantes'){
                        $infographinfos = "SELECT Nom_projet, Capture, note, infographisme.ID, Date_projet, Description, ROUND(AVG(`note`)) AS Moyenne FROM infographisme LEFT OUTER JOIN notesinfo ON IDparentnote = infographisme.ID LEFT OUTER JOIN captures ON parentcaptureID = infographisme.ID GROUP BY infographisme.ID ORDER BY Moyenne DESC LIMIT $setnbon,$setnboff";}
                elseif (($_POST['trier'])=='notes-croissantes'){
                        $infographinfos = "SELECT Nom_projet, Capture, note, infographisme.ID, Date_projet, Description, ROUND(AVG(`note`)) AS Moyenne FROM infographisme LEFT OUTER JOIN notesinfo ON IDparentnote = infographisme.ID LEFT OUTER JOIN captures ON parentcaptureID = infographisme.ID GROUP BY infographisme.ID ORDER BY Moyenne LIMIT $setnbon,$setnboff";}
                elseif (($_POST['trier'])=='noms-A-Z'){
                        $infographinfos = "SELECT Nom_projet, Capture, note, infographisme.ID, Date_projet, Description, ROUND(AVG(`note`)) AS Moyenne FROM infographisme LEFT OUTER JOIN notesinfo ON IDparentnote = infographisme.ID LEFT OUTER JOIN captures ON parentcaptureID = infographisme.ID GROUP BY infographisme.ID ORDER BY Nom_projet LIMIT $setnbon,$setnboff";}
                elseif (($_POST['trier'])=='noms-Z-A'){
                        $infographinfos = "SELECT Nom_projet, Capture, note, infographisme.ID, Date_projet, Description, ROUND(AVG(`note`)) AS Moyenne FROM infographisme LEFT OUTER JOIN notesinfo ON IDparentnote = infographisme.ID LEFT OUTER JOIN captures ON parentcaptureID = infographisme.ID GROUP BY infographisme.ID ORDER BY Nom_projet DESC LIMIT $setnbon,$setnboff";}
                elseif (($_POST['trier'])=='plus-anciennes'){
                        $infographinfos = "SELECT Nom_projet, Capture, note, infographisme.ID, Date_projet, Description, ROUND(AVG(`note`)) AS Moyenne FROM infographisme LEFT OUTER JOIN notesinfo ON IDparentnote = infographisme.ID LEFT OUTER JOIN captures ON parentcaptureID = infographisme.ID GROUP BY infographisme.ID ORDER BY Date_projet LIMIT $setnbon,$setnboff";}
                elseif (($_POST['trier'])=='plus-recentes'){
                        $infographinfos = "SELECT Nom_projet, Capture, note, infographisme.ID, Date_projet, Description, ROUND(AVG(`note`)) AS Moyenne FROM infographisme LEFT OUTER JOIN notesinfo ON IDparentnote = infographisme.ID LEFT OUTER JOIN captures ON parentcaptureID = infographisme.ID GROUP BY infographisme.ID ORDER BY Date_projet DESC LIMIT $setnbon,$setnboff";}
        }
        else {
                $infographinfos = "SELECT Nom_projet, Capture, note, infographisme.ID, Date_projet, Description, ROUND(AVG(`note`)) AS Moyenne FROM infographisme LEFT OUTER JOIN notesinfo ON IDparentnote = infographisme.ID LEFT OUTER JOIN captures ON parentcaptureID = infographisme.ID GROUP BY infographisme.ID ORDER BY infographisme.ID DESC LIMIT $setnbon,$setnboff";}
  $req1 = mysql_query($infographinfos) or die('Erreur SQL !<br>'.$infographinfos.'<br>'.mysql_error());
  $totalRows_infographinfos = mysql_num_rows($req1);
  while ($row_infographinfos = mysql_fetch_assoc($req1))
  {
  ?>
  <?php 
	$texte_a_tronquer = $row_infographinfos['Description'];
	$nb_maxi_caracteres = 240 ;
	if (strlen($texte_a_tronquer) > $nb_maxi_caracteres){
		$tronque = substr($texte_a_tronquer, 0, $nb_maxi_caracteres);
		$position_espace = strrpos($tronque, " ");
		$texte = substr($tronque, 0, $position_espace); 
		$tronque_finie = $texte."...";}
	else $tronque_finie = $row_infographinfos['Description'];
  ?>


Toutefois j'avais lu sur un site de confiance qu'il fallait commencer le nombre d'enregistrement à 1 et non 0, hors ce fut l'inverse.
Je finirais par remercier toutefois la communauté PHP France qui m'a aidé dans chacune de mes difficultés rencontrées,
=D> VIVE LA COMMUNAUTÉ PHP FRANCE ! :D <3