Comment puis-je afficher le nombre de messages sur un sujet comme un forum. J'ai utilisé cette ... (comment très noobish):Obtenir le nombre de messages dans un sujet PHP
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);
}
La boucle while de sujets: liste
<?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
}
?>
Bien qu'il soit une mauvaise méthode de le faire ... Tout ce que je besoin est de savoir quelle serait la meilleure méthode, ne me signalez pas un site Web, faites-le ici, parce que j'essaie d'apprendre beaucoup. D'accord? : D
Merci.
EDIT:
SQL:
Table Messages:
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, '');
table Sujets:
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 ;
Cela devrait vous aider à comprendre un mor bits e.
"SELECT COUNT (topic_id) comme totalCount de messages OU topic_id = '?" Vous pouvez ensuite sélectionner la ligne "totalCount" –
Woah, woah, woah, quel est le $ n? – MacMac
Avez-vous écrit le code dans votre message original cos '$ n' est la même chose que vous l'avez écrit comme là (l'ID de sujet)! –