Support for Drupal 7 is ending on 5 January 2025—it’s time to migrate to Drupal 10! Learn about the many benefits of Drupal 10 and find migration tools in our resource center.
PHP page snipplet to display monthly information on number of
- nodes and comments
- privatemessages and guestbook messages
- number of new users
Userful in following up the growth of your successful Drupal site!
Tested on 4.7.x
Requires privatemsg and guestbook modules. In case you do not have them please erase the middle section.
Works, but but a loop structure would make this more beautiful & efficient.
<?php
// no unauthorized access. Only admin can see the statistics.
global $user;
if (!$user->uid or $user->uid!=1 )
{
print '<p>';
print 'No access';
}
else
{
// Nodes and comments in each month
$query="SELECT Year, Month, sum(pcs) from ((SELECT FROM_UNIXTIME(node.created,'%Y') as Year, FROM_UNIXTIME(node.created,'%m') as Month, count(node.created) as pcs FROM node group by 1,2) union (select FROM_UNIXTIME(comments.timestamp,'%Y'),FROM_UNIXTIME(comments.timestamp,'%m'), count(comments.timestamp) FROM comments group by 1,2)) as sii group by 1,2 order by 1 asc, 2 asc";
print '<h2> Nodes and comments </h2>';
$result=mysql_query($query);
print '<table><tr><td>';
print 'Month';
print '</td><td>';
print 'Year';
print '</td><td>';
print 'Pcs';
print '</td></tr>';
while($row = mysql_fetch_array($result)){
print '<tr><td>';
print $row[1];
print '</td><td>';
print $row[0];
print '</td><td>';
print $row[2];
print '</td></tr>';}
print '</table>';
// Guestbook module messages and privatemsg module messages in each month
$query="SELECT Year, Month, sum(pcs) from ((SELECT FROM_UNIXTIME(privatemsg.timestamp,'%Y') as Year, FROM_UNIXTIME(privatemsg.timestamp,'%m') as Month, count(privatemsg.timestamp) as pcs FROM privatemsg group by 1,2) union (select FROM_UNIXTIME(guestbook.created,'%Y'),FROM_UNIXTIME(guestbook.created,'%m'), count(guestbook.created) FROM guestbook group by 1,2)) as sii group by 1,2 order by 1 asc, 2 asc";
print '<h2> Guestbook and private messages </h2>';
$result=mysql_query($query);
print '<table><tr><td>';
print 'Month';
print '</td><td>';
print 'Year';
print '</td><td>';
print 'Pcs';
print '</td></tr>';
while($row = mysql_fetch_array($result)){
print '<tr><td>';
print $row[1];
print '</td><td>';
print $row[0];
print '</td><td>';
print $row[2];
print '</td></tr>';}
print '</table>';
// Users stats. Users that have been logged in at least once in each month
$query="SELECT FROM_UNIXTIME(users.created,'%Y') as Year, FROM_UNIXTIME(users.created,'%m') as Month, count(users.created) as pcs FROM users WHERE users.access >0 group by 1,2 order by 1 asc, 2 asc";
print '<h2> Users </h2>';
$result=mysql_query($query);
print '<table><tr><td>';
print 'Month';
print '</td><td>';
print 'Year';
print '</td><td>';
print 'Pcs';
print '</td></tr>';
while($row = mysql_fetch_array($result)){
print '<tr><td>';
print $row[1];
print '</td><td>';
print $row[0];
print '</td><td>';
print $row[2];
print '</td></tr>';}
print '</table>';
}
?>