1. Go to this page and download the library: Download wangjian/ranking library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
wangjian / ranking example snippets
//定义provider
class AllProvider extends \Wangjian\Ranking\Provider\AllProvider
{
public function getAll()
{
$items = [];
for($i = 0; $i < 10; $i++) {
$items[] = [
'score' => rand(0, 100),
'member' => $this->getRandomMember()
];
}
return $items;
}
protected function getRandomMember($length = 6)
{
$tokens = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
$tokenCount = strlen($tokens);
$str = '';
for($i = 0; $i < $length; $i++) {
$str .= $tokens[rand(0, $tokenCount-1)];
}
return $str;
}
}
//从数据库中获取数据
class DatabaseAllProvider extends \Wangjian\Ranking\Provider\AllProvider
{
public function getAll()
{
//链接数据库
$pdo = new PDO('', '', '');
$select = 'SELECT score, member from tb';
$sth = $pdo->query($select);
return $sth->fetchAll(PDO::FETCH_ASSOC);
}
}
//定义PageProvider
class DatabasePageProvider extends \Wangjian\Ranking\Provider\PageProvider
{
public function getPage($page)
{
//定义每页加载条数
$perPage = 100;
//链接数据库
$pdo = new PDO('', '', '');
$select = 'SELECT score, member from tb LIMIT ' . ($page - 1) * $perPage . ', ' . $perPage;
$sth = $pdo->query($select);
return $sth->fetchAll(PDO::FETCH_ASSOC);
}
}
//定义GeneratorProvider
class DatabaseGeneratorProvider extends \Wangjian\Ranking\Provider\GeneratorProvider
{
public function generate()
{
//链接数据库
$pdo = new PDO('', '', '');
$select = 'SELECT score, member from tb LIMIT ' . ($page - 1) * $perPage . ', ' . $perPage;
$sth = $pdo->query($select)->setFetchMode(PDO::FETCH_ASSOC);
foreach($sth as $row) {
yield $row;
}
}
}
//链接redis
$client = new \Predis\Client([
'scheme' => 'tcp',
'host' => '127.0.0.1',
'port' => 6379,
'password' => 'root'
]);
//排行榜名称
$prefix = 'score';
//总排行榜
$totalRanking1 = new \Wangjian\Ranking\Ranking\TotalRanking($client, new DatabaseAllProvider(), $prefix);
$totalRanking2 = new \Wangjian\Ranking\Ranking\TotalRanking($client, new DatabasePageProvider(), $prefix);
$totalRanking3 = new \Wangjian\Ranking\Ranking\TotalRanking($client, new DatabaseGeneratorProvider(), $prefix);
//日排行榜
$dailyRanking = new \Wangjian\Ranking\Ranking\DailyRanking($client, new DatabaseAllProvider(), $prefix);
//周排行榜
$weeklyRanking = new \Wangjian\Ranking\Ranking\WeeklyRanking($client, new DatabaseAllProvider(), $prefix);
//月排行榜
$monthlyRanking = new \Wangjian\Ranking\Ranking\MonthlyRanking($client, new DatabaseAllProvider(), $prefix);
//历史日排行榜
$previousDailyRanking = new \Wangjian\Ranking\Ranking\PreviousDailyRanking($client, new DatabaseAllProvider(), $prefix);
//历史周排行榜
$previousWeeklyRanking = new \Wangjian\Ranking\Ranking\PreviousWeeklyRanking($client, new DatabaseAllProvider(), $prefix);
//历史月排行榜
$previousMonthlyRanking = new \Wangjian\Ranking\Ranking\PreviousMonthlyRanking($client, new DatabaseAllProvider(), $prefix);