Jump to content

Trying to get a count of initial instances...


Jim R

Recommended Posts

I'm trying to get a count of how many topics someone starts in a forum.  I use XenForo, and they only count Posts, but I can order Posts by post_id and group those posts by thread_id to determine the initial instance of that topic.  The problem I'm having is output.

I'm getting the right User info, but the output is offset by one row:  

User 1 | 0 topics (because $c=0)

User 2 | 128 topics (but those are User 1's #)

User 3 | 0 topics (but those are User 2's #)

User 4 | 141 topics (but those are User 3's #)

etc

 

I realize as I play through the logic of the code, it's printing in that order.  I can't seem to get the right order.  I've tried various ways, with a couple of extra IF statements in there too.  

 

$query = "select thread_id,user_id,username from 
(
select * from xf_post 
order by post_id
) x
group by thread_id
order by username,thread_id";

$results = mysqli_query($con,$query);
		echo mysqli_error($con);
		
		$c=0;
		$currentID = false; 

			while($line = mysqli_fetch_assoc($results)) {
				
				
				if ($currentID != $line['user_id']) {
				
							echo '<div>' . $line['username'] . ', ' . $c;

							$currentID = $line['user_id'];
							$c=0;
				}	
				else {
					$c++;
				}
					
			}

 

Link to comment
Share on other sites

Never mind...figured that part out....

Print the first half of what I need (the User Name) in the main IF loop, then add the count after there is a change but before the User name and count are reset.

 


$query = "select thread_id,user_id,username from 
(
select * from xf_post 
order by post_id
) x
group by thread_id
order by username,thread_id";

$results = mysqli_query($con,$query);
		echo mysqli_error($con);
		
		$currentID= false; 

			while($line = mysqli_fetch_assoc($results)) {
				
				
				if ($currentID != $line['user_id']) {
				
					if ($currentID != false) {
							echo $c . '</div>';
							$c=0;
					}

					$currentID = $line['user_id'];
					echo '<div>' . $line['username'] . ', ';
					$c++;


				}	
				else {
					$c++;
				}
			
				
			}

 

Edited by Jim R
Link to comment
Share on other sites

Create a view for yourself that shows threads and the initial posts. It'll make life easier. Though I'm really skeptical that XenForo doesn't have a way to get that information sort of finding the first post for a given thread ID - after all, since there is an ID in the first place, surely there is some source generating that ID, right?

Once you have the view the query to find users is trivial.

  • Like 1
Link to comment
Share on other sites

Try

SELECT username
     , COUNT(*) as topics_started
FROM (
        SELECT user_id, username                           -- find username in record matching first post in each thread
        FROM xf_post
             JOIN (
                    SELECT thread_id                       -- subquery to find time of first post in each thread
                         , MIN(timestamp) as timestamp
                    FROM xf_post
                    GROUP BY thread_id
                  ) firstpost USING (thread_id, timestamp)
     ) firstuser
GROUP BY user_id
ORDER BY topics_started DESC;

 

Link to comment
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.