Des images qui font répéter le nombre de publication

Résolu
max30_3775 Messages postés 185 Statut Membre -  
jordane45 Messages postés 30426 Date d'inscription   Statut Modérateur Dernière intervention   -
Bonjour,
J'ai mes annonces qui se répètent selon le nombre d'images présentes dans une annonce donnée, et celà depuis que j'ai changé mes tables. Au départ j'avais 2 tables, maintenant j'en ai 4.
Pour éviter également des soucis de requête plus tard, je voudrais ajouter une image par défaut s'il y en a pas une.


Dans le code ça donne ça:
function afficherAnnonce(){
    global $bdd;
    $res = [];
   $sql=' SELECT  A.*
   ,C.*
   ,I.*
FROM annonces A
LEFT JOIN categories C ON C.id = A.id_categorie
INNER JOIN images_annonces I ON A.id= I.id_annonce
ORDER BY created_date DESC';
      
      
    try{
      $req = $bdd->prepare($sql);
      $req->execute();
      $data = $req->fetchAll(PDO::FETCH_ASSOC);
      //on reconstruit un array mieux organisé pour avoir les photos
      
      foreach($data as $R){
        if(!isset($res[$R['id']])){
         $res[$R['id']] = $R;
         unset($res[$R['id']]['filepath']);
        }
         $res[$R['id']]['images'][] = $R['filepath'];                                               
      }
      
    }catch(Exception $e){
        echo "Erreur dans la requête " . $sql;
    }
 // le temps des tests pour voir le résultat
    return $res;
      
  }


Configuration: Windows / Chrome 96.0.4664.45

28 réponses

  • 1
  • 2
Résumé de la discussion

Le souci principal est que les annonces se répètent selon le nombre d'images associées, après un changement des tables, et il faut aussi afficher une image par défaut lorsqu'aucune image n’est présente.
La solution consiste à remplacer l INNER JOIN des images par un LEFT JOIN et à regrouper chaque annonce avec ses images, évitant les duplications lorsque plusieurs photos existent.
Pour l'image par défaut, tester si la liste est vide et attribuer 'no_image.png' afin d'afficher systématiquement une miniature même sans photo.
En cas de tests, des var_dump et la vérification du contenu des champs 'filepath' et 'images' permettent d'ajuster la logique d'affichage et de traiter les données manquantes.

Généré automatiquement par IA
sur la base des meilleures réponses
  1. jordane45 Messages postés 30426 Date d'inscription   Statut Modérateur Dernière intervention   4 830
     
    Bonjour,

    Le problème ne se trouve à mon avis pas là ... mais plutôt dans le code que tu utilises pour afficher ta liste.

    0
  2. max30_3775 Messages postés 185 Statut Membre
     
    Bonjour, merci pour ta réponse.
    Le code pour afficher, le voilà:
    <?php foreach($anouncement as $anounce){
      echo'
      <div class="annoncepubs">';
    
    
    
        $images = !empty($anounce['images']) ? $anounce['images'] : NULL;
        if($images){
          //boucle sur les images
          echo'<div class="container">';
          echo ' <div class="slider" >';
          foreach( $images as $img ){
        
            echo '<img class ="active"
            style="width:90px; height:90px;"
             src="'.$img.'">';
              }
         echo "</div>";

    Que faudrait-il changer?
    0
    1. jordane45 Messages postés 30426 Date d'inscription   Statut Modérateur Dernière intervention   4 830
       
      Tu as des divs qui ne sont pas fermées non?
      0
  3. max30_3775 Messages postés 185 Statut Membre
     
    Toutes mes div sont fermées, en même temps j'essayais d'ajouter une image par défaut mais ça ne marchait pas non plus:
    Est-ce notre requête? car les tables sont comme indiquées dans la photo:


    <?php foreach($anouncement as $anounce){
      echo'
      <div class="annoncepubs">';
     
        $images = !empty($anounce['images']) ? $anounce['images'] : []; // array
     
        if(!empty($images) )
        {
          //boucle sur les images
          echo'<div class="container">';
          echo ' <div class="slider" >';
          foreach( $images as $img )
          {
     
            if(empty($img) ){ $img = 'Projetsite/no_image.png'; }
     
            echo '<img class ="active" style="width:90px; height:90px;" src="'.$img.'">';
              }
         echo "</div>";
    
         echo ' <div class="cont-btn" >';
         echo ' <div class="btn-nav left" > <'; echo "</div>";
         echo ' <div class="btn-nav right" > >';echo "</div>";
         echo "</div>";
         
        }
      
        
        echo "</div>";
    
    
          echo  ($anounce['titre']); 
            echo ($anounce['description']);
           echo($anounce['prix']);
           
           echo($anounce['created_date']);
         
    
         echo "</div>";}
      
    ?>


    function afficherAnnonce(){
        global $bdd;
        $res = [];
       $sql=' SELECT  A.*
       ,C.*
       ,I.*
    FROM annonces A
    LEFT JOIN categories C ON C.id = A.id_categorie
    INNER JOIN images_annonces I ON A.id= I.id_annonce
    ORDER BY created_date DESC';
          
          
        try{
          $req = $bdd->prepare($sql);
          $req->execute();
          $data = $req->fetchAll(PDO::FETCH_ASSOC);
          //on reconstruit un array mieux organisé pour avoir les photos
          
          foreach($data as $R){
            if(!isset($res[$R['id']])){
             $res[$R['id']] = $R;
             unset($res[$R['id']]['filepath']);
            }
            $res[$R['id']]['images'][] = ( !empty($R['filepath']) )? $R['filepath'] : 'Projetsite/no_image.png';                                             
          }
          
        }catch(Exception $e){
            echo "Erreur dans la requête " . $sql;
        }
     // le temps des tests pour voir le résultat
        return $res;
          
      }
    
    
    
    0
    1. jordane45 Messages postés 30426 Date d'inscription   Statut Modérateur Dernière intervention   4 830
       
      Avant ta boucle, fais un var_dump de la variable $anouncement et montre nous ce que ça t'affiche
      0
  4. max30_3775 Messages postés 185 Statut Membre
     
    Voila ce que ça donne var_dump($anouncement):
    8 => 
        array (size=14)
          'id' => string '8' (length=1)
          'created_by_member' => string '1' (length=1)
          'id_categorie' => string '6' (length=1)
          'pseudo' => string 'b' (length=1)
          'telephone' => string '7178457' (length=7)
          'email' => string 'b@gmail.com' (length=11)
          'titre' => string 'voiture à louer' (length=16)
          'description' => string '' (length=0)
          'prix' => string '245' (length=3)
          'created_date' => string '2021-12-08' (length=10)
          'nom' => string 'voitures' (length=8)
          'id_annonce' => string '13' (length=2)
          'images' => 
            array (size=1)
              0 => string 'pictures/voiture.jpg' (length=20)
      1 => 
        array (size=14)
          'id' => string '1' (length=1)
          'created_by_member' => string '0' (length=1)
          'id_categorie' => string '11' (length=2)
          'pseudo' => string 'essai1' (length=6)
          'telephone' => string '44228866' (length=8)
          'email' => string 'essai1@gmail.com' (length=16)
          'titre' => string 'Téléphone à vendre' (length=21)
          'description' => string 'Je vends un téléphone. Appelez si vousêtes interessé' (length=56)
          'prix' => string '1000' (length=4)
          'created_date' => string '2021-12-07' (length=10)
          'nom' => string 'telephones' (length=10)
          'id_annonce' => string '2' (length=1)
          'images' => 
            array (size=1)
              0 => string 'pictures/telephone.jpg' (length=22)
      2 => 
        array (size=14)
          'id' => string '2' (length=1)
          'created_by_member' => string '1' (length=1)
          'id_categorie' => string '2' (length=1)
          'pseudo' => string 'a' (length=1)
          'telephone' => string '36377769' (length=8)
          'email' => string 'a@gmail.com' (length=11)
          'titre' => string 'Maison à vendre' (length=16)
          'description' => string 'Je vends une maison' (length=19)
          'prix' => string '2556' (length=4)
          'created_date' => string '2021-12-07' (length=10)
          'nom' => string 'maisons' (length=7)
          'id_annonce' => string '3' (length=1)
          'images' => 
            array (size=1)
              0 => string 'pictures/maison5.jpg' (length=20)
      3 => 
        array (size=14)
          'id' => string '3' (length=1)
          'created_by_member' => string '1' (length=1)
          'id_categorie' => string '5' (length=1)
          'pseudo' => string 'b' (length=1)
          'telephone' => string '7178457' (length=7)
          'email' => string 'b@gmail.com' (length=11)
          'titre' => string 'Location de bureau' (length=18)
          'description' => string 'Notre agence loue des bureau' (length=28)
          'prix' => string '2000' (length=4)
          'created_date' => string '2021-12-07' (length=10)
          'nom' => string 'bureaux' (length=7)
          'id_annonce' => string '4' (length=1)
          'images' => 
            array (size=1)
              0 => string 'pictures/bureau1.jpg' (length=20)
      4 => 
        array (size=14)
          'id' => string '4' (length=1)
          'created_by_member' => string '1' (length=1)
          'id_categorie' => string '5' (length=1)
          'pseudo' => string 'b' (length=1)
          'telephone' => string '7178457' (length=7)
          'email' => string 'b@gmail.com' (length=11)
          'titre' => string 'Location de bureau' (length=18)
          'description' => string 'Notre agence loue des bureau' (length=28)
          'prix' => string '2000' (length=4)
          'created_date' => string '2021-12-07' (length=10)
          'nom' => string 'bureaux' (length=7)
          'id_annonce' => string '4' (length=1)
          'images' => 
            array (size=1)
              0 => string 'pictures/bureau2.jpg' (length=20)
      5 => 
        array (size=14)
          'id' => string '5' (length=1)
          'created_by_member' => string '1' (length=1)
          'id_categorie' => string '5' (length=1)
          'pseudo' => string 'b' (length=1)
          'telephone' => string '7178457' (length=7)
          'email' => string 'b@gmail.com' (length=11)
          'titre' => string 'Location de bureau' (length=18)
          'description' => string 'Notre agence loue des bureau' (length=28)
          'prix' => string '2000' (length=4)
          'created_date' => string '2021-12-07' (length=10)
          'nom' => string 'bureaux' (length=7)
          'id_annonce' => string '4' (length=1)
          'images' => 
            array (size=1)
              0 => string 'pictures/bureau3.jpg' (length=20)
      6 => 
        array (size=14)
          'id' => string '6' (length=1)
          'created_by_member' => string '1' (length=1)
          'id_categorie' => string '5' (length=1)
          'pseudo' => string 'b' (length=1)
          'telephone' => string '7178457' (length=7)
          'email' => string 'b@gmail.com' (length=11)
          'titre' => string 'Location de bureau' (length=18)
          'description' => string 'Notre agence loue des bureau' (length=28)
          'prix' => string '2000' (length=4)
          'created_date' => string '2021-12-07' (length=10)
          'nom' => string 'bureaux' (length=7)
          'id_annonce' => string '4' (length=1)
          'images' => 
            array (size=1)
              0 => string 'pictures/bureau4.jpg' (length=20)
      7 => 
        array (size=14)
          'id' => string '7' (length=1)
          'created_by_member' => string '1' (length=1)
          'id_categorie' => string '5' (length=1)
          'pseudo' => string 'b' (length=1)
          'telephone' => string '7178457' (length=7)
          'email' => string 'b@gmail.com' (length=11)
          'titre' => string 'Location de bureau' (length=18)
          'description' => string 'Notre agence loue des bureau' (length=28)
          'prix' => string '2000' (length=4)
          'created_date' => string '2021-12-07' (length=10)
          'nom' => string 'bureaux' (length=7)
          'id_annonce' => string '4' (length=1)
          'images' => 
            array (size=1)
              0 => string 'pictures/bureau5.jpg' (length=20)
    0
  5. Vous n’avez pas trouvé la réponse que vous recherchez ?

    Posez votre question
  6. jordane45 Messages postés 30426 Date d'inscription   Statut Modérateur Dernière intervention   4 830
     
    Tu dis que tes div sont bien fermées .... je ne pense pas...

    Tu as, dans ton IF, l'ouverture d'une div ... que tu fermes que après ton if ..
     echo'<div class="container">';
    


    Tu peux avoir le bon nombre de </div> .. si ils ne sont pas bien placés dans ton code ça fou le bordel..

    En gros, pour corriger ton souci et mettre une image par défaut, ton code devrait ressembler à un truc du genre
    <?php foreach($anouncement as $anounce){
      echo'<div class="annoncepubs">';
     
        $images = !empty($anounce['images']) ? $anounce['images'] : ['Projetsite/no_image.png']; // array
     
        if(!empty($images) ) {
          //boucle sur les images
          echo'<div class="container">';
          echo ' <div class="slider" >';
          foreach( $images as $img ){
             echo '<img class ="active" style="width:90px; height:90px;" src="'.$img.'">';
          }
          echo "</div>"; // fin div slider . 
    
          //ce bloc de div est bien fermé;. 
         echo '<div class="cont-btn" >';
         echo '  <div class="btn-nav left" > <'; 
         echo "  </div>";
         echo '  <div class="btn-nav right" > >';
         echo "  </div>";
         echo "</div>";
         //----------------------------------------
         
         
         echo "</div>"; // fin div container
        }
      
        echo $anounce['titre']; 
        echo $anounce['description'];
        echo $anounce['prix'];
        echo $anounce['created_date'];
       
    
         echo "</div>"; // fin div annoncepubs 
     }
      
    ?>
    


    0
  7. max30_3775 Messages postés 185 Statut Membre
     
    La div étant fermée au bon endroit, les images présentes dans une annonce font toujours répéter cette dernière, et l'image par défaut ne s'affiche toujours pas:
    Page d'affichage
    <?php foreach($anouncement as $anounce){
      echo'
      <div class="annoncepubs">';
     
        $images = !empty($anounce['images']) ? $anounce['images'] : []; // array
     
        if(!empty($images) )
        {
          //boucle sur les images
          echo'<div class="container">';
          echo ' <div class="slider" >';
          foreach( $images as $img )
          {
     
            if(empty($img) ){ $img = 'Projetsite/no_image.png'; }
     
            echo '<img class ="active" style="width:90px; height:90px;" src="'.$img.'">';
              }
         echo "</div>";
    
         echo ' <div class="cont-btn" >';
         echo ' <div class="btn-nav left" > <'; echo "</div>";
         echo ' <div class="btn-nav right" > >';echo "</div>";
         echo "</div>";
    
    
    
    
         echo "</div>"; // fin div container
        }
      
        
      
    
    
          echo  ($anounce['titre']); 
            echo ($anounce['description']);
           echo($anounce['prix']);
           
           echo($anounce['created_date']);
         
    
         echo "</div>";}
      
    ?>


    La fonction affichage:
    function afficherAnnonce(){
        global $bdd;
        $res = [];
       $sql=' SELECT  A.*
       ,C.*
       ,I.*
    FROM annonces A
    LEFT JOIN categories C ON C.id = A.id_categorie
    INNER JOIN images_annonces I ON A.id= I.id_annonce
    ORDER BY created_date DESC';
          
          
        try{
          $req = $bdd->prepare($sql);
          $req->execute();
          $data = $req->fetchAll(PDO::FETCH_ASSOC);
          //on reconstruit un array mieux organisé pour avoir les photos
          
          foreach($data as $R){
            if(!isset($res[$R['id']])){
             $res[$R['id']] = $R;
             unset($res[$R['id']]['filepath']);
            }
            $res[$R['id']]['images'][] = ( !empty($R['filepath']) )? $R['filepath'] : 'PROJETSITE/no_image.png';                                              
          }
          
        }catch(Exception $e){
            echo "Erreur dans la requête " . $sql;
        }
     // le temps des tests pour voir le résultat
        return $res;
          
      }
    
    
    0
    1. jordane45 Messages postés 30426 Date d'inscription   Statut Modérateur Dernière intervention   4 830
       
      Pour l'image par défaut, reprend la ligne 4 du code que je t'ai donné...
      0
      1. jordane45 Messages postés 30426 Date d'inscription   Statut Modérateur Dernière intervention   4 830 > jordane45 Messages postés 30426 Date d'inscription   Statut Modérateur Dernière intervention  
         
        D'ailleurs, je ne comprends pas pourquoi tu n'as pas été capable de copier-coller le code que je t'ai fourni...
        Est-ce que au moins tu l'as testé ??
        Qu'as-tu obtenu ??

        Pourrais-tu nous fournir le code source généré de ta page ?
        Pour obtenir le code source généré de ta page, tu la fiche dans ton navigateur Internet puis tu utilises le raccourci clavier CTRL+u
        0
  8. Pitet Messages postés 2845 Statut Membre 530
     
    Bonjour,

    Avec ton dernier code, l'image par défaut ne pourra effectivement pas être affichée.
    En effet à la ligne 5, si la variable $anounce['images'] est vide (donc si l'annonce ne possède pas d'image), la variable $image sera initialisée avec un tableau vide ([]).
    La condition ligne 7 if(!empty($images)) sera donc fausse et le code php à l'intérieur de ce bloc if ne sera donc pas exécuté.

    Pour afficher l'image par défaut si l'annonce ne possède pas d'image, il faut donc :
    - soit déplacer la condition if(!empty($images)) juste avant l'affichage de la boucle et ajouter un bloc "else" à après ce bloc "if" pour afficher l'image par défaut
    - soit simplement initialiser la variable $image avec un tableau contenant l'image par défaut (['Projetsite/no_image.png']) lorsque l'annonce ne possède pas d'image, voir le dernier exemple proposé par jordane45 à la ligne 4.

    Tu as également une erreur de syntaxe html pour les boutons de navigation left et right du slider : il n'est pas correct d'utiliser les caractères "<" et ">" comme texte de ces boutons puisque ces caractères sont réservés en html (ouverture et fermeture de balise html).
    Pour utiliser ces caractères comme texte dans un document html, il faut les convertir en entités html :
    echo '<div class="cont-btn">';
    echo '<div class="btn-nav left">& lt;</div>';
    echo '<div class="btn-nav right">& gt;</div>';
    echo '</div>';

    -> dans cet exemple, il faut normalement bien supprimer les espaces entre & et lt; ou gt; (le forum ccm converti automatiquement les entités si j'écris cet exemple sans l'espace)
    https://developer.mozilla.org/fr/docs/Glossary/Entity

    A ce sujet, essaye de prendre le réflexe d'utiliser le validateur w3c pour vérifier que ton résultat html ne contient pas d'erreur de syntaxe.
    0
  9. max30_3775 Messages postés 185 Statut Membre
     
    Merci Jordane et Pitet pour vos retours. J'ai bien ajouté cette ligne:
    $images = !empty($anounce['images']) ? $anounce['images'] : ['PROJETSITE/no_image.png'];
    
    D'ailleurs l'image par défaut se trouve dans le même dossier que lespages php, j'ai beau enlevé donc projetsite pour laisser no_image.png, pour remettre projetsite finalement à sa place. L'image par défaut est toujours absente, et les images des annonces font répéter l'annonce elle même. On peut voir dans le code source l'exemple de vélo et bureau.
    Voici le code:
    <div class="annoncepubs"><div class="container"> <div class="slider" ><img class ="active" style="width:90px; height:90px;" src="pictures/velo1.jpg"></div><div class="cont-btn"><div class="btn-nav left"><</div><div class="btn-nav right">></div></div></div>VeloVelo à vendre02021-12-09</div><div class="annoncepubs"><div class="container"> <div class="slider" ><img class ="active" style="width:90px; height:90px;" src="pictures/velo2.jpg"></div><div class="cont-btn"><div class="btn-nav left"><</div><div class="btn-nav right">></div></div></div>VeloVelo à vendre02021-12-09</div><div class="annoncepubs"><div class="container"> <div class="slider" ><img class ="active" style="width:90px; height:90px;" src="pictures/voiture.jpg"></div><div class="cont-btn"><div class="btn-nav left"><</div><div class="btn-nav right">></div></div></div>voiture à louer2452021-12-08</div><div class="annoncepubs"><div class="container"> <div class="slider" ><img class ="active" style="width:90px; height:90px;" src="pictures/telephone.jpg"></div><div class="cont-btn"><div class="btn-nav left"><</div><div class="btn-nav right">></div></div></div>Téléphone à vendreJe vends un téléphone. Appelez si vousêtes interessé10002021-12-07</div><div class="annoncepubs"><div class="container"> <div class="slider" ><img class ="active" style="width:90px; height:90px;" src="pictures/maison5.jpg"></div><div class="cont-btn"><div class="btn-nav left"><</div><div class="btn-nav right">></div></div></div>Maison à vendreJe vends une maison25562021-12-07</div><div class="annoncepubs"><div class="container"> <div class="slider" ><img class ="active" style="width:90px; height:90px;" src="pictures/bureau1.jpg"></div><div class="cont-btn"><div class="btn-nav left"><</div><div class="btn-nav right">></div></div></div>Location de bureauNotre agence loue des bureau20002021-12-07</div><div class="annoncepubs"><div class="container"> <div class="slider" ><img class ="active" style="width:90px; height:90px;" src="pictures/bureau2.jpg"></div><div class="cont-btn"><div class="btn-nav left"><</div><div class="btn-nav right">></div></div></div>Location de bureauNotre agence loue des bureau20002021-12-07</div><div class="annoncepubs"><div class="container"> <div class="slider" ><img class ="active" style="width:90px; height:90px;" src="pictures/bureau3.jpg"></div><div class="cont-btn"><div class="btn-nav left"><</div><div class="btn-nav right">></div></div></div>Location de bureauNotre agence loue des bureau20002021-12-07</div><div class="annoncepubs"><div class="container"> <div class="slider" ><img class ="active" style="width:90px; height:90px;" src="pictures/bureau4.jpg"></div><div class="cont-btn"><div class="btn-nav left"><</div><div class="btn-nav right">></div></div></div>Location de bureauNotre agence loue des bureau20002021-12-07</div><div class="annoncepubs"><div class="container"> <div class="slider" ><img class ="active" style="width:90px; height:90px;" src="pictures/bureau5.jpg"></div><div class="cont-btn"><div class="btn-nav left"><</div><div class="btn-nav right">></div></div></div>Location de bureauNotre agence loue des bureau20002021-12-07</div>


    Pitet, j'ai bien suivi tes instructions.
    0
  10. jordane45 Messages postés 30426 Date d'inscription   Statut Modérateur Dernière intervention   4 830
     
    Bon.. on va y aller par étape...
    Car il semble y avoir une différence entre ce que le var_dump retourne et ce que ton code affiche..

    Peux tu remplacer ton code par
    <?php
    echo "<hr><pre> DUMP anouncement :<br>";
    var_dump($anouncement);
    echo "</pre>";
    foreach($anouncement as $K => $anounce){ 
      echo '<div class="annoncepubs">';
      echo "K:".$K . "<br>";
      print_r($anounce,true);
      echo '</div>';
    }
    


    et montre nous ce que ça donne.

    0
  11. bdsm
     
    tres bien jaimerais savoit ce que tu recherche
    0
    1. jordane45 Messages postés 30426 Date d'inscription   Statut Modérateur Dernière intervention   4 830
       
      ce que je cherche ?
      A comprendre pourquoi, alors que le résultat du var_dump que tu nous montres semble totalement cohérent avec ce que tu veux obtenir... ta boucle elle ne correspond pas à ce qui devrait être généré ...

      D'ailleurs, une fois que tu auras fais le test que je te demande .. il faudra que tu nous montres le code COMPLET de ta page... car pour l'instant tu ne nous montres que des morceaux... et on pourrait penser que le souci se situe ailleurs..

      Je précises également que je t'avais demandé tout à l'heure le code COMPLET généré de ta page .. et pas juste un bout...
      0
  12. max30_3775 Messages postés 185 Statut Membre
     
    Voici Jordane ce ça donne le var_dump que tu as demandais:
    9 => 
        array (size=14)
          'id' => string '9' (length=1)
          'created_by_member' => string '0' (length=1)
          'id_categorie' => string '9' (length=1)
          'pseudo' => string 'hello' (length=5)
          'telephone' => string '2152' (length=4)
          'email' => string 'l@l.ju' (length=6)
         'titre' => string 'Velo' (length=4)
          'description' => string 'Velo à vendre' (length=14)
          'prix' => string '0' (length=1)
          'created_date' => string '2021-12-09' (length=10)
          'nom' => string 'velos' (length=5)
          'id_annonce' => string '16' (length=2)
          'images' => 
            array (size=1)
              0 => string 'pictures/velo1.jpg' (length=18)
      10 => 
        array (size=14)
          'id' => string '10' (length=2)
          'created_by_member' => string '0' (length=1)
          'id_categorie' => string '9' (length=1)
          'pseudo' => string 'hello' (length=5)
          'telephone' => string '2152' (length=4)
          'email' => string 'l@l.ju' (length=6
          'titre' => string 'Velo' (length=4)
          'description' => string 'Velo à vendre' (length=14)
          'prix' => string '0' (length=1)
          'created_date' => string '2021-12-09' (length=10)
          'nom' => string 'velos' (length=5)
          'id_annonce' => string '16' (length=2)
          'images' => 
            array (size=1)
              0 => string 'pictures/velo2.jpg' (length=18)
      8 => 
        array (size=14)
          'id' => string '8' (length=1)
          'created_by_member' => string '1' (length=1)
          'id_categorie' => string '6' (length=1)
          'pseudo' => string 'b' (length=1)
          'telephone' => string '7178457' (length=7)
          'email' => string 'b@gmail.com' (length=11
          'titre' => string 'voiture à louer' (length=16)
          'description' => string '' (length=0)
          'prix' => string '245' (length=3)
          'created_date' => string '2021-12-08' (length=10)
          'nom' => string 'voitures' (length=8)
          'id_annonce' => string '13' (length=2)
          'images' => 
            array (size=1)
              0 => string 'pictures/voiture.jpg' (length=20)
      1 => 
        array (size=14)
          'id' => string '1' (length=1)
          'created_by_member' => string '0' (length=1)
          'id_categorie' => string '11' (length=2)
          'pseudo' => string 'essai1' (length=6)
          'telephone' => string '44228866' (length=8)
          'email' => string 'essai1@gmail.com' (length=16)
          'titre' => string 'Téléphone à vendre' (length=21)
          'description' => string 'Je vends un téléphone. Appelez si vousêtes interessé' (length=56)
          'prix' => string '1000' (length=4)
          'created_date' => string '2021-12-07' (length=10)
          'nom' => string 'telephones' (length=10)
          'id_annonce' => string '2' (length=1)
          'images' => 
            array (size=1)
              0 => string 'pictures/telephone.jpg' (length=22)
      2 => 
        array (size=14)
          'id' => string '2' (length=1)
          'created_by_member' => string '1' (length=1)
          'id_categorie' => string '2' (length=1)
          'pseudo' => string 'a' (length=1)
          'telephone' => string '36377769' (length=8)
          'email' => string 'a@gmail.com' (length=11)
          'titre' => string 'Maison à vendre' (length=16)
          'description' => string 'Je vends une maison' (length=19)
          'prix' => string '2556' (length=4)
          'created_date' => string '2021-12-07' (length=10)
          'nom' => string 'maisons' (length=7)
          'id_annonce' => string '3' (length=1)
          'images' => 
            array (size=1)
              0 => string 'pictures/maison5.jpg' (length=20)
      3 => 
        array (size=14)
          'id' => string '3' (length=1)
          'created_by_member' => string '1' (length=1)
          'id_categorie' => string '5' (length=1)
          'pseudo' => string 'b' (length=1)
          'telephone' => string '7178457' (length=7)
          'email' => string 'b@gmail.com' (length=11)
          'titre' => string 'Location de bureau' (length=18)
          'description' => string 'Notre agence loue des bureau' (length=28)
          'prix' => string '2000' (length=4)
          'created_date' => string '2021-12-07' (length=10)
          'nom' => string 'bureaux' (length=7)
          'id_annonce' => string '4' (length=1)
          'images' => 
            array (size=1)
              0 => string 'pictures/bureau1.jpg' (length=20)
      4 => 
        array (size=14)
          'id' => string '4' (length=1)
          'created_by_member' => string '1' (length=1)
          'id_categorie' => string '5' (length=1)
          'pseudo' => string 'b' (length=1)
          'telephone' => string '7178457' (length=7)
          'email' => string 'b@gmail.com' (length=11)
          'titre' => string 'Location de bureau' (length=18)
          'description' => string 'Notre agence loue des bureau' (length=28)
          'prix' => string '2000' (length=4)
          'created_date' => string '2021-12-07' (length=10)
          'nom' => string 'bureaux' (length=7)
          'id_annonce' => string '4' (length=1)
          'images' => 
            array (size=1)
              0 => string 'pictures/bureau2.jpg' (length=20)
      5 => 
        array (size=14)
          'id' => string '5' (length=1)
          'created_by_member' => string '1' (length=1)
          'id_categorie' => string '5' (length=1)
          'pseudo' => string 'b' (length=1)
          'telephone' => string '7178457' (length=7)
          'email' => string 'b@gmail.com' (length=11)
          'titre' => string 'Location de bureau' (length=18)
          'description' => string 'Notre agence loue des bureau' (length=28)
          'prix' => string '2000' (length=4)
          'created_date' => string '2021-12-07' (length=10)
          'nom' => string 'bureaux' (length=7)
          'id_annonce' => string '4' (length=1)
          'images' => 
            array (size=1)
              0 => string 'pictures/bureau3.jpg' (length=20)
      6 => 
        array (size=14)
          'id' => string '6' (length=1)
          'created_by_member' => string '1' (length=1)
          'id_categorie' => string '5' (length=1)
          'pseudo' => string 'b' (length=1)
          'telephone' => string '7178457' (length=7)
          'email' => string 'b@gmail.com' (length=11)
          'titre' => string 'Location de bureau' (length=18)
          'description' => string 'Notre agence loue des bureau' (length=28)
          'prix' => string '2000' (length=4)
          'created_date' => string '2021-12-07' (length=10)
          'nom' => string 'bureaux' (length=7)
          'id_annonce' => string '4' (length=1)
          'images' => 
            array (size=1)
              0 => string 'pictures/bureau4.jpg' (length=20)
      7 => 
        array (size=14)
          'id' => string '7' (length=1)
          'created_by_member' => string '1' (length=1)
          'id_categorie' => string '5' (length=1)
          'pseudo' => string 'b' (length=1)
          'telephone' => string '7178457' (length=7)
          'email' => string 'b@gmail.com' (length=11)
          'titre' => string 'Location de bureau' (length=18)
          'description' => string 'Notre agence loue des bureau' (length=28)
          'prix' => string '2000' (length=4)
          'created_date' => string '2021-12-07' (length=10)
          'nom' => string 'bureaux' (length=7)
          'id_annonce' => string '4' (length=1)
          'images' => 
            array (size=1)
              0 => string 'pictures/bureau5.jpg' (length=20)
    0
    1. jordane45 Messages postés 30426 Date d'inscription   Statut Modérateur Dernière intervention   4 830
       
      le var_dump n'est pas complet ......


      Pourrais tu... une bonne fois pour toutes .... copier/coller le code que je t'ai donné et le lancer puis nous copier/coller le résultat COMPLET de ce que ça te donne ????
      *
      Bien entendu, tu auras ajouté, au début du code .. l'appel à la fonction qui te permet de récupérer la liste de tes annonces... code que tu ne nous as toujours pas donné au passage ...
      0
  13. max30_3775 Messages postés 185 Statut Membre
     
    La fonction afficherAnnonce:
    $anouncement= afficherAnnonce();

    Var_dump:
     DUMP anouncement :
    C:\wamp64\www\Projetsite\accueil.php:179:
    array (size=10)
      9 => 
        array (size=14)
          'id' => string '9' (length=1)
          'created_by_member' => string '0' (length=1)
          'id_categorie' => string '9' (length=1)
          'pseudo' => string 'hello' (length=5)
          'telephone' => string '2152' (length=4)
          'email' => string 'l@l.ju' (length=6)
          'ville' => string 'nouakchott' (length=10)
          'titre' => string 'Velo' (length=4)
          'description' => string 'Velo à vendre' (length=14)
          'prix' => string '0' (length=1)
          'created_date' => string '2021-12-09' (length=10)
          'nom' => string 'velos' (length=5)
          'id_annonce' => string '16' (length=2)
          'images' => 
            array (size=1)
              0 => string 'pictures/velo1.jpg' (length=18)
      10 => 
        array (size=14)
          'id' => string '10' (length=2)
          'created_by_member' => string '0' (length=1)
          'id_categorie' => string '9' (length=1)
          'pseudo' => string 'hello' (length=5)
          'telephone' => string '2152' (length=4)
          'email' => string 'l@l.ju' (length=6)
          'ville' => string 'nouakchott' (length=10)
          'titre' => string 'Velo' (length=4)
          'description' => string 'Velo à vendre' (length=14)
          'prix' => string '0' (length=1)
          'created_date' => string '2021-12-09' (length=10)
          'nom' => string 'velos' (length=5)
          'id_annonce' => string '16' (length=2)
          'images' => 
            array (size=1)
              0 => string 'pictures/velo2.jpg' (length=18)
      8 => 
        array (size=14)
          'id' => string '8' (length=1)
          'created_by_member' => string '1' (length=1)
          'id_categorie' => string '6' (length=1)
          'pseudo' => string 'b' (length=1)
          'telephone' => string '7178457' (length=7)
          'email' => string 'b@gmail.com' (length=11)
          'ville' => string 'nouakchott' (length=10)
          'titre' => string 'voiture à louer' (length=16)
          'description' => string '' (length=0)
          'prix' => string '245' (length=3)
          'created_date' => string '2021-12-08' (length=10)
          'nom' => string 'voitures' (length=8)
          'id_annonce' => string '13' (length=2)
          'images' => 
            array (size=1)
              0 => string 'pictures/voiture.jpg' (length=20)
      1 => 
        array (size=14)
          'id' => string '1' (length=1)
          'created_by_member' => string '0' (length=1)
          'id_categorie' => string '11' (length=2)
          'pseudo' => string 'essai1' (length=6)
          'telephone' => string '44228866' (length=8)
          'email' => string 'essai1@gmail.com' (length=16)
          'ville' => string 'nouakchott' (length=10)
          'titre' => string 'Téléphone à vendre' (length=21)
          'description' => string 'Je vends un téléphone. Appelez si vousêtes interessé' (length=56)
          'prix' => string '1000' (length=4)
          'created_date' => string '2021-12-07' (length=10)
          'nom' => string 'telephones' (length=10)
          'id_annonce' => string '2' (length=1)
          'images' => 
            array (size=1)
              0 => string 'pictures/telephone.jpg' (length=22)
      2 => 
        array (size=14)
          'id' => string '2' (length=1)
          'created_by_member' => string '1' (length=1)
          'id_categorie' => string '2' (length=1)
          'pseudo' => string 'a' (length=1)
          'telephone' => string '36377769' (length=8)
          'email' => string 'a@gmail.com' (length=11)
          'ville' => string 'nouakchott' (length=10)
          'titre' => string 'Maison à vendre' (length=16)
          'description' => string 'Je vends une maison' (length=19)
          'prix' => string '2556' (length=4)
          'created_date' => string '2021-12-07' (length=10)
          'nom' => string 'maisons' (length=7)
          'id_annonce' => string '3' (length=1)
          'images' => 
            array (size=1)
              0 => string 'pictures/maison5.jpg' (length=20)
      3 => 
        array (size=14)
          'id' => string '3' (length=1)
          'created_by_member' => string '1' (length=1)
          'id_categorie' => string '5' (length=1)
          'pseudo' => string 'b' (length=1)
          'telephone' => string '7178457' (length=7)
          'email' => string 'b@gmail.com' (length=11)
          'ville' => string 'nouadhibou' (length=10)
          'titre' => string 'Location de bureau' (length=18)
          'description' => string 'Notre agence loue des bureau' (length=28)
          'prix' => string '2000' (length=4)
          'created_date' => string '2021-12-07' (length=10)
          'nom' => string 'bureaux' (length=7)
          'id_annonce' => string '4' (length=1)
          'images' => 
            array (size=1)
              0 => string 'pictures/bureau1.jpg' (length=20)
      4 => 
        array (size=14)
          'id' => string '4' (length=1)
          'created_by_member' => string '1' (length=1)
          'id_categorie' => string '5' (length=1)
          'pseudo' => string 'b' (length=1)
          'telephone' => string '7178457' (length=7)
          'email' => string 'b@gmail.com' (length=11)
          'ville' => string 'nouadhibou' (length=10)
          'titre' => string 'Location de bureau' (length=18)
          'description' => string 'Notre agence loue des bureau' (length=28)
          'prix' => string '2000' (length=4)
          'created_date' => string '2021-12-07' (length=10)
          'nom' => string 'bureaux' (length=7)
          'id_annonce' => string '4' (length=1)
          'images' => 
            array (size=1)
              0 => string 'pictures/bureau2.jpg' (length=20)
      5 => 
        array (size=14)
          'id' => string '5' (length=1)
          'created_by_member' => string '1' (length=1)
          'id_categorie' => string '5' (length=1)
          'pseudo' => string 'b' (length=1)
          'telephone' => string '7178457' (length=7)
          'email' => string 'b@gmail.com' (length=11)
          'ville' => string 'nouadhibou' (length=10)
          'titre' => string 'Location de bureau' (length=18)
          'description' => string 'Notre agence loue des bureau' (length=28)
          'prix' => string '2000' (length=4)
          'created_date' => string '2021-12-07' (length=10)
          'nom' => string 'bureaux' (length=7)
          'id_annonce' => string '4' (length=1)
          'images' => 
            array (size=1)
              0 => string 'pictures/bureau3.jpg' (length=20)
      6 => 
        array (size=14)
          'id' => string '6' (length=1)
          'created_by_member' => string '1' (length=1)
          'id_categorie' => string '5' (length=1)
          'pseudo' => string 'b' (length=1)
          'telephone' => string '7178457' (length=7)
          'email' => string 'b@gmail.com' (length=11)
          'ville' => string 'nouadhibou' (length=10)
          'titre' => string 'Location de bureau' (length=18)
          'description' => string 'Notre agence loue des bureau' (length=28)
          'prix' => string '2000' (length=4)
          'created_date' => string '2021-12-07' (length=10)
          'nom' => string 'bureaux' (length=7)
          'id_annonce' => string '4' (length=1)
          'images' => 
            array (size=1)
              0 => string 'pictures/bureau4.jpg' (length=20)
      7 => 
        array (size=14)
          'id' => string '7' (length=1)
          'created_by_member' => string '1' (length=1)
          'id_categorie' => string '5' (length=1)
          'pseudo' => string 'b' (length=1)
          'telephone' => string '7178457' (length=7)
          'email' => string 'b@gmail.com' (length=11)
          'ville' => string 'nouadhibou' (length=10)
          'titre' => string 'Location de bureau' (length=18)
          'description' => string 'Notre agence loue des bureau' (length=28)
          'prix' => string '2000' (length=4)
          'created_date' => string '2021-12-07' (length=10)
          'nom' => string 'bureaux' (length=7)
          'id_annonce' => string '4' (length=1)
          'images' => 
            array (size=1)
              0 => string 'pictures/bureau5.jpg' (length=20)
    0
    1. jordane45 Messages postés 30426 Date d'inscription   Statut Modérateur Dernière intervention   4 830
       
      Alors on est fait le dump ne semble pas totalement cohérent.

      Combien d'annonces as-tu dans ta table annonces ?

      Peux-tu également nous préciser la structure ( le nom des champs) de tes tables ?
      0
  14. max30_3775 Messages postés 185 Statut Membre
     
    J'ai 18 annonces dans la table annonces. Uniquement celles où au moins une image est soumise, apparaissent dans l'affichage, et donc leurs images apparaissent dans la table images_annonces. Les autres annonces sans images n'apparaissent pas dans l'affichage, et l'image par défaut non plus dans la table images_annonces.
    Les champs des tables annonces, images_annonces, membres et catégories, sont comme indiqués en photo:
    0
    1. jordane45 Messages postés 30426 Date d'inscription   Statut Modérateur Dernière intervention   4 830
       
      Ben oui...
      Sais tu à quoi sert un INNER JOIN et ce que c'est sensé retourner ?
      https://sql.sh/cours/jointures/inner-join


      Là, le premier souci .. c'est déjà ta requête SQL... pas ton code php.

      SELECT  A.*
         ,C.*
         ,I.*
      FROM annonces A
      LEFT JOIN categories C ON C.id = A.id_categorie
      INNER JOIN images_annonces I ON A.id= I.id_annonce
      ORDER BY created_date DESC';
      


      Il faut déjà que tu réussisses à écrire une requête SQL qui te retourne toutes les infos voulues...
      (pour rappel.. il faut tester tes requête DIRECTEMENT dans ta BDD via l'onglet sql/requete de PHPMYADMIN et non via du code PHP ..)

      Une fois que tu auras réussi à obtenir toutes tes annonces... reviens nous voir si ton code php pose toujours problème..
      0
  15. max30_3775 Messages postés 185 Statut Membre
     
    J'ai réussi à afficher l'image par défaut. Tu as raison Jordane, c'était au niveau de ma requête. Il me fallait un left join au lieu du inner join. Je reviendrai plus tard pour que nous puissions corriger l'autre soucis concernant la réptition des annonces en fonctions des images. Merci pour tout Jordane :)
    Voici ce que j'ai fait si ça peut aider des gens plus tard:

    Page d'affichage:
    <?php foreach($anouncement as $anounce){
      echo'<div class="annoncepubs">';
     
        $images = !empty($anounce['images']) ? $anounce['images'] : ['no_image.png']; // array
     
        if(!empty($images) ) {
          //boucle sur les images
          echo'<div class="container">';
          echo ' <div class="slider" >';
          foreach( $images as $img ){
             echo '<img class ="active" style="width:90px; height:90px;" src="'.$img.'">';
          }
          echo "</div>"; // fin div slider . 
    
          //ce bloc de div est bien fermé;. 
          echo '<div class="cont-btn">';
          echo '<div class="btn-nav left"><</div>';
          echo '<div class="btn-nav right">></div>';
          echo '</div>';
         //----------------------------------------
         
         
         echo "</div>"; // fin div container
        }
      
        echo $anounce['titre']; 
        echo $anounce['description'];
        echo $anounce['prix'];
        echo $anounce['created_date'];
       
    
         echo "</div>"; // fin div annoncepubs 
     }
      
    ?>

    la fonction afficherannonce:
    function afficherAnnonce(){
        global $bdd;
        $res = [];
       $sql=' SELECT  A.*
       ,C.*
       ,I.*
    FROM annonces A
    LEFT JOIN categories C ON C.id = A.id_categorie
    LEFT JOIN images_annonces I ON A.id= I.id_annonce
    ORDER BY created_date DESC';
          
          
        try{
          $req = $bdd->prepare($sql);
          $req->execute();
          $data = $req->fetchAll(PDO::FETCH_ASSOC);
          //on reconstruit un array mieux organisé pour avoir les photos
          
          foreach($data as $R){
            if(!isset($res[$R['id']])){
             $res[$R['id']] = $R;
             unset($res[$R['id']]['filepath']);
            }
            $res[$R['id']]['images'][] = ( !empty($R['filepath']) )? $R['filepath'] :'no_image.png';                                               
          }
          
        }catch(Exception $e){
            echo "Erreur dans la requête " . $sql;
        }
     // le temps des tests pour voir le résultat
        return $res;
          
      }
    0
  16. jordane45 Messages postés 30426 Date d'inscription   Statut Modérateur Dernière intervention   4 830
     
    Sinon, un peu plus simple via du php

    <?php
    
    function getAnnnonces(){
      global $bdd;
      $sql = ' SELECT  A.*
                      ,C.nom AS CATEGORIE
               FROM annonces A
               LEFT JOIN categories C ON C.id = A.id_categorie
               ORDER BY A.created_date DESC';
      try {
        $req = $bdd->prepare($sql);
        $req->execute();
        $res = $req->fetchAll(PDO::FETCH_ASSOC);
      } catch (Exception $e) {
        echo "Erreur dans la requête " . $sql;
      }
      // le temps des tests pour voir le résultat
      return $res;
    }
    
    function getImagesAnnonces(){
      global $bdd;
      $sql = "SELECT id_annonce,filepath as IMG 
          FROM images_annonces
          ORDER BY id_annonce DESC";
      try {
        $req = $bdd->prepare($sql);
        $req->execute();
        $res = $req->fetchAll(PDO::FETCH_GROUP);
      } catch (Exception $e) {
        echo "Erreur dans la requête " . $sql;
      }
      // le temps des tests pour voir le résultat
      return $res;
    }
    
    function afficherAnnonce(){
    
      $annonces = getAnnnonces();
      $images = getImagesAnnonces();
    
      $res = [];
      foreach ($annonces as $R) {
        $res[$R['id']] = $R;
        $imgs = !empty($images[$R['id']]) ? $images[$R['id']] : NULL;
        $res[$R['id']]['images'] = !empty($imgs['IMG']) ? $imgs['IMG'] : null;
      }
    
      return $res;
    }
    
    // et ensuite.. pour ton affichage...
    $anouncement = afficherAnnonce();
    
    foreach ($anouncement as $anounce) {
      echo '<div class="annoncepubs">';
    
      $images = !empty($anounce['images']) ? $anounce['images'] : [__DIR__ . '/no_image.png']; // array
    
      if (!empty($images)) {
        //boucle sur les images
        echo'<div class="container">';
        echo ' <div class="slider" >';
        foreach ($images as $img) {
          echo '<img class ="active" style="width:90px; height:90px;" src="' . $img . '">';
        }
        echo "</div>"; // fin div slider . 
        echo '<div class="cont-btn" >';
        echo '  <div class="btn-nav left" > <';
        echo "  </div>";
        echo '  <div class="btn-nav right" > >';
        echo "  </div>";
        echo "</div>";
        echo "</div>"; // fin div container
      }
      echo "<span class='category'>" . $anounce['CATEGORIE'] . "</span>";
      echo "<span class='title'>" . $anounce['titre'] . "</span>";
      echo "<span class='comment'>" . $anounce['description'] . "</span>";
      echo "<span class='price'>" . $anounce['prix'] . "</span>";
      echo "<span class='date'>" . $anounce['created_date'] . "</span>";
    
    
      echo "</div>"; // fin div annoncepubs 
    }
      
    


    Si ça ne marche pas... merci de modifier la fonction comme ceci et de nous montrer ce que ça donne
    function afficherAnnonce(){
    
      $annonces = getAnnnonces();
      $images = getImagesAnnonces();
    
      $res = [];
      foreach ($annonces as $R) {
        $res[$R['id']] = $R;
        $imgs = !empty($images[$R['id']]) ? $images[$R['id']] : NULL;
        $res[$R['id']]['images'] = !empty($imgs['IMG']) ? $imgs['IMG'] : null;
      }
    
      //--- POUR DEBUGUER ----//
      echo "<hr><br><h1>DEBUG</h1><pre>";
      echo "<br> Annonces:<br>";
      var_dump($annonces);
      echo "<hr><br> Images:<br>";
      var_dump($images);
      echo "<hr><br> Res:<br>";
      var_dump($res);
      echo "</pre>";
    
      return $res;
    }
    


    0
  17. max30_3775 Messages postés 185 Statut Membre
     
    C'est beaucoup mieux qu'avant, les annonces ne se répètent plus, mais GROS PROBLEME:
    ==>Toutes les images ont été remplacées par l'image par défaut ????



    La page des fonctions:
    function getAnnnonces(){
      global $bdd;
      $sql = ' SELECT  A.*
                      ,C.nom AS CATEGORIE
               FROM annonces A
               LEFT JOIN categories C ON C.id = A.id_categorie
               ORDER BY A.created_date DESC';
      try {
        $req = $bdd->prepare($sql);
        $req->execute();
        $res = $req->fetchAll(PDO::FETCH_ASSOC);
      } catch (Exception $e) {
        echo "Erreur dans la requête " . $sql;
      }
      // le temps des tests pour voir le résultat
      return $res;
    }
    
    function getImagesAnnonces(){
      global $bdd;
      $sql = "SELECT id_annonce,filepath as IMG 
          FROM images_annonces
          ORDER BY id_annonce DESC";
      try {
        $req = $bdd->prepare($sql);
        $req->execute();
        $res = $req->fetchAll(PDO::FETCH_GROUP);
      } catch (Exception $e) {
        echo "Erreur dans la requête " . $sql;
      }
      // le temps des tests pour voir le résultat
      return $res;
    }
    
    function afficherAnnonce(){
    
      $annonces = getAnnnonces();
      $images = getImagesAnnonces();
    
      $res = [];
      foreach ($annonces as $R) {
        $res[$R['id']] = $R;
        $imgs = !empty($images[$R['id']]) ? $images[$R['id']] : NULL;
        $res[$R['id']]['images'] = !empty($imgs['IMG']) ? $imgs['IMG'] : null;
      }
    
      return $res;
    }


    Page d'affichage:
    <?php foreach ($anouncement as $anounce) {
      echo '<div class="annoncepubs">';
    
      $images = !empty($anounce['images']) ? $anounce['images'] : ['/no_image.png']; // array
    
      if (!empty($images)) {
        //boucle sur les images
        echo'<div class="container">';
        echo ' <div class="slider" >';
        foreach ($images as $img) {
          echo '<img class ="active" style="width:90px; height:90px;" src="' . $img . '">';
        }
        echo "</div>"; // fin div slider . 
        echo '<div class="cont-btn" >';
        echo '  <div class="btn-nav left" > <';
        echo "  </div>";
        echo '  <div class="btn-nav right" > >';
        echo "  </div>";
        echo "</div>";
        echo "</div>"; // fin div container
      }
      echo "<span class='category'>" . $anounce['CATEGORIE'] . "</span>";
      echo "<span class='title'>" . $anounce['titre'] . "</span>";
      echo "<span class='comment'>" . $anounce['description'] . "</span>";
      echo "<span class='price'>" . $anounce['prix'] . "</span>";
      echo "<span class='date'>" . $anounce['created_date'] . "</span>";
    
    
      echo "</div>"; // fin div annoncepubs 
    }
      
    0
    1. jordane45 Messages postés 30426 Date d'inscription   Statut Modérateur Dernière intervention   4 830
       
      Tu as fait les var_dump pour voir ce que contiennent les variables ?
      0
  18. max30_3775 Messages postés 185 Statut Membre
     
    un var_dump de la $anouncement a retournes images: NULL pour toutes:
    C:\wamp64\www\Projetsite\accueil.php:208:
    array (size=7)
      16 => 
        array (size=13)
          'id' => string '16' (length=2)
          'created_by_member' => string '0' (length=1)
          'id_categorie' => string '9' (length=1)
          'pseudo' => string 'hello' (length=5)
          'telephone' => string '2152' (length=4)
          'email' => string 'l@l.ju' (length=6)
          'ville' => string 'nouakchott' (length=10)
          'titre' => string 'Velo' (length=4)
          'description' => string 'Velo à vendre' (length=14)
          'prix' => string '0' (length=1)
          'created_date' => string '2021-12-09' (length=10)
          'CATEGORIE' => string 'velos' (length=5)
          'images' => null
      20 => 
        array (size=13)
          'id' => string '20' (length=2)
          'created_by_member' => string '0' (length=1)
          'id_categorie' => string '12' (length=2)
          'pseudo' => string 'mik' (length=3)
          'telephone' => string '212363' (length=6)
          'email' => string 'k@k.g' (length=5)
          'ville' => string 'nouakchott' (length=10)
          'titre' => string 'Ordi à vendre' (length=14)
          'description' => string 'Un bon ordi à vendrre' (length=22)
          'prix' => string '800' (length=3)
          'created_date' => string '2021-12-09' (length=10)
          'CATEGORIE' => string 'ordinateurs_portables' (length=21)
          'images' => null
      21 => 
        array (size=13)
          'id' => string '21' (length=2)
          'created_by_member' => string '0' (length=1)
          'id_categorie' => string '6' (length=1)
          'pseudo' => string 'essainoimage' (length=12)
          'telephone' => string '2213' (length=4)
          'email' => string 'lo@ol.fr' (length=8)
          'ville' => string 'nouakchott' (length=10)
          'titre' => string 'voiture sans image' (length=18)
          'description' => string 'voiture sans imagevoiture sans image' (length=36)
          'prix' => string '252' (length=3)
          'created_date' => string '2021-12-09' (length=10)
          'CATEGORIE' => string 'voitures' (length=8)
          'images' => null
      22 => 
        array (size=13)
          'id' => string '22' (length=2)
          'created_by_member' => string '0' (length=1)
          'id_categorie' => string '11' (length=2)
          'pseudo' => string 'phonosansimage' (length=14)
          'telephone' => string '458' (length=3)
          'email' => string 'lo@pm.lo' (length=8)
          'ville' => string 'nouakchott' (length=10)
          'titre' => string 'Telephone sans image' (length=20)
          'description' => string 'Essai pour telelphone sans image' (length=32)
          'prix' => string '52' (length=2)
          'created_date' => string '2021-12-09' (length=10)
          'CATEGORIE' => string 'telephones' (length=10)
          'images' => null
      13 => 
        array (size=13)
          'id' => string '13' (length=2)
          'created_by_member' => string '1' (length=1)
          'id_categorie' => string '6' (length=1)
          'pseudo' => string 'b' (length=1)
          'telephone' => string '7178457' (length=7)
          'email' => string 'b@gmail.com' (length=11)
          'ville' => string 'nouakchott' (length=10)
          'titre' => string 'voiture à louer' (length=16)
          'description' => string '' (length=0)
          'prix' => string '245' (length=3)
          'created_date' => string '2021-12-08' (length=10)
          'CATEGORIE' => string 'voitures' (length=8)
          'images' => null
      2 => 
        array (size=13)
          'id' => string '2' (length=1)
          'created_by_member' => string '0' (length=1)
          'id_categorie' => string '11' (length=2)
          'pseudo' => string 'essai1' (length=6)
          'telephone' => string '44228866' (length=8)
          'email' => string 'essai1@gmail.com' (length=16)
          'ville' => string 'nouakchott' (length=10)
          'titre' => string 'Téléphone à vendre' (length=21)
          'description' => string 'Je vends un téléphone. Appelez si vousêtes interessé' (length=56)
          'prix' => string '1000' (length=4)
          'created_date' => string '2021-12-07' (length=10)
          'CATEGORIE' => string 'telephones' (length=10)
          'images' => null
      3 => 
        array (size=13)
          'id' => string '3' (length=1)
          'created_by_member' => string '1' (length=1)
          'id_categorie' => string '2' (length=1)
          'pseudo' => string 'a' (length=1)
          'telephone' => string '36377769' (length=8)
          'email' => string 'a@gmail.com' (length=11)
          'ville' => string 'nouakchott' (length=10)
          'titre' => string 'Maison à vendre' (length=16)
          'description' => string 'Je vends une maison' (length=19)
          'prix' => string '2556' (length=4)
          'created_date' => string '2021-12-07' (length=10)
          'CATEGORIE' => strin
    0
    1. jordane45 Messages postés 30426 Date d'inscription   Statut Modérateur Dernière intervention   4 830
       
      Pourrais-tu s'il te plaît, relire avec une très grande attention, le texte complet du message précédent celui qui contenait le code est dans lequel je t'ai fourni les instructions à écrire afin de debuguer...
      0
  19. max30_3775 Messages postés 185 Statut Membre
     
    Voilà ce que ça donne:

    C:\wamp64\www\Projetsite\cmdaffichage.php:53:
    array (size=7)
      0 => 
        array (size=12)
          'id' => string '16' (length=2)
          'created_by_member' => string '0' (length=1)
          'id_categorie' => string '9' (length=1)
          'pseudo' => string 'hello' (length=5)
          'telephone' => string '2152' (length=4)
          'email' => string 'l@l.ju' (length=6)
          'ville' => string 'nouakchott' (length=10)
          'titre' => string 'Velo' (length=4)
          'description' => string 'Velo à vendre' (length=14)
          'prix' => string '0' (length=1)
          'created_date' => string '2021-12-09' (length=10)
          'CATEGORIE' => string 'velos' (length=5)
      1 => 
        array (size=12)
          'id' => string '20' (length=2)
          'created_by_member' => string '0' (length=1)
          'id_categorie' => string '12' (length=2)
          'pseudo' => string 'mik' (length=3)
          'telephone' => string '212363' (length=6)
          'email' => string 'k@k.g' (length=5)
          'ville' => string 'nouakchott' (length=10)
          'titre' => string 'Ordi à vendre' (length=14)
          'description' => string 'Un bon ordi à vendrre' (length=22)
          'prix' => string '800' (length=3)
          'created_date' => string '2021-12-09' (length=10)
          'CATEGORIE' => string 'ordinateurs_portables' (length=21)
      2 => 
        array (size=12)
          'id' => string '21' (length=2)
          'created_by_member' => string '0' (length=1)
          'id_categorie' => string '6' (length=1)
          'pseudo' => string 'essainoimage' (length=12)
          'telephone' => string '2213' (length=4)
          'email' => string 'lo@ol.fr' (length=8)
          'ville' => string 'nouakchott' (length=10)
          'titre' => string 'voiture sans image' (length=18)
          'description' => string 'voiture sans imagevoiture sans image' (length=36)
          'prix' => string '252' (length=3)
          'created_date' => string '2021-12-09' (length=10)
          'CATEGORIE' => string 'voitures' (length=8)
      3 => 
        array (size=12)
          'id' => string '22' (length=2)
          'created_by_member' => string '0' (length=1)
          'id_categorie' => string '11' (length=2)
          'pseudo' => string 'phonosansimage' (length=14)
          'telephone' => string '458' (length=3)
          'email' => string 'lo@pm.lo' (length=8)
          'ville' => string 'nouakchott' (length=10)
          'titre' => string 'Telephone sans image' (length=20)
          'description' => string 'Essai pour telelphone sans image' (length=32)
          'prix' => string '52' (length=2)
          'created_date' => string '2021-12-09' (length=10)
          'CATEGORIE' => string 'telephones' (length=10)
      4 => 
        array (size=12)
          'id' => string '13' (length=2)
          'created_by_member' => string '1' (length=1)
          'id_categorie' => string '6' (length=1)
          'pseudo' => string 'b' (length=1)
          'telephone' => string '7178457' (length=7)
          'email' => string 'b@gmail.com' (length=11)
          'ville' => string 'nouakchott' (length=10)
          'titre' => string 'voiture à louer' (length=16)
          'description' => string '' (length=0)
          'prix' => string '245' (length=3)
          'created_date' => string '2021-12-08' (length=10)
          'CATEGORIE' => string 'voitures' (length=8)
      5 => 
        array (size=12)
          'id' => string '2' (length=1)
          'created_by_member' => string '0' (length=1)
          'id_categorie' => string '11' (length=2)
          'pseudo' => string 'essai1' (length=6)
          'telephone' => string '44228866' (length=8)
          'email' => string 'essai1@gmail.com' (length=16)
          'ville' => string 'nouakchott' (length=10)
          'titre' => string 'Téléphone à vendre' (length=21)
          'description' => string 'Je vends un téléphone. Appelez si vousêtes interessé' (length=56)
          'prix' => string '1000' (length=4)
          'created_date' => string '2021-12-07' (length=10)
          'CATEGORIE' => string 'telephones' (length=10)
      6 => 
        array (size=12)
          'id' => string '3' (length=1)
          'created_by_member' => string '1' (length=1)
          'id_categorie' => string '2' (length=1)
          'pseudo' => string 'a' (length=1)
          'telephone' => string '36377769' (length=8)
          'email' => string 'a@gmail.com' (length=11)
          'ville' => string 'nouakchott' (length=10)
          'titre' => string 'Maison à vendre' (length=16)
          'description' => string 'Je vends une maison' (length=19)
          'prix' => string '2556' (length=4)
          'created_date' => string '2021-12-07' (length=10)
          'CATEGORIE' => string 'maisons' (length=7)
    
     Images:
    C:\wamp64\www\Projetsite\cmdaffichage.php:55:
    array (size=5)
      20 => 
        array (size=1)
          0 => 
            array (size=1)
              'IMG' => string 'pictures/ordi.jpg' (length=17)
      16 => 
        array (size=2)
          0 => 
            array (size=1)
              'IMG' => string 'pictures/velo1.jpg' (length=18)
          1 => 
            array (size=1)
              'IMG' => string 'pictures/velo2.jpg' (length=18)
      13 => 
        array (size=1)
          0 => 
            array (size=1)
              'IMG' => string 'pictures/voiture.jpg' (length=20)
      3 => 
        array (size=1)
          0 => 
            array (size=1)
              'IMG' => string 'pictures/maison5.jpg' (length=20)
      2 => 
        array (size=1)
          0 => 
            array (size=1)
              'IMG' => string 'pictures/telephone.jpg' (length=22)
    
     Res:
    C:\wamp64\www\Projetsite\cmdaffichage.php:57:
    array (size=7)
      16 => 
        array (size=13)
          'id' => string '16' (length=2)
          'created_by_member' => string '0' (length=1)
          'id_categorie' => string '9' (length=1)
          'pseudo' => string 'hello' (length=5)
          'telephone' => string '2152' (length=4)
          'email' => string 'l@l.ju' (length=6)
          'ville' => string 'nouakchott' (length=10)
          'titre' => string 'Velo' (length=4)
          'description' => string 'Velo à vendre' (length=14)
          'prix' => string '0' (length=1)
          'created_date' => string '2021-12-09' (length=10)
          'CATEGORIE' => string 'velos' (length=5)
          'images' => null
      20 => 
        array (size=13)
          'id' => string '20' (length=2)
          'created_by_member' => string '0' (length=1)
          'id_categorie' => string '12' (length=2)
          'pseudo' => string 'mik' (length=3)
          'telephone' => string '212363' (length=6)
          'email' => string 'k@k.g' (length=5)
          'ville' => string 'nouakchott' (length=10)
          'titre' => string 'Ordi à vendre' (length=14)
          'description' => string 'Un bon ordi à vendrre' (length=22)
          'prix' => string '800' (length=3)
          'created_date' => string '2021-12-09' (length=10)
          'CATEGORIE' => string 'ordinateurs_portables' (length=21)
          'images' => null
      21 => 
        array (size=13)
          'id' => string '21' (length=2)
          'created_by_member' => string '0' (length=1)
          'id_categorie' => string '6' (length=1)
          'pseudo' => string 'essainoimage' (length=12)
          'telephone' => string '2213' (length=4)
          'email' => string 'lo@ol.fr' (length=8)
          'ville' => string 'nouakchott' (length=10)
          'titre' => string 'voiture sans image' (length=18)
          'description' => string 'voiture sans imagevoiture sans image' (length=36)
          'prix' => string '252' (length=3)
          'created_date' => string '2021-12-09' (length=10)
          'CATEGORIE' => string 'voitures' (length=8)
          'images' => null
      22 => 
        array (size=13)
          'id' => string '22' (length=2)
          'created_by_member' => string '0' (length=1)
          'id_categorie' => string '11' (length=2)
          'pseudo' => string 'phonosansimage' (length=14)
          'telephone' => string '458' (length=3)
          'email' => string 'lo@pm.lo' (length=8)
          'ville' => string 'nouakchott' (length=10)
          'titre' => string 'Telephone sans image' (length=20)
          'description' => string 'Essai pour telelphone sans image' (length=32)
          'prix' => string '52' (length=2)
          'created_date' => string '2021-12-09' (length=10)
          'CATEGORIE' => string 'telephones' (length=10)
          'images' => null
      13 => 
        array (size=13)
          'id' => string '13' (length=2)
          'created_by_member' => string '1' (length=1)
          'id_categorie' => string '6' (length=1)
          'pseudo' => string 'b' (length=1)
          'telephone' => string '7178457' (length=7)
          'email' => string 'b@gmail.com' (length=11)
          'ville' => string 'nouakchott' (length=10)
          'titre' => string 'voiture à louer' (length=16)
          'description' => string '' (length=0)
          'prix' => string '245' (length=3)
          'created_date' => string '2021-12-08' (length=10)
          'CATEGORIE' => string 'voitures' (length=8)
          'images' => null
      2 => 
        array (size=13)
          'id' => string '2' (length=1)
          'created_by_member' => string '0' (length=1)
          'id_categorie' => string '11' (length=2)
          'pseudo' => string 'essai1' (length=6)
          'telephone' => string '44228866' (length=8)
          'email' => string 'essai1@gmail.com' (length=16)
          'ville' => string 'nouakchott' (length=10)
          'titre' => string 'Téléphone à vendre' (length=21)
          'description' => string 'Je vends un téléphone. Appelez si vousêtes interessé' (length=56)
          'prix' => string '1000' (length=4)
          'created_date' => string '2021-12-07' (length=10)
          'CATEGORIE' => string 'telephones' (length=10)
          'images' => null
      3 => 
        array (size=13)
          'id' => string '3' (length=1)
          'created_by_member' => string '1' (length=1)
          'id_categorie' => string '2' (length=1)
          'pseudo' => string 'a' (length=1)
          'telephone' => string '36377769' (length=8)
          'email' => string 'a@gmail.com' (length=11)
          'ville' => string 'nouakchott' (length=10)
          'titre' => string 'Maison à vendre' (length=16)
          'description' => string 'Je vends une maison' (length=19)
          'prix' => string '2556' (length=4)
          'created_date' => string '2021-12-07' (length=10)
          'CATEGORIE' => string 'maisons' (length=7)
          'images' => null
    Que recherchez-vous ?
    
    Catégorie
     
    maison,voiture.../
     
    Ville
     
    
    0
    1. jordane45 Messages postés 30426 Date d'inscription   Statut Modérateur Dernière intervention   4 830
       
      Tu peux tester ça
      function afficherAnnonce(){
      
        $annonces = getAnnnonces();
        $images = getImagesAnnonces();
      
        $res = [];
        foreach ($annonces as $R) {
          $res[$R['id']] = $R;
          $imgs = !empty($images[$R['id']]) ? $images[$R['id']] : NULL;
          if(!empty($imgs)){
            foreach($imgs as $I){
              $res[$R['id']]['images'][] = !empty($I['IMG']) ? $I['IMG'] : null;
            }
          }else{
              $res[$R['id']]['images'] = null;
          }
         
        }
      
        //--- POUR DEBUGUER ----//
        echo "<hr><br><h1>DEBUG</h1><pre>";
        echo "<br> Annonces:<br>";
        var_dump($annonces);
        echo "<hr><br> Images:<br>";
        var_dump($images);
        echo "<hr><br> Res:<br>";
        var_dump($res);
        echo "</pre>";
      
        return $res;
      }
      
      
      0
  20. max30_3775 Messages postés 185 Statut Membre
     
    Propre! Super!!! le code marche parfaitement, rien à dire. Un sauveur comme dh'habitude. Grand merci à toi Jordane :)
    0
  21. max30_3775 Messages postés 185 Statut Membre
     
    Bonjour Jordane,
    1-Donc du coup pour rechercher une annonce en adoptant la même démarche, ça affiche des erreurs. Les variables $q, $categorie, $villes sont indéfinies.
    2-Est ce que a va rechercher la catégorie présentes dans la table catégories en faisant ainsi :
    $categorie= isset($_GET['CATEGORIE'])? trim($_GET['CATEGORIE']) : null;
    ? On ne peut pas mettre postcategorie, car il les id dans les options values.
    page des fonctions
    function getSearch($q, $categorie, $villes){
        global $bdd;
        $res = [];
        $sql = 'SELECT  A.*
        ,C.nom AS CATEGORIE
    FROM annonces A
    LEFT JOIN categories C ON C.id = A.id_categorie
          WHERE (A.titre LIKE "%'.$q.'%" 
        AND C.nom LIKE "%'.$categorie.'%" 
        AND A.ville LIKE "%'.$villes.'%" 
        )
        ORDER BY created_date DESC';
          
          
        try{
          $req = $bdd->prepare($sql);
          $req->execute();
          $data = $req->fetchAll(PDO::FETCH_ASSOC);
          //on reconstruit un array mieux organisé pour avoir les photos                                            
          
        }catch(Exception $e){
            echo "Erreur dans la requête " . $sql;
        }
     // le temps des tests pour voir le résultat
        return $res;
          
      }
    
      function recherche(){
    
        $annonces =getSearch($q, $categorie, $villes);
        $images = getImagesAnnonces();
      
        $res = [];
        foreach ($annonces as $R) {
          $res[$R['id']] = $R;
          $imgs = !empty($images[$R['id']]) ? $images[$R['id']] : NULL;
          if(!empty($imgs)){
            foreach($imgs as $I){
              $res[$R['id']]['images'][] = !empty($I['IMG']) ? $I['IMG'] : null;
            }
          }else{
              $res[$R['id']]['images'] = null;
          }
         
        }
      
      
        return $res;
      }
      
    
    
    


    page de recherche:
    <?php 
    $q = !empty($_GET['q']) ? trim($_GET['q']) : null;
    $categorie= isset($_GET['CATEGORIE'])? trim($_GET['CATEGORIE']) : null;
    $villes= isset($_GET['villes'])? trim($_GET['villes']) : null  ;
    
    if($q || $categorie || $villes) {
      $search= recherche();
    
      if(!empty($search)){
    
    
       // boucle pour afficher tes résultats..
        foreach($search as $rec){
          echo'
      <div class="annoncepub">';
    
    
    
      $images = !empty($rec['images']) ? $rec['images'] : ['/no_image.png']; // array
    
      if (!empty($images)) {
        //boucle sur les images
        echo'<div class="container">';
        echo ' <div class="slider" >';
        foreach ($images as $img) {
          echo '<img class ="active" style="width:90px; height:90px;" src="' . $img . '">';
        }
        echo "</div>"; // fin div slider . 
        echo '<div class="cont-btn" >';
        echo '  <div class="btn-nav left" > <';
        echo "  </div>";
        echo '  <div class="btn-nav right" > >';
        echo "  </div>";
        echo "</div>";
        echo "</div>"; // fin div container
      }
    
    
      echo "<span class='category'>" .$rec['CATEGORIE'] . "</span>";
      echo "<span class='title'>" .$rec['titre'] . "</span>";
      echo "<span class='comment'>" .$rec['description'] . "</span>";
      echo "<span class='price'>" .$rec['prix'] . "</span>";
      echo "<span class='date'>" . $rec['created_date'] . "</span>";
    
    
      echo "</div>"; // fin div annoncepubs 
    }
    
     }else{
      echo "Aucun résultat pour votre recherche...";
     }
    }
    
    ?>
    0
    1. jordane45 Messages postés 30426 Date d'inscription   Statut Modérateur Dernière intervention   4 830
       
      Cherche a comprendre le passage de paramètres dans une fonction...
      0
  • 1
  • 2