Jump to content

Recommended Posts

Here is a snipet from a script I have.

 

	elseif($action == "reply")
	{
	$topicid=$_POST['topicid'];
	$sql8="SELECT MAX(answerid) AS Maxanswerid FROM $tablereplies";
	$result8=mysql_query($sql8);
	$rows8=mysql_fetch_array($result8);

	if ($rows8) 
		{
		$Maxid = $rows8['Maxanswerid']+1;
		}
	else 
		{
		$Maxid = 1;
		}
	$myusername=$_SESSION['myusername'];
	$mydisplayname=$displayname;
	$content=$_POST['content'];
	$author=$myusername;
	$authordisplay=$displayname;
	$datetime=date("m/d/y H:i:s"); // create date and time 

	$sql9="INSERT INTO $tablereplies(questionid, answerid, author, authordisplay, content, datetime)VALUES('$topicid', '$Maxid',  '$author', '$authordisplay', '$content', '$datetime')";
        $result9=mysql_query($sql9);

	if($result9)
		{
		$topicid2=$_POST['topicid'];
		$sql14="SELECT MAX(answerid) AS Maxreplies FROM $tablereplies WHERE questionid='$topicid2'";
		$result14=mysql_query($sql14);
		$rows14=mysql_fetch_array($result14);

		echo "<br><br>Successful<BR>";
		echo "<a href='index.php?viewtopic=".$topicid."'>View your answer</a>";

		if ($rows8) 
			{
			$Maxreplies = $rows14['Maxreplies']+1;
			}
		else 
			{
			$Maxreplies = 1;
			}

		$sql10="UPDATE $tableposts SET posts='$Maxreplies' WHERE id='$topicid'";
		$result10=mysql_query($sql10);

		}
	else 
		{
		echo "ERROR";
		}

	}

 

This is called when a user submits an answer to a topic. What I want it to do is update the number of replies (posts) in a topic to the MAX(answerid) for a topic (topicid) and then add 1 to that. What it is doing is adding 1 to the MAX(answerid) for ALL topics. SO if a topic's, say id 1, MAX(answerid) is 2 and topic 2's MAX(answerid) is 7, when someone adds a reply to topic 1, the number of replies (posts) is updated to 8, not 3.

 

I hope this makes sense. Any help is appreciated.

 

~Shaun

Link to comment
https://forums.phpfreaks.com/topic/51240-little-help/
Share on other sites

if I understand you correctly you're wanting seperate post counts for each topic, and an overall post count for all topics?

so you can maybe list the post count for each topic individualy, but also display an overal count for the whole forum, or whatever it is you're making..

 

In which case I'd probably have a total posts variable. let's say $totposts, then check the database for all the posts and adding them together as $totposts.

<?php
$totposts = 0;
$sql = "SELECT * FROM $tablereplies";
if ($result = mysql_query($sql)) {
if (mysql_num_rows($result)) {
    while ($row = mysql_fetch_assoc($result)) {
	$totpost++
    }
}else{
    echo "no posts";
}
}else{
echo mysql_error();
}
?>

that basically increments $totposts for each post in the database.

 

I hope that's along the lines of what you wanted...

Link to comment
https://forums.phpfreaks.com/topic/51240-little-help/#findComment-252420
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.