views:

64

answers:

4

How do I get to display the number of posts on a topic like a forum. I used this... (how very noobish):

function numberofposts($n)
{
    $sql = "SELECT * FROM posts
            WHERE topic_id = '" . $n . "'";

    $result = mysql_query($sql) or die(mysql_error());          
    $count = mysql_num_rows($result);           

    echo number_format($count);
}

The while loop of listing topics:

<?php

$sql = "SELECT * FROM topics ORDER BY topic_id ASC LIMIT $start, $limit";
        $result = mysql_query($sql) or die(mysql_error());

while($row = mysql_fetch_array($result))
{
?>
<div class="topics">
    <div class="topic-name">
        <p><?php echo $row['topic_title']; ?></p>
    </div>
    <div class="topic-posts">
        <p><?php echo numberofposts($row['topic_id']); ?></p>
    </div>
</div>
<?php
}
?>

Although it is a bad method of doing this... All I need is to know what would be the best method, don't just point me out to a website, do it here, because I'm trying to learn much. Okay? :D

Thanks.

EDIT:

SQL:

Posts table:

CREATE TABLE `posts` (
  `post_id` mediumint(8) NOT NULL AUTO_INCREMENT,
  `topic_id` mediumint(8) NOT NULL,
  `forum_id` mediumint(8) NOT NULL,
  `user_id` mediumint(8) NOT NULL,
  `post_time` varchar(100) NOT NULL,
  `post_timestamp` mediumint(20) NOT NULL,
  `post_ip` varchar(20) NOT NULL,
  `post_reported` tinyint(1) NOT NULL,
  `post_reportdesc` text NOT NULL,
  PRIMARY KEY (`post_id`)
) ENGINE=MyISAM  DEFAULT CHARSET=latin1 AUTO_INCREMENT=2 ;

--
-- Dumping data for table `posts`
--

INSERT INTO `posts` VALUES(1, 1, 0, 1, '15th Junenee', 0, '', 0, '');

Topics table:

CREATE TABLE `topics` (
  `topic_id` mediumint(8) NOT NULL AUTO_INCREMENT,
  `section_name` varchar(25) NOT NULL,
  `topic_title` varchar(120) NOT NULL,
  `topic_description` char(120) NOT NULL,
  `user_id` mediumint(8) NOT NULL,
  `topic_time` varchar(100) NOT NULL,
  `topic_views` varchar(1000) NOT NULL,
  `topic_up` mediumint(11) NOT NULL,
  `topic_down` mediumint(11) NOT NULL,
  `topic_status` tinyint(1) NOT NULL,
  PRIMARY KEY (`topic_id`)
) ENGINE=MyISAM  DEFAULT CHARSET=latin1 AUTO_INCREMENT=1 ;

This should help you understand a bit more.

+2  A: 

You can use:

"SELECT COUNT(topic_id) FROM posts WHERE topic_id = '?'"

The ? is a place-holder. If you use mysql, you should use mysql_real_escape_string:

$sql = "SELECT COUNT(topic_id)
        WHERE topic_id = '" . mysql_real_escape_string($n) . "'";

If you use mysql_fetch_array, $row[0] will be the count. You can name it but it's not necessary.

A better option is some kind of prepared statements, such as PDOStatement or mysqli_stmt. This helps prevent SQL injection.

Matthew Flaschen
"SELECT COUNT(topic_id) as totalCount FROM posts WHERE topic_id = '?'"You can then select the row "totalCount"
Tom Gullen
Woah, woah, woah, what is the $n?
YouBook
Did you write the code in your original post cos `$n` is the same thing you wrote it as there (the topic id)!
Can you get this to be implemented into my code in my OP. Thanks.
YouBook
A: 

You could use this SQL query instead if you only want the number of records:

SELECT COUNT(topic_id) AS 'count' WHERE topic_id = '123'

Then after you do:

$result = mysql_query($sql);
if ($result !== false)
{
    $row = mysql_fetch_assoc($result);
    if ($row !== false)
        echo('Number of rows = ' . $row['count']);
}
AlexV
I can't get this to work... can you implement it to my code, I've tried to put it out with your code into mine but it doesn't work...
YouBook
Tell me the following so I understand more your question: topic_id is unique? You want to display the title of the post AND the position of the post in the database? Must that position be unique and never change or it's just for show?
AlexV
I've edited the OP, added the database tables so you can see what there is.
YouBook
You want to display the title of the post AND the position of the post in the database? Must that position be unique and never change or it's just for show?
AlexV
Well, basically the while loop is displayed on the index.php which will list all the topics and has the number of posts per row in the list.
YouBook
And does this number must never change? I mean can I just use it's position in the database (which can change if more records are added/deleted or if the ORDER BY changes).
AlexV
Like the mysql_num_rows() of the posts table, but I need it to be specific in their own topic like a forum.
YouBook
mysql_num_rows returns the row count in the last SELECT/SHOW query, not the position of a row in a group... I't not sure to follow you at all. What you ask is really simple to do, but I'm not sure what you expect exactly.
AlexV
A: 
SELECT topic_title, COUNT(*) AS toipic_count
FROM topics
GROUP BY topic_id
ORDER BY topic_id ASC

Then you will not need any more mysql-querys, just use:

$row['topic_count']
Tobias P.
A: 

When you're listing topics, you should include the post count in a join:

SELECT t.*, COUNT(p.post_id) AS post_count
FROM topic t LEFT JOIN posts p ON p.topic = t.topic_id 
GROUP BY t.topic_id

Didn't study your schema too much, but you get the point.

Tor Valamo