像这样使用开关作为动态和分页有效吗?
is it effective using switch as dynamic and pagination like this?
我用switch创建动态和分页,这样的代码有效吗?
if(isset($_GET['pages'])){
$pages = $_GET['pages'];
switch ($pages) {
case 'home':
$totalData = count(query("SELECT * FROM tb_items"));
$totalPage = ceil($totalData / $totalDataPerPage);
$activePage = $_GET['page'];
$earlyData = ( $totalDataPerPage * $activePage ) - $totalDataPerPage;
$item = query("SELECT * FROM tb_items ORDER BY listing_time DESC LIMIT $earlyData, $totalDataPerPage");
$cat = "home";
include "ez/_ez.php";
break;
case 'items':
$totalData = count(query("SELECT * FROM tb_items WHERE cat_id = 1"));
$totalPage = ceil($totalData / $totalDataPerPage);
$activePage = $_GET['page'];
$earlyData = ( $totalDataPerPage * $activePage ) - $totalDataPerPage;
$item = query("SELECT * FROM tb_items WHERE cat_id = 1 ORDER BY id DESC LIMIT $earlyData, $totalDataPerPage");
$cat = "items";
include "ez/_ez.php";
break;
case 'worlds':
$totalData = count(query("SELECT * FROM tb_items WHERE cat_id = 2"));
$totalPage = ceil($totalData / $totalDataPerPage);
$activePage = $_GET['page'];
$earlyData = ( $totalDataPerPage * $activePage ) - $totalDataPerPage;
$cat = "worlds";
$item = query("SELECT * FROM tb_items WHERE cat_id = 2 ORDER BY id DESC LIMIT $earlyData, $totalDataPerPage");
include "ez/_ez.php";
break;
default:
echo "<center><h1>Maaf. Halaman tidak di temukan !</h1></center>";
break;
}
}else{
include "ez/_ez.php";
}
有没有办法让它更有效?
帮我看看有没有办法让它看起来更好
您可以将页面类别存储在一个数组中,而不是重复您的代码:
$category_ids = ['home' => null, 'items' => 1, 'worlds' => 2];
if (isset($_GET['pages'])) {
$cat = $_GET['pages'];
if (array_key_exists($cat, $category_ids)) {
$category_id = $category_ids[$cat];
$where = !is_null($category_id) ? 'WHERE cat_id = ' . $category_id : '';
$totalData = count(query("SELECT * FROM tb_items $where"));
$totalPage = ceil($totalData / $totalDataPerPage);
$activePage = $_GET['page'];
$earlyData = ($totalDataPerPage * $activePage) - $totalDataPerPage;
$item = query("SELECT * FROM tb_items $where ORDER BY listing_time DESC LIMIT $earlyData, $totalDataPerPage");
include "ez/_ez.php";
} else {
echo "<center><h1>Maaf. Halaman tidak di temukan !</h1></center>";
}
} else {
include "ez/_ez.php";
}
我用switch创建动态和分页,这样的代码有效吗?
if(isset($_GET['pages'])){
$pages = $_GET['pages'];
switch ($pages) {
case 'home':
$totalData = count(query("SELECT * FROM tb_items"));
$totalPage = ceil($totalData / $totalDataPerPage);
$activePage = $_GET['page'];
$earlyData = ( $totalDataPerPage * $activePage ) - $totalDataPerPage;
$item = query("SELECT * FROM tb_items ORDER BY listing_time DESC LIMIT $earlyData, $totalDataPerPage");
$cat = "home";
include "ez/_ez.php";
break;
case 'items':
$totalData = count(query("SELECT * FROM tb_items WHERE cat_id = 1"));
$totalPage = ceil($totalData / $totalDataPerPage);
$activePage = $_GET['page'];
$earlyData = ( $totalDataPerPage * $activePage ) - $totalDataPerPage;
$item = query("SELECT * FROM tb_items WHERE cat_id = 1 ORDER BY id DESC LIMIT $earlyData, $totalDataPerPage");
$cat = "items";
include "ez/_ez.php";
break;
case 'worlds':
$totalData = count(query("SELECT * FROM tb_items WHERE cat_id = 2"));
$totalPage = ceil($totalData / $totalDataPerPage);
$activePage = $_GET['page'];
$earlyData = ( $totalDataPerPage * $activePage ) - $totalDataPerPage;
$cat = "worlds";
$item = query("SELECT * FROM tb_items WHERE cat_id = 2 ORDER BY id DESC LIMIT $earlyData, $totalDataPerPage");
include "ez/_ez.php";
break;
default:
echo "<center><h1>Maaf. Halaman tidak di temukan !</h1></center>";
break;
}
}else{
include "ez/_ez.php";
}
有没有办法让它更有效? 帮我看看有没有办法让它看起来更好
您可以将页面类别存储在一个数组中,而不是重复您的代码:
$category_ids = ['home' => null, 'items' => 1, 'worlds' => 2];
if (isset($_GET['pages'])) {
$cat = $_GET['pages'];
if (array_key_exists($cat, $category_ids)) {
$category_id = $category_ids[$cat];
$where = !is_null($category_id) ? 'WHERE cat_id = ' . $category_id : '';
$totalData = count(query("SELECT * FROM tb_items $where"));
$totalPage = ceil($totalData / $totalDataPerPage);
$activePage = $_GET['page'];
$earlyData = ($totalDataPerPage * $activePage) - $totalDataPerPage;
$item = query("SELECT * FROM tb_items $where ORDER BY listing_time DESC LIMIT $earlyData, $totalDataPerPage");
include "ez/_ez.php";
} else {
echo "<center><h1>Maaf. Halaman tidak di temukan !</h1></center>";
}
} else {
include "ez/_ez.php";
}