[Release] Dynamic Top Stats

JayC

Always Learning
Aug 8, 2013
5,493
1,398
Hey Devbest,

I am here to release a nice top stat page its going to be dynamic and simple :) Doesn't take up much space at all. Everything is manageable via the database.

First Add the table to the database:
Code:
DROP TABLE IF EXISTS `cms_topstats`;
CREATE TABLE `cms_topstats` (
  `stat` varchar(120) NOT NULL,
  `limit` int(11) NOT NULL,
  `table` varchar(80) NOT NULL,
  `display` varchar(120) NOT NULL,
  `order` varchar(5) NOT NULL DEFAULT '' COMMENT 'ASC or DESC',
  `after` varchar(50) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

Next put the code in the stats page:

Code:
<?php
                        function GetTimeDiff($time)
                        {
                        $d = floor($time/86400);
                        $_d = ($d < 10 ? '' : '').$d;
                        $h = floor(($time-$d*86400)/3600);
                        $_h = ($h < 10 ? '' : '').$h;
                        $time_str = $_d . ' days ' . $_h . ' hours';
                        return $time_str;
                        }
                        $getTopStats = mysql_query("SELECT * FROM `cms_TopStats`");
                        while($currentStat = mysql_fetch_assoc($getTopStats)){
                        ?>
                    <div class="c-box" style="width:220px;display:inline-block;">
                    <div class="c-title red" style="width:220px;display:inline-block;"><?php echo $currentStat['display']; ?></div>
                    <div class="c-main">
                        <?php
                          if($currentStat['table'] == "users"){
                          $getUserStats = mysql_query("SELECT * FROM ".$currentStat['table']." WHERE rank < 3 ORDER BY ".$currentStat['stat']." ".$currentStat['order']." LIMIT ".$currentStat['limit']."");
                            }else{
                          $getUserStats = mysql_query("SELECT * FROM ".$currentStat['table']." ORDER BY ".$currentStat['stat']." ".$currentStat['order']." LIMIT ".$currentStat['limit']."");
                            }
                          while($currentWinner = mysql_fetch_assoc($getUserStats)){
                          $userStats = mysql_fetch_assoc(mysql_query("SELECT `look`,`username` FROM users WHERE id = '".$currentWinner['id']."'"));
                          ?>
                    <img style="float:left;display:inline-block;" src="http://www.habbo.com/habbo-imaging/avatarimage?figure=<?php echo $userStats['look']; ?>&direction=2&head_direction=3&size=s">
                    <br>
                    <p style="overflow:none;"><span style="color:blue;"><?php echo $userStats['username'];?> ~ </span><span style="color:red;"><?php $value = $currentStat['stat'];if($currentStat['stat'] != "OnlineTime"){echo $currentWinner[$value];}else{echo GetTimeDiff($currentWinner[$value]);}echo $currentStat['after'];?></span></p>
                    <br>
                        <?php
                        }
                        ?>
                    </div>
                    </div>
                        <?php
                        }
                        ?>

How it works:
Just add the stat into the database as follows;
Stat Name = The name of the column in the database (Credits/Activity_points/RoomVisits)
Limit = The number of records you would like to display (5/8/10)
Table = The name of the table the column is in (Users/user_stats)
Display = Text on the div box (Credits, Duckets, Online Time, Room Visits)
Order = ASC/DESC (Ascending or Descending)
After = Text after the output example: 500c 8 gifts

Defaults I made for you:
Code:
INSERT INTO `cms_topstats` VALUES ('credits', '5', 'users', 'Credits', 'DESC', 'c');
INSERT INTO `cms_topstats` VALUES ('vip_points', '5', 'users', 'Diamonds', 'DESC', ' Diamonds');
INSERT INTO `cms_topstats` VALUES ('activity_points', '5', 'users', 'Duckets', 'DESC', ' Duckets');
INSERT INTO `cms_topstats` VALUES ('OnlineTime', '5', 'user_stats', 'Online Time', 'DESC', '');
INSERT INTO `cms_topstats` VALUES ('RoomVisits', '5', 'user_stats', 'Room Visits', 'DESC', ' Visits');
INSERT INTO `cms_topstats` VALUES ('Respect', '5', 'user_stats', 'Respects Recieved', 'DESC', ' Respects');
INSERT INTO `cms_topstats` VALUES ('RespectGiven', '5', 'user_stats', 'Respects Given', 'DESC', ' Respects');
INSERT INTO `cms_topstats` VALUES ('GiftsGiven', '5', 'user_stats', 'Gifts Given', 'DESC', ' Gifts');

You can just add more if you would like, or change the ones already there.

TIP: Change the <div class="c-box"> and other div's to match your revcms edit, otherwise it will not display correctly.

Notice: This is a basic top stats, it just shows a small image of them with their username and then the stat. There is nothing special on the front end :) Feel free to edit that, and maybe re-release in the comments a nice front end and I will put it in the thread !

Thanks,
- JayCustom
 

Users who are viewing this thread

Top