Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 10 of 10
  1. #1
    New Coder
    Join Date
    Oct 2012
    Posts
    19
    Thanks
    12
    Thanked 0 Times in 0 Posts

    how do I echo the # of comments from database??

    So, I have created a blog for myself, which allows comments.

    I want to display the # of comments on the web page like so....

    Comments(9)

    >>>> and then the nine comments follow...

    My question is this. In this example, there are 9 comments to be displayed.

    How do I use PHP to echo the "9", or number of comments that are in the database?

    I know this is probably a very beginner question, but I can't find an answer anywhere.

    Does that make sense well enough?????

  • #2
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,987
    Thanks
    4
    Thanked 2,660 Times in 2,629 Posts
    This is more of a SQL question, but you haven't posted any code, so help will be limited. It's a matter of issuing a COUNT() call with the appropriate GROUP BY clause and providing it a valid WHERE. Then fetch that result which is the number of associated comments count.
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 

  • Users who have thanked Fou-Lu for this post:

    rhender (03-25-2013)

  • #3
    New Coder
    Join Date
    Oct 2012
    Posts
    19
    Thanks
    12
    Thanked 0 Times in 0 Posts
    Thanks so much. That definitely points me in the right direction. COUNT is definitely what I was looking for.

    I can upload some code in a little bit to try and show where I am at.

  • #4
    New Coder
    Join Date
    Oct 2012
    Posts
    19
    Thanks
    12
    Thanked 0 Times in 0 Posts
    Ok, so here is the code I have right now.

    The table that holds the comments is called 'comments'

    Code:
    <div id="comment-section">
    		
    	<h1 class="comments-title">Comments(# of comments from database)</h1>
    	
    	<?php
    	$query = "SELECT * FROM comments WHERE post_id = $id AND approved = 1";
    	  $result = mysqli_query($dbc, $query);
    		while($row = mysqli_fetch_array($result)) {  //open up the loop to gather database on info on news article
    	?>
    	
    	<p id="author-of"><?php echo $row['author']; //echo $row['date']; ?></p>
    	<p id="show-comment"><?php echo nl2br($row['comment']); ?></p>
    	<?php } ?>
    	
    	<!-- end comment section --></div>

  • #5
    New Coder
    Join Date
    Oct 2012
    Posts
    19
    Thanks
    12
    Thanked 0 Times in 0 Posts
    So I need the "# of comments from database" to be dynamic, obviously.

    Below that <h1>, which is supposed to display # of comments... is the list of comments with the author's name, just like any blog has. I used the while loop to gather those comments and display them on the page. They are working just fine.

    Displaying the # of comments is my last step!!

  • #6
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,987
    Thanks
    4
    Thanked 2,660 Times in 2,629 Posts
    I see, so you want to show something like comments(9), then followed by the actual content of the posts correct?
    In this case, a COUNT is probably not worth the effort UNLESS you are going to paginate it in which case you'll want to count. To do that, its a simple matter of:
    PHP Code:
    $sQry "SELECT COUNT(*) FROM comments WHERE post_id = $id AND approved = 1 GROUP BY post_id"
    Then just query and pull the first result.

    If you need all the comments, then using a row count would be easier:
    PHP Code:
    <div id="comment-section">
    <?php
    $query 
    "SELECT * FROM comments WHERE post_id = $id AND approved = 1";
    $result mysqli_query($dbc$query);
    if (
    mysqli_num_rows($result) > 0)
    {
        
    printf('<h1 class="comments-title">Comments(%d)</h1>'mysqli_num_rows($result));
    // now continue with the looping
    }
    Like that.
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 

  • Users who have thanked Fou-Lu for this post:

    rhender (03-25-2013)

  • #7
    New Coder
    Join Date
    Oct 2012
    Posts
    19
    Thanks
    12
    Thanked 0 Times in 0 Posts
    YES!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!



    FOU LOU you are the man. Thank you so much.

    I used the second option and it worked like a charm. I don't really understand it at all yet.

    The
    Code:
    printf
    and
    Code:
    %d
    , I haven't dealt with or seen before... maybe that shows how new I am to this. But man am I happy to be done with building my blog now. It has been a while coming. Thank you so much. Fantastic.

  • #8
    New Coder
    Join Date
    Oct 2012
    Posts
    19
    Thanks
    12
    Thanked 0 Times in 0 Posts
    Sorry, mispell.

    Thank you FOU-LU

  • #9
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,987
    Thanks
    4
    Thanked 2,660 Times in 2,629 Posts
    printf = print format
    %d within print format = argument to be treated as an integer.
    If you're interested in using it, you can check the php api at http://php.net/manual/en/function.sprintf.php for the specifiers. sprintf is the same as printf except it returns a result instead of putting it on the output stream.
    I mucho prefer the [s]printf over echo since I can do argument swapping in the format string. It is a bit slower than the standard echo or print though (which is. . . a little strange).
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 

  • Users who have thanked Fou-Lu for this post:

    rhender (03-25-2013)

  • #10
    New Coder
    Join Date
    Oct 2012
    Posts
    19
    Thanks
    12
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by Fou-Lu View Post
    printf = print format
    %d within print format = argument to be treated as an integer.
    If you're interested in using it, you can check the php api at http://php.net/manual/en/function.sprintf.php for the specifiers. sprintf is the same as printf except it returns a result instead of putting it on the output stream.
    I mucho prefer the [s]printf over echo since I can do argument swapping in the format string. It is a bit slower than the standard echo or print though (which is. . . a little strange).
    Wow, thank you so much. This stuff is getting very interesting to me now. Loads to learn before I can feel comfortable, but thanks so much.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •