랭킹관련 문의드려봅니다.
본문
아래는 포인트 랭킹인데
이것이 지난주와 이번주를 비교하는거같아요
저는 어제와 오늘을 비교해서 랭킹을 보여주고싶어서요
저번주와 금주로 하면 갭차이가 너무 나서요
혹시 어제와 오늘 또는 하루단위로 설정하고싶으면
어떻게 하여야할까요
부탁드려봅니다.
<?php
include_once('./_common.php');
// 이번 주의 시작과 지난 주의 시작 계산
$this_week_start = date('Y-m-d', strtotime('last Monday'));
$last_week_start = date('Y-m-d', strtotime('last Monday -1 week'));
$last_week_end = date('Y-m-d', strtotime('last Sunday'));
// 출력 인원수
$limit = 30;
// 제외할 아이디
$exclude_ids_array = ['admin'];
$exclude_ids = implode("','", $exclude_ids_array);
// 현재 주의 총 포인트를 가져오는 쿼리
$sql_current_week = "
SELECT
m.mb_id,
m.mb_nick,
SUM(IFNULL(p.po_point, 0)) AS total_points
FROM
{$g5['member_table']} m
LEFT JOIN
{$g5['point_table']} p ON m.mb_id = p.mb_id
WHERE
m.mb_id NOT IN ('{$exclude_ids}')
GROUP BY
m.mb_id
ORDER BY
total_points DESC
LIMIT {$limit}
";
$current_week_result = sql_query($sql_current_week);
// 지난 주의 총 포인트를 가져오는 쿼리
$sql_last_week = "
SELECT
m.mb_id,
SUM(IFNULL(p.po_point, 0)) AS total_points
FROM
{$g5['member_table']} m
LEFT JOIN
{$g5['point_table']} p ON m.mb_id = p.mb_id
WHERE
m.mb_id NOT IN ('{$exclude_ids}')
AND p.po_datetime BETWEEN '{$last_week_start}' AND '{$last_week_end}'
GROUP BY
m.mb_id
ORDER BY
total_points DESC
";
$last_week_result = sql_query($sql_last_week);
// 지난 주의 랭킹을 계산
$last_week_ranking = [];
$rank = 1;
while ($row = sql_fetch_array($last_week_result)) {
$last_week_ranking[$row['mb_id']] = $rank++;
}
// 순위 변동 계산 함수
function get_rank_change($mb_id, $current_rank, $last_week_ranking) {
if (isset($last_week_ranking[$mb_id])) {
$last_rank = $last_week_ranking[$mb_id];
$change = $last_rank - $current_rank;
if ($change > 0) {
return "<span style='color:green'>▲ {$change}</span>";
} elseif ($change < 0) {
return "<span style='color:red'>▼ " . abs($change) . "</span>";
} else {
return "-";
}
} else {
return "<span style='color:blue'>New</span>";
}
}
?>
<style>
.ranking-table {
display: flex;
flex-direction: column;
width: 100%;
max-width: 600px;
margin: 0px auto;
box-shadow: 0 0 15px rgba(0, 0, 0, 0.1);
}
.ranking-table ul {
display: flex;
width: 100%;
justify-content: space-between;
padding: 10px;
border-bottom: 1px solid #ddd;
text-align: center;
}
.ranking-table ul:first-child {
background-color: #f8f8f8;
font-weight: bold;
}
.ranking-table ul:nth-child(even) {
background-color: #f2f2f2;
}
.ranking-table ul:hover {
background-color: #e8f4ff;
}
.ranking-table ul li {
flex: 1;
text-align: center;
white-space: nowrap;
overflow: hidden;
text-overflow: ellipsis;
}
.ranking-table ul li:nth-child(1) {
flex: 0 0 15%;
}
.ranking-table ul li:last-child {
flex: 0 0 15%;
}
.ranking-title {
background: #6f809a;
text-align: center;
padding: 8px 5px;
font-size: 0.92em;
margin-top: 20px;
color: #fff;
border: 1px solid #60718b;
}
</style>
<div class="ranking-container">
<h2 class="ranking-title">배달팡팡 포인트랭킹 (매월1일~말일)</h2>
<div class="ranking-table">
<ul>
<li>순위</li>
<li>닉네임</li>
<li>포인트</li>
<li>변동</li>
</ul>
<?php
$rank = 1;
while ($row = sql_fetch_array($current_week_result)) {
$mb_id = $row['mb_id'];
$mb_nick = $row['mb_nick'];
$total_points = number_format($row['total_points']);
$rank_change = get_rank_change($mb_id, $rank, $last_week_ranking);
echo "<ul>";
echo "<li>{$rank}</li>";
echo "<li>{$mb_nick}</li>";
echo "<li>{$total_points}</li>";
echo "<li>{$rank_change}</li>";
echo "</ul>";
$rank++;
}
?>
</div>
</div>
답변 2
안녕하세요...
@개애비 님
제가 만들어 배포한 최근 게시물 스킨이네요....
원하시는데로 코드를 변경 하였습니다.
파일 내 통째로 (어디부터 어디까지가 아니라 그냥 통째로 바꾸세요) 변경하시면 됩니다.
코드 주석은 달려있으나, 날짜 변경은 아래에서 할 수 있습니다.
질문대로 하고 싶으시다면 -1 하시면 됩니다.
절대로 - 마이너스 빼먹지 말아주세요^^
그럼 행복한 하루 보내세요
채택은 잊지 말아주세요^0^
<?php
include_once('./_common.php');
// 날짜 간격 설정 (오늘과 비교할 과거 날짜, -5면 5일전)
$days_ago = -30;
// 오늘과 비교 날짜 계산
$today = date('Y-m-d');
$compare_date = date('Y-m-d', strtotime($days_ago.' days'));
// 출력 인원수
$limit = 10;
// 제외할 아이디
$exclude_ids_array = ['webmaster', 'test3'];
$exclude_ids = implode("','", $exclude_ids_array);
// 오늘의 총 포인트를 가져오는 쿼리
$sql_today = "
SELECT
m.mb_id,
m.mb_nick,
SUM(IFNULL(p.po_point, 0)) AS total_points
FROM
{$g5['member_table']} m
LEFT JOIN
{$g5['point_table']} p ON m.mb_id = p.mb_id
WHERE
m.mb_id NOT IN ('{$exclude_ids}')
AND DATE(p.po_datetime) = '{$today}'
GROUP BY
m.mb_id
ORDER BY
total_points DESC
LIMIT {$limit}
";
$today_result = sql_query($sql_today);
// 비교 날짜의 총 포인트를 가져오는 쿼리
$sql_compare = "
SELECT
m.mb_id,
SUM(IFNULL(p.po_point, 0)) AS total_points
FROM
{$g5['member_table']} m
LEFT JOIN
{$g5['point_table']} p ON m.mb_id = p.mb_id
WHERE
m.mb_id NOT IN ('{$exclude_ids}')
AND DATE(p.po_datetime) = '{$compare_date}'
GROUP BY
m.mb_id
ORDER BY
total_points DESC
";
$compare_result = sql_query($sql_compare);
// 비교 날짜의 랭킹을 계산
$compare_ranking = [];
$rank = 1;
while ($row = sql_fetch_array($compare_result)) {
$compare_ranking[$row['mb_id']] = $rank++;
}
// 순위 변동 계산 함수
function get_rank_change($mb_id, $current_rank, $compare_ranking) {
if (isset($compare_ranking[$mb_id])) {
$compare_rank = $compare_ranking[$mb_id];
$change = $compare_rank - $current_rank;
if ($change > 0) {
return "<span style='color:green'>▲ {$change}</span>";
} elseif ($change < 0) {
return "<span style='color:red'>▼ " . abs($change) . "</span>";
} else {
return "-";
}
} else {
return "<span style='color:blue'>New</span>";
}
}
?>
<style>
.ranking-table {
display: flex;
flex-direction: column;
width: 100%;
max-width: 600px;
margin: 0px auto;
box-shadow: 0 0 15px rgba(0, 0, 0, 0.1);
}
.ranking-table ul {
display: flex;
width: 100%;
justify-content: space-between;
padding: 10px;
border-bottom: 1px solid #ddd;
text-align: center;
}
.ranking-table ul:first-child {
background-color: #f8f8f8;
font-weight: bold;
}
.ranking-table ul:nth-child(even) {
background-color: #f2f2f2;
}
.ranking-table ul:hover {
background-color: #e8f4ff;
}
.ranking-table ul li {
flex: 1;
text-align: center;
white-space: nowrap;
overflow: hidden;
text-overflow: ellipsis;
}
.ranking-table ul li:nth-child(1) {
flex: 0 0 15%;
}
.ranking-table ul li:last-child {
flex: 0 0 15%;
}
.ranking-title {
background: #6f809a;
text-align: center;
padding: 8px 5px;
font-size: 0.92em;
margin-top: 20px;
color: #fff;
border: 1px solid #60718b;
}
</style>
<div class="ranking-container">
<h2 class="ranking-title">포인트 랭킹</h2>
<div class="ranking-table">
<ul>
<li>순위</li>
<li>닉네임</li>
<li>포인트</li>
<li>변동</li>
</ul>
<?php
$rank = 1;
while ($row = sql_fetch_array($today_result)) {
$mb_id = $row['mb_id'];
$mb_nick = $row['mb_nick'];
$total_points = number_format($row['total_points']);
$rank_change = get_rank_change($mb_id, $rank, $compare_ranking);
echo "<ul>";
echo "<li>{$rank}</li>";
echo "<li>{$mb_nick}</li>";
echo "<li>{$total_points}</li>";
echo "<li>{$rank_change}</li>";
echo "</ul>";
$rank++;
}
?>
</div>
</div>
혹시 기존 소스처럼 나오돼 변동만 하루전거랑 비교하려고하는것인데
새로 주신소스를 적용하니 12시가 지나기 회원순위가 싹 사라지고
이상해서 회원에게 포인트를 1점을 지급해보니
그때서야 순위에 나오고 총포인트가 나오는것이 아니고 1포인트가 나오네요
혹시 이부분 체크해주실수 있으신지해서요 ㅎㅎ 지송요
아래이미지는 기존소스입니다.
이거와 모든 기능은 같고 변동부분만 하루전과 비교해서
노출하고 싶어서요