How to recount posts for members in SMF.

Something that members ask from time to time is for a way to recount the total posts for their member. A while back I wrote a small script to do this. To prevent the script from overloading the server it’s done in small steps. It still needs some work but here it is. Hope it comes in handy to some.

Recount User Posts File Download

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
< ?php
// Load SSI.php
require_once('SSI.php');
 
// Turn this on.
error_reporting(E_ALL);
 
// Are we allowed in here?
isAllowedTo('admin_forum');
 
$_REQUEST['start'] = !isset($_REQUEST['start']) ? '0' : (int) $_REQUEST['start'];
 
// Header step and footer.
show_header();
// Check if it exists.
if (!empty($_REQUEST['start']) || (empty($_REQUEST['start']) && isset($_REQUEST['recount'])))
	recount();
elseif (isset($_GET['start']) && $_GET['start'] != -1)
	// Give a link to start
	echo '
		<h2><a href="', $_SERVER['PHP_SELF'], '?recount;start=0">Start Post Recount</a>';
// Footeeerr.
show_footer();
 
 
function recount()
{
	global $db_prefix;
 
	while (true)
	{
		// Only run this query if we don't have the total.
		if (!isset($_SESSION['totalMembers']))
		{
			$request = db_query("
				SELECT COUNT(DISTINCT m.ID_MEMBER)
				FROM ({$db_prefix}messages AS m, {$db_prefix}boards AS b)
				WHERE m.ID_MEMBER != 0
					AND b.countPosts = 0
					AND m.ID_BOARD = b.ID_BOARD", __FILE__, __LINE__);
			list ($_SESSION['totalMembers']) = mysql_fetch_row($request);
			mysql_free_result($request);
			$_SESSION['recountedMembers'] = 0;
		}
 
		// Lets get the members and their post counts.
		// Make sure that we only get boards that have posts count enabled.
		// !!! On a big board 200 might be a bit to high to count.
		$request = db_query("
			SELECT m.ID_MEMBER, COUNT(m.ID_MEMBER) AS posts
			FROM ({$db_prefix}messages AS m, {$db_prefix}boards AS b)
			WHERE m.ID_MEMBER != 0
				AND b.countPosts = 0
				AND m.ID_BOARD = b.ID_BOARD
			GROUP BY m.ID_MEMBER
			LIMIT $_REQUEST[start], 200", __FILE__, __LINE__);
		$total_rows = mysql_num_rows($request);
		$_SESSION['recountedMembers'] += $total_rows * 200;
 
		// Get all the results and assign the correct value.
		while ($row = mysql_fetch_assoc($request))
			db_query("
				UPDATE {$db_prefix}members
				SET posts = $row[posts]
				WHERE ID_MEMBER = $row[ID_MEMBER]", __FILE__, __LINE__);
 
		$_GET['start'] += 200;
		if ($total_rows < 200)
			$_GET['start'] = -1;
		nextStep();
	}
}
 
 
function show_header()
{
	global $steps, $timestart;
 
	if (isset($_SESSION['totalMembers']) && !empty($_GET['start']))
	{
		$timestart = time();
		$total_members = isset($_SESSION['totalMembers']) ? $_SESSION['totalMembers'] : 0;
		$fake_start = $_GET['start'] + 200 >= $total_members ? $total_members : $_GET['start'] + 2;
		$percent = round(100 / $total_members * $fake_start, 2);
	}
 
	echo '< !DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
	<head>
		<title>SMF Member Posts Recount</title>
		<style type="text/css"><!--
			body
			{
				background-color: #E5E5E5;
				margin: 0;
				padding: 0;
				font-size: 10pt;
			}
			#header
			{
				font-weight: bold;
				font-size: 200%;
			}
			#content
			{
				background-color: #CECED7;
				border: 1px solid #8B9AAA;
				padding: 2px;
			}
			#footer
			{
				font-size: 12px;
				text-align: right;
				font-style: italic;
			}
			#container
			{
				width: 50%;
				margin: 0 auto 0 auto;
			}
			h2
			{
				margin: 0;
				margin-top: 0.5ex;
				margin-bottom: 0.5ex;
				padding-bottom: 3px;
				border-bottom: 1px dashed black;
				font-size: 14pt;
				font-weight: normal;
			}
			h3
			{
				margin: 0;
				margin-bottom: 2ex;
				font-size: 10pt;
				font-weight: normal;
			}
			input
			{
				margin: 2px;
			}
			.status_bar_border
			{
				height: 20px;
				width: 450px;
				background: #ffffff;
				border: 1px solid silver;
				margin: 0 auto 0 auto;
				padding-right: 4px;
				text-align: right;
			}
			.status_bar
			{
				height: 16px;
				margin: 2px;
				padding: 0px;
				text-align: center;
				text-align: right;
			}
			.bg1
			{
				background: #3dc0df;
			}
			.bg2
			{
				background: #C9DDEC;
			}
			.percent
			{
				text-align: right;
				color: #000;
				font-weight: bold;
				padding-bottom: 5px;
			}
			.codebox
			{
				background-color: #cccccc;
				font: normal 10px "Courier New", monospace;
				border: solid 1px black;
				padding: 5px;
				white-space: nowrap;
				overflow: auto;
				width: 95%;
			}
		--></style>
	</head>
	<body>
		<div id="container">
			<div id="header">
				SMF Member Posts Recount
			</div>
			<div id="content">';
 
	if (!empty($total_members))
		echo '
				<h2>Recount Status</h2>
				<h3 style="margin: 0 0 0 40px; font-weight: bold;">Overall Process</h3>
				<div class="status_bar_border">
					<div class="status_bar bg1" style="width: ', $percent, '%; white-space: nowrap;">', $percent, '% (', $fake_start, ' out of ', $total_members, ' members) </div>
				</div>';
 
	if (isset($percent) && $percent == 100 || isset($_GET['start']) && $_GET['start'] == -1)
		echo '
				<h1>Recount Complete</h1>';
 
}
 
function show_footer()
{
	echo '
			</div>
			<div id="footer">
				<a href="http://jaybachatero.com">JayBachatero.com</a>
			</div>
		</div>
	</body>
</html>';
}
 
function nextStep($step = null)
{
	global $timestart;
 
	@set_time_limit(300);
	if (function_exists('apache_reset_timeout'))
		apache_reset_timeout();
 
	echo '
				<h2 style="margin-top: 2ex;">Recount Paused!</h2>
				<h3>This recount has paused to avoid server overload.</h3>
 
				<form action="', $_SERVER['PHP_SELF'], '?start=', $_GET['start'], '" method="post" name="auto_submit">
					<div align="right" style="margin: 1ex;"><input name="timer" type="submit" value="Continue" /></div>
				</form>
				<script language="JavaScript" type="text/javascript"><!-- // -->< ![CDATA[
					window.onload = autoSubmit;
					var countdown = 3;
 
					function autoSubmit()
					{
						if (countdown == 0)
							document.auto_submit.submit();
						else if (countdown == -1)
							return;
 
						document.auto_submit.timer.value = "Continue (" + countdown + ")";
						countdown--;
 
						setTimeout("autoSubmit();", 1000);
					}
				// ]]></script>';
 
	show_footer();
	exit;
}
? >

7 thoughts on “How to recount posts for members in SMF.”

  1. Hi Juan!

    Good work on this topic, as I was previously often asked by local community users how to do this and given them rough SQL query that had to be manually edited for every particular case. This will help a lot.

    Regards,

    Bernard

  2. hye mate, that isn’t working for me, when i copy file to my board dir and open it, i see only title, and footer… i’m using 1.1.4, is there anything i can do?

  3. hi,

    How to use it under SMF 1.1.2 ?

    i have always the page : SMF Member Posts Recount with no link to click in order to recount posts by members….

    Many thanks

  4. Hi, I would like to ask if this works with 1.1.11? I get a parse error when I tried it.

    Parse error: syntax error, unexpected T_REQUIRE_ONCE in .../recount_user_posts.php on line 3

Leave a Reply

Your email address will not be published. Required fields are marked *


*