Top poster last month script

Discussion in 'PHP' started by fadetoblack22, Oct 6, 2011.

  1. #1
    This script shows the top poster this month for my forum. I basically want to change it to last month. I think it's quite simple to do, but I don't know php so I can't do it.

    /* The Top 10 Posters and Topic Starters Stats mod starts here. */
    	
    	// This is for the top 10 today. It gets the current date in one array.
    	$date = @getdate(forum_time(false));
    	
    	// This is needed for the top 10 this month. It will determine if we're on the first day of the month or not.
    	$month_day = date('j') == 1 ? date('j') : date('j') - 1;
    	
    	// This is needed for the top 10 this year. It gets the current day of the year.
    	$year_days = date('z');
    	
    	// Set the time on where to start.
    	$start_time = array(
    		'today' => mktime(0, 0, 0, $date['mon'], $date['mday'], $date['year']) - ($modSettings['time_offset'] * 3600),
    		'week' => mktime(0, 0, 0, date('n'), date('j'), date('Y')) - (3600*24*date('w')),
    		'month' => mktime(0, 0, 0, date('n'), date('j'), date('Y')) - (3600*24*$month_day),
    		'year' => mktime(0, 0, 0, date('n'), date('j'), date('Y')) - (3600*24*$year_days),
    	);
    	
    	// Format the $start_time for use in the query, and use the forum time offset.
    	$start_time = array(
    		'today' => forum_time(false, $start_time['today']),
    		'week' => forum_time(false, $start_time['week']),
    		'month' => forum_time(false, $start_time['month']),
    		'year' => forum_time(false, $start_time['year']),
    	);
    	
    	// The way we are going to sort this when we display it on the template.
    	$context['top10_sort'] = array('today', 'week', 'month', 'year');
    	
    	// This is the slowest way possible to do this, but it's SMF 1.1.x. It's WAY FASTER in SMF 2.0..... 0.o
    	
    	// Let's try to find away to prevent ourselves from creating duplicate code.
    	foreach ($start_time as $time => $format)
    	{
    		// The query to execute, for top 10 posters.
    		$top10_posters = db_query("
    			SELECT mem.ID_MEMBER, mem.realName, COUNT(*) as count_posts
    			FROM {$db_prefix}messages AS m
    				LEFT JOIN {$db_prefix}members AS mem ON (mem.ID_MEMBER = m.ID_MEMBER)
    			WHERE m.posterTime > " . $start_time[$time] . "
    				AND m.ID_MEMBER != 0
    			GROUP BY mem.ID_MEMBER
    			ORDER BY count_posts DESC
    			LIMIT 10", __FILE__, __LINE__);
    		
    		// Try to cache this when possible, because it's a little unavoidably slow.
    		if (($members = cache_get_data('stats_top_starters_mod', 360)) == null)
    		{
    			$request = db_query("
    				SELECT t.ID_MEMBER_STARTED, t.ID_FIRST_MSG, COUNT(*) AS hits
    				FROM {$db_prefix}topics AS t
    					LEFT JOIN {$db_prefix}messages AS m ON (t.ID_FIRST_MSG = m.ID_MSG)" . (!empty($modSettings['recycle_enable']) && $modSettings['recycle_board'] > 0 ? "
    				WHERE t.ID_BOARD != $modSettings[recycle_board]
    					AND m.posterTime > " . $start_time[$time] : "
    				WHERE m.posterTime > " . $start_time[$time]) . "
    				GROUP BY t.ID_MEMBER_STARTED
    				ORDER BY hits DESC
    				LIMIT 20", __FILE__, __LINE__);
    			$members = array();
    			while ($row = mysql_fetch_assoc($request))
    				$members[$row['ID_MEMBER_STARTED']] = $row['hits'];
    			mysql_free_result($request);
    
    			cache_put_data('stats_top_starters_mod', $members, 360);
    		}
    
    		if (empty($members))
    			$members = array(0 => 0);
    		
    		// And the query for top 10 topic starters.
    		$top10_starters = db_query("
    			SELECT ID_MEMBER, realName
    			FROM {$db_prefix}members
    			WHERE ID_MEMBER IN (" . implode(', ', array_keys($members)) . ")
    			ORDER BY FIND_IN_SET(ID_MEMBER, '" . implode(',', array_keys($members)) . "')
    			LIMIT 10", __FILE__, __LINE__);
    	
    		// Setup the $txt vars.
    		$top_posters = 'top10_posters_' . $time;
    		$top_starters = 'top10_topic_starters_' . $time;
    		$context['top10_txt']['posters'][$time] = $txt[$top_posters];
    		$context['top10_txt']['topic_starters'][$time] = $txt[$top_starters];
    		
    		// Everything is going into this array.
    		$context[$top_posters] = array();
    		$context[$top_starters] = array();
    		
    		// The max number of posts/topics.
    		$max_num = 1;
    			
    		// Return the top 10 posters data!
    		while ($row_members = mysql_fetch_assoc($top10_posters))
    		{			
    			// The number of posts.
    			$count_posts = $row_members['count_posts'];
    			
    			// Here comes the data!
    			$context[$top_posters][$row_members['ID_MEMBER']] = array(
    				'name' => $row_members['realName'],
    				'id' => $row_members['ID_MEMBER'],
    				'num_amount' => $count_posts,
    				'href' => $scripturl . '?action=profile;u=' . $row_members['ID_MEMBER'],
    				'link' => '<a href="' . $scripturl . '?action=profile;u=' . $row_members['ID_MEMBER'] . '">' . $row_members['realName'] . '</a>',
    			);
    			
    			// Set the max num of posts/topics.
    			if ($max_num < $count_posts)
    				$max_num = $count_posts;
    		}
    		mysql_free_result($top10_posters);
    		
    		// Calculate the percentages.
    		foreach ($context[$top_posters] as $i => $j)
    			$context[$top_posters][$i]['post_percent'] = round(($j['num_amount'] * 100) / $max_num);
    
    		// Tidy up everything...
    		unset($max_num, $row_members, $j, $i);
    		
    		// The max number of posts/topics.
    		$max_num = 1;
    		
    		while ($row_members = mysql_fetch_assoc($top10_starters))
    		{		
    			// The number of posts.
    			$count_posts = $members[$row_members['ID_MEMBER']];
    			
    			// Here comes the data!
    			$context[$top_starters][$row_members['ID_MEMBER']] = array(
    				'name' => $row_members['realName'],
    				'id' => $row_members['ID_MEMBER'],
    				'num_amount' => $count_posts,
    				'href' => $scripturl . '?action=profile;u=' . $row_members['ID_MEMBER'],
    				'link' => '<a href="' . $scripturl . '?action=profile;u=' . $row_members['ID_MEMBER'] . '">' . $row_members['realName'] . '</a>',
    			);
    			
    			// Set the max num of posts/topics.
    			if ($max_num < $count_posts)
    				$max_num = $count_posts;
    		}
    		mysql_free_result($top10_starters);
    		
    		// Calculate the percentages.
    		foreach ($context[$top_starters] as $i => $j)
    			$context[$top_starters][$i]['post_percent'] = round(($j['num_amount'] * 100) / $max_num);
    
    		// Tidy up everything...
    		unset($max_num, $row_members, $j, $i);
    	}
    	/* The Top 10 Posters and Topic Starters Stats mod ends here. */
    PHP:

    Any help would be appreciated :)
     
    fadetoblack22, Oct 6, 2011 IP
  2. fri3ndly

    fri3ndly Active Member

    Messages:
    111
    Likes Received:
    5
    Best Answers:
    0
    Trophy Points:
    60
    #2
    I think you should be able to just replace this line:

    
    
    // This is needed for the top 10 this month. It will determine if we're on the first day of the month or not.
    $month_day = date('j') == 1 ? date('j') : date('j') - 1;
    
    
    Code (markup):
    With this:

    
    
    // This is needed for the top 10 this month. It will determine if we're on the first day of the month or not.
    $month_day = date('j') == 1 ? date('j', strtotime('- 1 month') ) : date('j', strtotime('- 1 month') ) - 1;
    
    
    Code (markup):
    Not tested but give it a try
     
    fri3ndly, Oct 6, 2011 IP
  3. fadetoblack22

    fadetoblack22 Well-Known Member

    Messages:
    2,399
    Likes Received:
    62
    Best Answers:
    0
    Trophy Points:
    160
    #3
    Thanks for the reply. I just tested it and it shows the same post count, so I am guessing that it doesn't work :(
     
    fadetoblack22, Oct 6, 2011 IP
  4. fadetoblack22

    fadetoblack22 Well-Known Member

    Messages:
    2,399
    Likes Received:
    62
    Best Answers:
    0
    Trophy Points:
    160
    #4
    Is anyone else able to help?
     
    fadetoblack22, Oct 13, 2011 IP