Search
Left Quote    Maybe this world is another planet's Hell.
- Aldous Huxley    
Right Quote
 
[login] | [Register]
Forum Index » PHP and mySQL » Viewing Topic and Replies
Post Reply


        Viewing Topic: Number of Comments per article_id?
  This user is offline  UnknownZ
  Subject: "Number of Comments per article_id?" Posted: @ 8:52 am on Jun 08 2005   
Member #: 47
Rank: User - (29)
Since: 06/02/05
Posts: 29

bs0d, could you please tell me how to count it?


    Viewed: 57,012 Times | Reply to This | To top
  This user is offline  bs0d
  Subject: "re: Number of Comments per article_..." Posted: @ 11:31 am on Jun 08 2005    

Member #: 1
Rank: Admin. - (1,505)
Since: 02/06/05
Posts: 600
From: USA

Ok, its pretty easy. When I designed my table, I also created a column for tutorial id. Then, I do a query like this:


Code:


//query # of comments for given tutorial
$q = "SELECT * FROM `tute_comments` WHERE tute_id = '".$tute _id."'& quot;;
$q2 = mysql_query($q) ;
$total_comments = mysql_num_rows( $q2);






-bs0d | AllSyntax.com

    Viewed: 48,472 Times | Reply to This | To top
  This user is online  Guest
  Subject: "re: Number of Comments per article_..." Posted: @ 1:01 pm on Jun 08 2005    
Unregistered

My comments table is like this:

Code:


DROP TABLE IF EXISTS comments;
CREATE TABLE `comments` (
`article_id` int(11) NOT NULL default '0',
`id` int(11) NOT NULL auto_increment,
`page` varchar(255) NOT NULL default '',
`username` varchar(255) NOT NULL default 'Guest',
`subject` varchar(255) NOT NULL default '',
`contact` varchar(255) NOT NULL default '',
`comment` text NOT NULL,
`ip` varchar(15) NOT NULL default '0',
`date` varchar(255) NOT NULL default '',
`time` varchar(11) NOT NULL default '',
PRIMARY KEY (`id`)
) TYPE=MyISAM AUTO_INCREMENT= 51 ;



And here is the code where I call the number of comments:

Code:

<b>Articl e:</b> <# NAME #><br>
<b>By:< ;/b> <a href="cp/ profiles.php?m= <# AUTHOR #>"> ;<# AUTHOR #></a> on the <# DATE #><br>
<br>
<# FULL #>
<hr>
<?php
//query # of comments for given news
$q = "SELECT * FROM `comments` WHERE article_id = '".$id. "'" ;
$q2 = mysql_query($q) ;
$total_comments = mysql_num_rows( $q2);

echo '(<a href="../ comment.php?id= <# NEWSID #>"> ;', $total_comments ,'</a>' , ' Comments.' ;

?>
<br>
<hr>



And it shows up like this:
News & Updates

Article: Testing

Testing Testing Testing Full Description
--------------- --------------- --------------- --------------- --------------- -----
(9) Comments.

--------------- --------------- --------------- --------------- --------------- -----
Article: Testing 2

Testing Again 2
--------------- --------------- --------------- --------------- --------------- -----
(9) Comments.

The Number of comments are the same..



    Viewed: 48,468 Times | Reply to This | To top
  This user is offline  UnknownZ
  Subject: "re: Number of Comments per article_..." Posted: @ 1:23 pm on Jun 08 2005    
Member #: 47
Rank: User - (29)
Since: 06/02/05
Posts: 29

Sorry, the person above is me..




    Viewed: 48,465 Times | Reply to This | To top
  This user is offline  UnknownZ
  Subject: "re: Number of Comments per article_..." Posted: @ 1:23 pm on Jun 08 2005    
Member #: 47
Rank: User - (29)
Since: 06/02/05
Posts: 29

Post Deleted.




    Viewed: 48,464 Times | Reply to This | To top
  This user is offline  UnknownZ
  Subject: "re: Number of Comments per article_..." Posted: @ 1:23 pm on Jun 08 2005    
Member #: 47
Rank: User - (29)
Since: 06/02/05
Posts: 29

Post Deleted.




    Viewed: 48,463 Times | Reply to This | To top
  This user is offline  UnknownZ
  Subject: "re: Number of Comments per article_..." Posted: @ 1:45 pm on Jun 08 2005    
Member #: 47
Rank: User - (29)
Since: 06/02/05
Posts: 29

wakk...
I don't mean to post 3 times..and I don't know how to delete them..sorry...



Edited at 01:48:51 pm on 06/08/05


    Viewed: 48,461 Times | Reply to This | To top
  This user is offline  bs0d
  Subject: "re: Number of Comments per article_..." Posted: @ 3:03 pm on Jun 08 2005    

Member #: 1
Rank: Admin. - (1,505)
Since: 02/06/05
Posts: 600
From: USA

well, you get the article id with $_GET['id'];

like if your url was like this:

site.com/articl es/view.php?id= 5

$_GET['id'] would = 5. So you can change that $id to $_GET['id'] in your query, should work




-bs0d | AllSyntax.com

    Viewed: 48,451 Times | Reply to This | To top
  This user is offline  UnknownZ
  Subject: "re: Number of Comments per article_..." Posted: @ 10:10 pm on Jun 08 2005    
Member #: 47
Rank: User - (29)
Since: 06/02/05
Posts: 29

actually I get that one right, but I could not show total comments per article_id, for example article_id 1 has 9 comments, and there is no article_id 2, but it also shows up 9 comments..pleas e see my MySQL table:

Code:


article_id id page username subject contact comment ip date time
1 103 /comment.php?id =1 demo agag http://tatata agagag ip.xxx.xxx.xxx June 4, 2005. 1117907115
1 104 /comment.php?id =1 demo adfadfa http://adfada afahkhkjlafj akfajljaflj afjldajlajljfal j ip.xxx.xxx.xxx June 4, 2005. 1117908464
1 105 /comment.php?id =1 demo test http://test test ip.xxx.xxx.xxx June 4, 2005. 1117909688
1 106 /comment.php?id =1 demo afaaff http://afadafda afadfaadf ip.xxx.xxx.xxx June 4, 2005. 1117911076
1 107 /comment.php?id =1 Admin working http://working Yessssssss.. ip.xxx.xxx.xxx June 5, 2005. 1117993375
1 108 /comment.php?id =1 Admin yay http://asdf yay1 ip.xxx.xxx.xxx June 6, 2005. 1118100746
1 101 /comment.php?id =1 demo test http://test tes ip.xxx.xxx.xxx June 4, 2005. 1117906220
1 102 /comment.php?id =1 demo tssts http://tsts tsts ip.xxx.xxx.xxx June 4, 2005. 1117906567
1 93 /comment.php?id =1 demo see http://see not working agajlajldafjl ip.xxx.xxx.xxx June 4, 2005. 1117900086



as you can see, there is only article_id 1 with 9 comments, I don't have article_id 2 yet..please take a look at my website:
http://www.temp lateszone.biz
username: demo ... password: demo

Thanks.



Edited at 10:33:16 pm on 06/08/05


    Viewed: 48,446 Times | Reply to This | To top
  This user is offline  bs0d
  Subject: "re: Number of Comments per article_..." Posted: @ 12:08 am on Jun 08 2005    

Member #: 1
Rank: Admin. - (1,505)
Since: 02/06/05
Posts: 600
From: USA

well; what if when you do have more than 1 article? you'll use a while loop to display each article and respective comments. so...

do something like:


Code:


<?
while($total_co mments = mysql_num_rows( $q2)) {
echo("(&l t;a href='../comme nt.php?id=&quo t;.$id." ', ".$total_ comments." ; ,'</a>' , ' Comments.')&q uot;
}
?>






-bs0d | AllSyntax.com

    Viewed: 48,439 Times | Reply to This | To top
  This user is offline  UnknownZ
  Subject: "re: Number of Comments per article_..." Posted: @ 5:47 am on Jun 09 2005    
Member #: 47
Rank: User - (29)
Since: 06/02/05
Posts: 29

LoL..it's flooding...

News & Updates

Article: Testing
By: Admin on the 22-09-2003 @ 18:25:18

Testing Testing Testing Full Description
--------------- --------------- --------------- --------------- --------------- -----
(9) Comments.(9) Comments.(9) Comments.(9) Comments.(9) Comments.(9) Comments.(9) Comments.(9) Comments.(9) Comments.(9) Comments.(9) Comments.(9) Comments.(9) Comments.(9) Comments.(9) Comments.(9) Comments.(9) Comments.(9) Comments.(9) Comments.(9) Comments.(9) Comments.(9) Comments.(9) Comments.(9) Comments.(9) Comments.(9) Comments.(9) Comments.(9) Comments.(9) Comments.(9) Comments.(9) Comments.(9) Comments.(9) Comments.(9) Comments.(9) Comments.(9)

..........
Did I do something wrong?


Code:


<b>Articl e:</b> <# NAME #><br>
<b>By:< ;/b> <a href="htt p://www.templat eszone.biz/cp/p rofiles.php?m=& lt;# AUTHOR #>"> ;<# AUTHOR #></a> on the <# DATE #><br>
<br>
<# FULL #>
<hr>
<?php
//query # of comments for given news
$q = "SELECT * FROM `comments` WHERE article_id = '".$id. "'" ;
$q2 = mysql_query($q) ;
$total_comments = mysql_num_rows( $q2);

while($total_co mments = mysql_num_rows( $q2)) {

echo '(<a href="../ comment.php?id= <# NEWSID #>"> ;', $total_comments ,'</a>' , ' Comments.' ;
}
?>
<br>
<hr>




    Viewed: 48,436 Times | Reply to This | To top
  This user is offline  bs0d
  Subject: "re: Number of Comments per article_..." Posted: @ 11:48 am on Jun 09 2005    

Member #: 1
Rank: Admin. - (1,505)
Since: 02/06/05
Posts: 600
From: USA

do this...


Code:


<?
$total_comments = mysql_num_rows( $q2);
while($q3 = mysql_fetch_obj ect($q2)) {
echo '(<a href="../ comment.php?id= <# NEWSID #>"> ;', $total_comments ,'</a>' , ' Comments.' ;
}
?>



Edited at 11:50:25 am on 06/09/05


-bs0d | AllSyntax.com

    Viewed: 48,432 Times | Reply to This | To top
  This user is offline  UnknownZ
  Subject: "re: Number of Comments per article_..." Posted: @ 3:03 pm on Jun 09 2005    
Member #: 47
Rank: User - (29)
Since: 06/02/05
Posts: 29

It's not working too.. ..please help..

Here is the result:
=============== =============== ===
Article: Testing
By: Admin on the 22-09-2003 @ 18:25:18

Testing Testing Testing Full Description
--------------- --------------- --------------- --------------- --------------- -----
(9) Comments.(9) Comments.(9) Comments.(9) Comments.(9) Comments.(9) Comments.(9) Comments.(9) Comments.(9) Comments.

--------------- --------------- --------------- --------------- --------------- -----
Article: Testing 2
By: on the 31-12-1969 @ 18:00:12

Testing Again 2
--------------- --------------- --------------- --------------- --------------- -----
(9) Comments.(9) Comments.(9) Comments.(9) Comments.(9) Comments.(9) Comments.(9) Comments.(9) Comments.(9) Comments.




    Viewed: 48,427 Times | Reply to This | To top
  This user is offline  bs0d
  Subject: "re: Number of Comments per article_..." Posted: @ 3:48 pm on Jun 09 2005    

Member #: 1
Rank: Admin. - (1,505)
Since: 02/06/05
Posts: 600
From: USA

Hrm.. well, im not sure. I will have to look into this more in detail when im off work today. Until then, maybe Misterhaan will catch this thread and offer some help




-bs0d | AllSyntax.com

    Viewed: 48,423 Times | Reply to This | To top
  This user is offline  UnknownZ
  Subject: "re: Number of Comments per article_..." Posted: @ 4:34 pm on Jun 09 2005    
Member #: 47
Rank: User - (29)
Since: 06/02/05
Posts: 29

LoL..
please help me when you are back...




    Viewed: 48,419 Times | Reply to This | To top
  This user is offline  misterhaan
  Subject: "re: Number of Comments per article_..." Posted: @ 4:20 pm on Jun 09 2005    

Member #: 5
Rank: User - (213)
Since: 02/11/05
Posts: 148
From: chair

drop the while from bs0d's last post with code -- it's looping over all the comments and writing out the total number of comments each time.




please note that the above post is likely made up in its entirety.

    Viewed: 48,418 Times | Reply to This | To top
  This user is offline  UnknownZ
  Subject: "re: Number of Comments per article_..." Posted: @ 4:36 pm on Jun 09 2005    
Member #: 47
Rank: User - (29)
Since: 06/02/05
Posts: 29

Thank you for the post, misterhaan..but the problem is back again..I could not count the number of comments per article_id ..see here:
------------- News & Updates

Article: Testing
By: Admin on the 22-09-2003 @ 18:25:18

Testing Testing Testing Full Description
--------------- --------------- --------------- --------------- --------------- -----
(9) Comments.

--------------- --------------- --------------- --------------- --------------- -----
Article: Testing 2
By: on the 31-12-1969 @ 18:00:12

Testing Again 2
--------------- --------------- --------------- --------------- --------------- -----
(9) Comments.

--------------< br />
There is no comment for Article 2 but it still counts as 9 comments..




    Viewed: 48,415 Times | Reply to This | To top
  This user is offline  bs0d
  Subject: "re: Number of Comments per article_..." Posted: @ 6:56 pm on Jun 09 2005    

Member #: 1
Rank: Admin. - (1,505)
Since: 02/06/05
Posts: 600
From: USA

follow along this code with your proper table names and column names

Code:


<?
$q_articles =mysql_query(& quot;SELECT * FROM `articles`&quo t;
if(!$q_articles ) die(mysql_error ());

while($show = mysql_fetch_obj ect($q_articles )) {

echo("art icle name: ".$show-& gt;name." id: ".$show-& gt;id."&l t;BR>"< img src=http://www. allsyntax.com/f orums/images/sm ilies/wink.gif border=0>;

//now query comments for this article
$q2 = mysql_query(&q uot;SELECT * FROM `comments` WHERE `article_id` = '".$show ->id." '";
$how_many = mysql_num_rows( $q2);
if($how_many >= 0) {
echo(" 0 comments"< img src=http://www. allsyntax.com/f orums/images/sm ilies/wink.gif border=0>;
} else {
echo("thi s article has ".$how_ma ny." comments." ; <BR> ";
}//end else
} //end while
?>




-bs0d | AllSyntax.com

    Viewed: 48,408 Times | Reply to This | To top
  This user is offline  UnknownZ
  Subject: "re: Number of Comments per article_..." Posted: @ 11:31 pm on Jun 09 2005    
Member #: 47
Rank: User - (29)
Since: 06/02/05
Posts: 29

bs0d....thank you...it's working...
but I must change
if($how_many >= 0) {

to

if($how_many == 0) {

Do you think is that okay?


Here it is how it shows up:
article name: Testing id: 1this article has 9 comments.
article name: Testing 2 id: 2this article has 0 comments.



Edited at 12:09:36 am on 06/10/05


    Viewed: 48,402 Times | Reply to This | To top
  This user is offline  bs0d
  Subject: "re: Number of Comments per article_..." Posted: @ 12:23 am on Jun 09 2005    

Member #: 1
Rank: Admin. - (1,505)
Since: 02/06/05
Posts: 600
From: USA

eh, that was suppose to be less than or equal to, my bad. you're all fixed up!




-bs0d | AllSyntax.com

    Viewed: 48,398 Times | Reply to This | To top
  This user is offline  UnknownZ
  Subject: "re: Number of Comments per article_..." Posted: @ 10:55 pm on Jun 10 2005    
Member #: 47
Rank: User - (29)
Since: 06/02/05
Posts: 29

thank you..




    Viewed: 48,389 Times | Reply to This | To top
Viewing Page: 1 of 3


1 | 2 | 3 |
Next »





"AllSyntax.com" Copyright © 2002-2018; All rights lefted, all lefts righted.
Privacy Policy  |  Internet Rank