File: /var/www/doco2/wp-content/themes/ormedia/page-alert-list.php
<?php
/* Template Name: Alert List */
require_once('key_alert_functions.php');
// show_php_error();
// 分页
function paging($total_pages, $page, $url, $display = 5, $pg_name = "pg")
{
$first = "";
$pre_icon = "«";
$next_icon = "»";
$prev = ($page > 1) ? "<a class='w3-button icon-container' href='" . $url . $pg_name . "=" . ($page - 1) . "'>" . $pre_icon . "</a>" : "<a class='w3-button icon-container not-reach'>" . $pre_icon . "</a>";
$current = "";
if ($total_pages <= $display) {
for ($i = 1; $i <= $total_pages; $i++) {
if ($i == $page) {
$current .= "<a class='w3-button current-pg'><span>" . $i . "</span></a>";
} else {
$current .= "<a class='w3-button' href='" . $url . $pg_name . "=" . $i . "'><span>$i</span></a>";
}
}
} else {
$half = floor($display / 2);
if ($page < $display) {
for ($i = 1; $i <= $display; $i++) {
if ($i == $page) {
$current .= "<a class='w3-button current-pg'><span>" . $i . "</span></a>";
} else {
$current .= "<a class='w3-button' href='" . $url . $pg_name . "=" . $i . "'><span>$i</span></a>";
}
}
} elseif (($page >= $display) && ($total_pages - $page >= $half - 1)) {
for ($i = $page - $half - 1; $i <= $page + $half - 1; $i++) {
if ($i == $page) {
$current .= "<a class='w3-button current-pg'><span>" . $i . "</span></a>";
} else {
$current .= "<a class='w3-button' href='" . $url . $pg_name . "=" . $i . "'><span>$i</span></a>";
}
}
} elseif (($page > $half + 1) && ($total_pages - $page < $half - 1)) {
for ($i = $total_pages - ($display - 1); $i <= $total_pages; $i++) {
if ($i == $page) {
$current .= "<a class='w3-button current-pg'><span>" . $i . "</span></a>";
} else {
$current .= "<a class='w3-button' href='" . $url . $pg_name . "=" . $i . "'><span>$i</span></a>";
}
}
} else {
for ($i = 1; $i < $display; $i++) {
if ($i == $page) {
$current .= "<a class='w3-button current-pg'><span>" . $i . "</span></a>";
} else {
$current .= "<a class='w3-button' href='" . $url . $pg_name . "=" . $i . "'><span>$i</span></a>";
}
}
}
}
$next = ($page < $total_pages) ? "<a class='w3-button icon-container' href='" . $url . $pg_name . "=" . ($page + 1) . "'>" . $next_icon . "</a>" : "<a class='w3-button icon-container not-reach'>" . $next_icon . "</a>";
$arr1 = array_filter(explode('?', $url));
$arr2 = array_filter(explode('&', $arr1[count($arr1) - 1]));
$last_content = "";
foreach ($arr2 as $a) {
$arr3 = explode('=', $a);
$last_content .= "<input type=' text' hidden name='" . $arr3[0] . "' value='" . $arr3[1] . "'>";
}
$last = "<form>
<span>共 </span>" . $total_pages . "<span> 頁</span><span>,到第 </span><input class='w3-button' type='number' name='" . $pg_name . "'><span> 頁</span>
" . $last_content . "
<input class='w3-button' type='submit' value='確認'>
</form>";
echo "<div class='paging w3-bar'>" . $first . $prev . $current . $next . $last . "</div>";
}
$status = -2;
$cat = 1;
$limit = $_REQUEST['al'];
$page = $_REQUEST['pg'];
$limit = $limit > 0 ? $limit : 20;
$page = $page > 0 ? $page : 1;
$sql = $wpdb->prepare("select count(id) from keylab_alert_msg where cat in (0,%d)", $cat);
$count = $wpdb->get_var($sql);
$total_page = ceil($count / $limit);
if ($page > $total_page) {
$page = $total_page;
}
$offset = ($page - 1) * $limit;
$results = get_alerts_by_cat($status, $cat, $offset, $limit);
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>KeyAlert</title>
<link rel="stylesheet" href="https://www.w3schools.com/w3css/4/w3.css">
<style>
.red {
color: #903939;
}
.bold {
font-weight: bold;
}
.alert-title {
color: #903939;
text-align: center;
}
.alert-page-tips {
padding: 0;
margin: 16px;
}
.alert-content {
max-width: 800px;
margin: 0 auto;
}
.alert-item {
padding: 16px;
display: block;
text-decoration: none;
}
.alert-item-title {
font-size: 120%;
}
.alert-item-time {
color: #666;
margin-top: 10px;
}
.paging {
text-align: center;
padding: 10px 0;
}
.paging .current-pg {
background: #903939;
color: #fff;
}
.paging .not-reach {
opacity: 0.6;
cursor: not-allowed;
}
.paging form {
display: inline;
}
.paging form input[type='number'] {
width: 70px;
border: 1px solid #903939;
cursor: auto;
padding: 8px 4px;
}
.paging form input[type='number']:hover{
background-color: #fff !important;
}
.paging form input[type='submit'] {
background: #903939;
color: #fff;
border: unset;
cursor: pointer;
}
@media(max-width: 601px) {
.paging {
text-align: center !important;
}
.paging form {
margin: 10px 0;
display: block !important;
}
}
</style>
</head>
<body>
<div class="alert-content">
<h2 class="alert-title">KeyAlert</h2>
<?php
echo sprintf('<div class="alert-page-tips">當前第 <span class="red bold">%d</span> 頁,共 %d 頁</div>', $page, $total_page);
foreach ($results as $item) {
echo sprintf('
<a class="alert-item w3-card w3-margin w3-hover-shadow" href="%s">
<div class="alert-item-title">%s</div>
<div class="alert-item-time">%s</div>
</a>
', home_url('/alert-detail/?id=' . $item->id), $item->title, $item->creation_time);
}
paging($total_page, $page, home_url('/alert-list/') . "?list=1&", 5);
?>
</div>
</body>
</html>