zhangy Posted December 24, 2008 Share Posted December 24, 2008 Merry Christmas! I have a sort of makeshift forum and want to keep track of the number of replies each post gets. I want the number of replies to be displayed next to the each post's topic link (basically just like the way its displayed here on the phpfreaks forum, and most other forums for that matter). Replies are stored in the same table column as the topics but the two are identified by their ids and relate by how the replies topic_id correlates to the topics submission_id. Any help would be appreciated. Quote Link to comment https://forums.phpfreaks.com/topic/138309-solved-replies/ Share on other sites More sharing options...
Maq Posted December 24, 2008 Share Posted December 24, 2008 the two are identified by their ids and relate by how the replies topic_id correlates to the topics submission_id. Can you just show the table structure? I'm not sure what exactly you mean here, anyway... Why can't you just use a simple query...? $sql = "SELECT replies FROM table WHERE topic_id = '$topic_id'"; $result = mysql_query($sql); $num_replies = mysql_num_rows($result); echo "Replies: " . $num_replies; Quote Link to comment https://forums.phpfreaks.com/topic/138309-solved-replies/#findComment-723172 Share on other sites More sharing options...
.josh Posted December 24, 2008 Share Posted December 24, 2008 Replies are stored in the same table column as the topics but the two are identified by their ids and relate by how the replies topic_id correlates to the topics submission_id. How do you select the replies for display? Just do the same thing you did there, but instead of selecting the info, just count the rows returned, using COUNT Quote Link to comment https://forums.phpfreaks.com/topic/138309-solved-replies/#findComment-723175 Share on other sites More sharing options...
zhangy Posted December 24, 2008 Author Share Posted December 24, 2008 I'm currently using the following code but instead of returning the number of replies it displays "Resource ID #..." <?php require_once('fLoad.php'); $result = mysql_query("SELECT submission_id, col_1, submission_date FROM $table WHERE topic_id = '0' ORDER BY submission_id DESC"); if (!$result) { die("Query to show fields from table failed:".mysql_error()); } while($row = mysql_fetch_array($result)) { $sql = "SELECT COUNT(*) FROM $table WHERE topic_id = '". $_GET['id'] ."'"; $comments = mysql_query($sql) or die("Error ". mysql_error(). " with query ". $sql); echo "<div><strong>"; echo '<a href="topic.php?id='.$row['submission_id'].'">'.$row['col_1'].'</a>'; echo $comments; echo "</strong><br/>"; echo date("l M dS, Y", $row['submission_date']); echo "</div>"; } mysql_free_result($result); ?> Quote Link to comment https://forums.phpfreaks.com/topic/138309-solved-replies/#findComment-723192 Share on other sites More sharing options...
Maq Posted December 24, 2008 Share Posted December 24, 2008 Try this: $comments = mysql_query($sql) or die("Error ". mysql_error(). " with query ". $sql); $row2 = mysql_fetch_array($comments); echo ""; echo ''.$row['col_1'].''; echo $row2['COUNT(replies)']; echo " "; echo date("l M dS, Y", $row['submission_date']); echo ""; Quote Link to comment https://forums.phpfreaks.com/topic/138309-solved-replies/#findComment-723200 Share on other sites More sharing options...
zhangy Posted December 24, 2008 Author Share Posted December 24, 2008 Not sure if i implemented that correctly cause nothing displayed where the reply count should have been... However, I seem to get a more understanable result with the following code. But it displays the total number of replies that are in the table column instead of just the specific number of of replies to each individual topic. For example, I have three topics posted, but only one has comments (three), yet each topic has the number "3" showing as their reply count. <?php require_once('fLoad.php'); $result = mysql_query("SELECT submission_id, col_1, submission_date FROM $table WHERE topic_id = '0' ORDER BY submission_id DESC"); if (!$result) { die("Query to show fields from table failed:".mysql_error()); } while($row = mysql_fetch_array($result)) { $sql = "SELECT * FROM $table WHERE topic_id = '". $_GET['id'] ."'"; $comments = mysql_query($sql) or die("Error ". mysql_error(). " with query ". $sql); $count = mysql_num_rows($comments); echo "<div><strong>"; echo '<a href="topic.php?id='.$row['submission_id'].'">'.$row['col_1'].'</a>'; echo $count; echo "</strong><br/>"; echo date("l M dS, Y", $row['submission_date']); echo "</div>"; } mysql_free_result($result); ?> Quote Link to comment https://forums.phpfreaks.com/topic/138309-solved-replies/#findComment-723203 Share on other sites More sharing options...
Maq Posted December 24, 2008 Share Posted December 24, 2008 1) Please use either the COUNT method or the mysql_num_rows method. 2) Where does $table come from? 3) Is submission_id the ACTUAL topic id and topic_id is what topic the reply is associated with? If so, you want to match the topic id with the submission_id. Your query should look like: $sql = "SELECT * FROM $table WHERE topic_id = " . $row['submission_id']; (You don't need single quotes around integers.) Quote Link to comment https://forums.phpfreaks.com/topic/138309-solved-replies/#findComment-723215 Share on other sites More sharing options...
zhangy Posted December 24, 2008 Author Share Posted December 24, 2008 Argh!! I've been staring at this screen too long me thinks. Thanks Maq that did it. Quote Link to comment https://forums.phpfreaks.com/topic/138309-solved-replies/#findComment-723219 Share on other sites More sharing options...
Maq Posted December 24, 2008 Share Posted December 24, 2008 great, no prob mark as solved pls Quote Link to comment https://forums.phpfreaks.com/topic/138309-solved-replies/#findComment-723221 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.