How to count the number of comments and display them.

Solved
flo39400 Posted messages 659 Status Member -  
flo39400 Posted messages 659 Status Member -
Hello,

I have several articles all stored in a database in a table.
I have links for each article based on their ID.
The comments are all placed in a table: "comments".

So this link is generated for each article:
 <a href="show_comment.php?id=<?php echo $data['news_id']; ?>" onclick="window.open(this.href); return false;">Comment</a> 


I would like to display the number of comments and add an S to comment when it is greater than 1.

Thank you in advance.

Configuration: Windows 10,
Intel core I7
32 GB RAM
1 TB HDD
512 GB SSD
NVIDIA® GeForce GTX700 series graphics card
optimal internet connection,...

1 answer

  1. jordane45 Posted messages 30427 Registration date   Status Moderator Last intervention   4 833
     
    Hello,

    You can possibly add a COUNT in your query
    Or execute a second query that gives you the count
    Or, if you're using PDO, use a FETCHALL followed by a COUNT (this time in PHP) on it

    --
    Best regards,
    Jordane
    0
    1. flo39400 Posted messages 659 Status Member 21
       
      Yes, I would like to, but I don’t know how to phrase it.

      I have an error message:
      Parse error: syntax error, unexpected '' (T_ENCAPSED_AND_WHITESPACE), expecting identifier (T_STRING) or variable (T_VARIABLE) or number (T_NUM_STRING)

      My code:

       $totalcommentaireReqID = $bdd->query("SELECT COUNT(*) FROM commentaires WHERE id_article = '$donnees['news_id']'"); $totalcommentaireID = $totalcommentaireReqID->fetchAll(); 
      0
    2. Anonymous user > flo39400 Posted messages 659 Status Member
       
      The error comes from a misuse between single and double quotes, as you open single quotes for your article ID, but you also use them for your index in your array. I advise you to store your ID first in a single variable.

      $id_article = $donnees['news_id']; $totalcommentaireReqID = $bdd->query("SELECT COUNT(*) FROM commentaires WHERE id_article = '$id_article'"); $totalcommentaireID = $totalcommentaireReqID->fetchAll();
      0
    3. flo39400 Posted messages 659 Status Member 21
       
      I'm making progress, but I can't display the value.

       <?php $articles = $bdd->query('SELECT * FROM news_tab_articles WHERE (news_publier = 1) ORDER BY news_id DESC LIMIT '.$depart.','.$articlesParPage); while($donnees = $articles->fetch(PDO::FETCH_ASSOC)) { ?> <p class="article_text"><?= $donnees['news_titre'] ?></p> <?= $donnees['news_contenu'] ?> <?php $id_article = $donnees['news_id']; $totalcommentaireReqID = $bdd->query("SELECT COUNT(*) FROM commentaires WHERE id_article = '$id_article'"); $totalcommentaireID = $totalcommentaireReqID->fetchAll(); ?> <div class="com"> <a href="afficher_com.php?id=<?php echo $donnees['news_id']; ?>" onclick="window.open(this.href); return false;"><?php echo $totalcommentaireID; ?> Commentaire</a> </div> <?php } $articles->closeCursor(); ?> 



      My <?php echo $totalcommentaireID; ?> is not working...
      0
    4. Anonymous user > flo39400 Posted messages 659 Status Member
       
      And if you do a var_dump of $totalcommentaireID, what do you get?

      var_dump($totalcommentaireID);
      0
    5. flo39400 Posted messages 659 Status Member 21
       
      Here:

      C:\wamp64\www\2017\baspage.php:43:
      array (size=1)
      0 =>
      array (size=1)
      'COUNT(*)' => string '3' (length=1)
      0