Jump to content

Recommended Posts

Merry Christmas!  ;D

 

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.

Link to comment
https://forums.phpfreaks.com/topic/138309-solved-replies/
Share on other sites

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;

 

Link to comment
https://forums.phpfreaks.com/topic/138309-solved-replies/#findComment-723172
Share on other sites

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

Link to comment
https://forums.phpfreaks.com/topic/138309-solved-replies/#findComment-723175
Share on other sites

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);
?>

Link to comment
https://forums.phpfreaks.com/topic/138309-solved-replies/#findComment-723192
Share on other sites

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 "";

Link to comment
https://forums.phpfreaks.com/topic/138309-solved-replies/#findComment-723200
Share on other sites

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);
?>

Link to comment
https://forums.phpfreaks.com/topic/138309-solved-replies/#findComment-723203
Share on other sites

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.)

Link to comment
https://forums.phpfreaks.com/topic/138309-solved-replies/#findComment-723215
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.