Skip to main content
Participant
June 11, 2008
Question

Display record count in a comments application

  • June 11, 2008
  • 1 reply
  • 283 views
Hi, in my application, i try to display the number of comments for each subject in a list. The problem is that the total number of records are returned by the loop query for each subject, even if the there is no comments for the subject related. Anybody can help me? Here is the code :

<cfquery name="Qsujets" datasource="Modules_Web">
SELECT *
FROM dbo.Sujet
ORDER BY SUJ_ID
DESC
</cfquery>
<cfquery name="Qcommentaire" datasource="Modules_Web">
SELECT *
FROM dbo.Commentaire
</cfquery>


<html>
<head>
<title>Liste des sujets</title>
<link href="/sites/1676/commentaires/CSS/styles.css" rel="stylesheet" type="text/css" />
</head>

<body>
<h1>Liste des articles </h1>
<br />
<cfoutput query="Qsujets">
<ul><li>
<span class="sujets">#Qsujets.SUJ_Nom#</span><br />
<span class="auteur">#Qsujets.SUJ_Auteur#</span><br />
<span class="description">#Qsujets.SUJ_Description#</span><br />
<div id="bloc_sujet">
<table>
<tr>
<td><img src="/sites/1676/commentaires/images/oeil.gif" alt="Lire les réactions " width="20" height="20" /></td>
<td><a href="/index.cfm/2,0,1676,9914,2832,0,html?COM_ID=#Qsujets.SUJ_ID#">Lire les r&eacute;actions &agrave; cet article </a> (#Qcommentaire.RecordCount#)</td>
<br />
</tr>
<tr>
<td><img src="/sites/1676/commentaires/images/comment.gif" alt="Réagir" width="20" height="19" /></td>
<td><a href="liste_com.cfm?COM_ID=#Qsujets.SUJ_ID#"></a><a href="/index.cfm/2,0,1676,9914,2831,0,html?SUJ_ID=#Qsujets.SUJ_ID#">R&eacute;agir &agrave; cet article</a></td>
</tr>
</table>
______________________________________</div>
</li></ul>
</cfoutput>
</body>
</html>
This topic has been closed for replies.

1 reply

Inspiring
June 11, 2008
something like this?:

SELECT dbo.Sujet.*, COUNT(dbo.Commentaire.*) AS commentscount
FROM dbo.Sujet LEFT JOIN dbo.Commentaire ON dbo.Sujet.SUJ_ID =
dbo.Commentaire.SUJ_ID
GROUP BY dbo.Sujet.SUJ_ID
ORDER BY dbo.Sujet.SUJ_ID DESC

i suggest you list just the necessary fields in your queries instead of
using SELECT *.

Azadi Saryev
Sabai-dee.com
http://www.sabai-dee.com/