title: Get number of posts in a topic PHP
body: <p>How do I get to display the number of posts on a topic like a forum. I used this... (how very noobish):</p>
  
  <pre><code>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);
  }
  </code></pre>
  
  <p>The while loop of listing topics:</p>
  
  <pre><code>&lt;?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))
  {
  ?&gt;
  &lt;div class="topics"&gt;
      &lt;div class="topic-name"&gt;
          &lt;p&gt;&lt;?php echo $row['topic_title']; ?&gt;&lt;/p&gt;
      &lt;/div&gt;
      &lt;div class="topic-posts"&gt;
          &lt;p&gt;&lt;?php echo numberofposts($row['topic_id']); ?&gt;&lt;/p&gt;
      &lt;/div&gt;
  &lt;/div&gt;
  &lt;?php
  }
  ?&gt;
  </code></pre>
  
  <p>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</p>
  
  <p>Thanks.</p>
  
  <p><strong>EDIT:</strong></p>
  
  <p><strong>SQL:</strong></p>
  
  <p>Posts table:</p>
  
  <pre><code>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, '');
  </code></pre>
  
  <p>Topics table:</p>
  
  <pre><code>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 ;
  </code></pre>
  
  <p>This should help you understand a bit more.</p>