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

Résolu/Fermé
max30_3775 Messages postés 178 Date d'inscription jeudi 4 novembre 2021 Statut Membre Dernière intervention 18 mars 2022 - Modifié le 8 déc. 2021 à 13:34
jordane45 Messages postés 38238 Date d'inscription mercredi 22 octobre 2003 Statut Modérateur Dernière intervention 6 septembre 2024 - 13 déc. 2021 à 20:54
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

jordane45 Messages postés 38238 Date d'inscription mercredi 22 octobre 2003 Statut Modérateur Dernière intervention 6 septembre 2024 4 689
8 déc. 2021 à 14:17
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
max30_3775 Messages postés 178 Date d'inscription jeudi 4 novembre 2021 Statut Membre Dernière intervention 18 mars 2022
8 déc. 2021 à 14:21
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
jordane45 Messages postés 38238 Date d'inscription mercredi 22 octobre 2003 Statut Modérateur Dernière intervention 6 septembre 2024 4 689
8 déc. 2021 à 19:06
Tu as des divs qui ne sont pas fermées non?
0
max30_3775 Messages postés 178 Date d'inscription jeudi 4 novembre 2021 Statut Membre Dernière intervention 18 mars 2022
Modifié le 8 déc. 2021 à 19:22
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
jordane45 Messages postés 38238 Date d'inscription mercredi 22 octobre 2003 Statut Modérateur Dernière intervention 6 septembre 2024 4 689
8 déc. 2021 à 19:27
Avant ta boucle, fais un var_dump de la variable $anouncement et montre nous ce que ça t'affiche
0
max30_3775 Messages postés 178 Date d'inscription jeudi 4 novembre 2021 Statut Membre Dernière intervention 18 mars 2022
8 déc. 2021 à 19:35
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

Vous n’avez pas trouvé la réponse que vous recherchez ?

Posez votre question
jordane45 Messages postés 38238 Date d'inscription mercredi 22 octobre 2003 Statut Modérateur Dernière intervention 6 septembre 2024 4 689
8 déc. 2021 à 21:31
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
max30_3775 Messages postés 178 Date d'inscription jeudi 4 novembre 2021 Statut Membre Dernière intervention 18 mars 2022
9 déc. 2021 à 11:24
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
jordane45 Messages postés 38238 Date d'inscription mercredi 22 octobre 2003 Statut Modérateur Dernière intervention 6 septembre 2024 4 689
9 déc. 2021 à 14:20
Pour l'image par défaut, reprend la ligne 4 du code que je t'ai donné...
0
jordane45 Messages postés 38238 Date d'inscription mercredi 22 octobre 2003 Statut Modérateur Dernière intervention 6 septembre 2024 4 689 > jordane45 Messages postés 38238 Date d'inscription mercredi 22 octobre 2003 Statut Modérateur Dernière intervention 6 septembre 2024
9 déc. 2021 à 14:21
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
Pitet Messages postés 2826 Date d'inscription lundi 11 février 2013 Statut Membre Dernière intervention 21 juillet 2022 524
9 déc. 2021 à 13:58
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
max30_3775 Messages postés 178 Date d'inscription jeudi 4 novembre 2021 Statut Membre Dernière intervention 18 mars 2022
9 déc. 2021 à 15:13
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
jordane45 Messages postés 38238 Date d'inscription mercredi 22 octobre 2003 Statut Modérateur Dernière intervention 6 septembre 2024 4 689
9 déc. 2021 à 15:22
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
tres bien jaimerais savoit ce que tu recherche
0
jordane45 Messages postés 38238 Date d'inscription mercredi 22 octobre 2003 Statut Modérateur Dernière intervention 6 septembre 2024 4 689
9 déc. 2021 à 15:29
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
max30_3775 Messages postés 178 Date d'inscription jeudi 4 novembre 2021 Statut Membre Dernière intervention 18 mars 2022
9 déc. 2021 à 15:47
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
jordane45 Messages postés 38238 Date d'inscription mercredi 22 octobre 2003 Statut Modérateur Dernière intervention 6 septembre 2024 4 689
9 déc. 2021 à 15:50
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
max30_3775 Messages postés 178 Date d'inscription jeudi 4 novembre 2021 Statut Membre Dernière intervention 18 mars 2022
9 déc. 2021 à 16:00
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
jordane45 Messages postés 38238 Date d'inscription mercredi 22 octobre 2003 Statut Modérateur Dernière intervention 6 septembre 2024 4 689
9 déc. 2021 à 16:14
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
max30_3775 Messages postés 178 Date d'inscription jeudi 4 novembre 2021 Statut Membre Dernière intervention 18 mars 2022
9 déc. 2021 à 16:22
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
jordane45 Messages postés 38238 Date d'inscription mercredi 22 octobre 2003 Statut Modérateur Dernière intervention 6 septembre 2024 4 689
9 déc. 2021 à 17:04
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
max30_3775 Messages postés 178 Date d'inscription jeudi 4 novembre 2021 Statut Membre Dernière intervention 18 mars 2022
Modifié le 9 déc. 2021 à 18:14
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
jordane45 Messages postés 38238 Date d'inscription mercredi 22 octobre 2003 Statut Modérateur Dernière intervention 6 septembre 2024 4 689
9 déc. 2021 à 18:24
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
max30_3775 Messages postés 178 Date d'inscription jeudi 4 novembre 2021 Statut Membre Dernière intervention 18 mars 2022
9 déc. 2021 à 19:17
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
jordane45 Messages postés 38238 Date d'inscription mercredi 22 octobre 2003 Statut Modérateur Dernière intervention 6 septembre 2024 4 689
9 déc. 2021 à 20:44
Tu as fait les var_dump pour voir ce que contiennent les variables ?
0
max30_3775 Messages postés 178 Date d'inscription jeudi 4 novembre 2021 Statut Membre Dernière intervention 18 mars 2022
9 déc. 2021 à 20:54
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
jordane45 Messages postés 38238 Date d'inscription mercredi 22 octobre 2003 Statut Modérateur Dernière intervention 6 septembre 2024 4 689
9 déc. 2021 à 21:12
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
max30_3775 Messages postés 178 Date d'inscription jeudi 4 novembre 2021 Statut Membre Dernière intervention 18 mars 2022
9 déc. 2021 à 21:27
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
jordane45 Messages postés 38238 Date d'inscription mercredi 22 octobre 2003 Statut Modérateur Dernière intervention 6 septembre 2024 4 689
9 déc. 2021 à 21:49
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
max30_3775 Messages postés 178 Date d'inscription jeudi 4 novembre 2021 Statut Membre Dernière intervention 18 mars 2022
9 déc. 2021 à 21:58
Propre! Super!!! le code marche parfaitement, rien à dire. Un sauveur comme dh'habitude. Grand merci à toi Jordane :)
0
max30_3775 Messages postés 178 Date d'inscription jeudi 4 novembre 2021 Statut Membre Dernière intervention 18 mars 2022
10 déc. 2021 à 16:35
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
jordane45 Messages postés 38238 Date d'inscription mercredi 22 octobre 2003 Statut Modérateur Dernière intervention 6 septembre 2024 4 689
10 déc. 2021 à 17:04
Cherche a comprendre le passage de paramètres dans une fonction...
0