我在一个网站上工作,在那里你可以读到不同的歌曲,听它们,给它们打分等等,和他们的歌手一样。
我一直在考虑如何使用PHP创建一个简单但工作完美的分页脚本,昨天晚上,我想出了下面的代码。
首先,这是下面代码的最终结果:
Desktop视图

Mobile视图

返回注释数量的function:
function countComments($ID,$table){
$st=$this->conn->prepare("SELECT COUNT(*) FROM ".$table."_comments WHERE ".$table."ID=?");
$st->execute([$ID]);
return $st->fetchall();
}决定从哪个表中选择注释,根据哪个表设置ID、歌曲或歌手:
if(isset($_GET['songid'])){
$table="song";
$ID=$_GET['songid'];
}
else{
$table="singer";
$ID=$_GET['singerid'];
}将注释数存储在total变量中,然后将all变量设置为与total相等:
$countComments=$comments->countComments($ID,$table);
$total;
foreach($countComments as $key){
$total=$key[0];
}
$all=$total;然后计算页数:
$page=1;
$limit_end=15;
$limit_start=($_GET['page']*$limit_end)-$limit_end;
while($all>$limit_end){
$page++;
$all-=$limit_end;
}
$all_pages=$page;
$page_diff=$all_pages-$_GET['page'];定义设置ID、歌曲或歌手的链接:
if(isset($_GET['songid'])){
$next_link="/comments/song/".$_GET['songid']."/".($_GET['page']+1);
$prev_link="/comments/song/".$_GET['songid']."/".($_GET['page']-1);
$next_next_link="/comments/song/".$_GET['songid']."/".($_GET['page']+2);
$prev_prev_link="/comments/song/".$_GET['songid']."/".($_GET['page']-2);
$last_link="/comments/song/".$_GET['songid']."/".$page;
$first_link="/comments/song/".$_GET['songid']."/1";
}
else{
$next_link="/comments/singer/".$_GET['singerid']."/".($_GET['page']+1);
$prev_link="/comments/singer/".$_GET['singerid']."/".($_GET['page']-1);
$next_next_link="/comments/singer/".$_GET['singerid']."/".($_GET['page']+2);
$prev_prev_link="/comments/singer/".$_GET['singerid']."/".($_GET['page']-2);
$last_link="/comments/singer/".$_GET['singerid']."/".$page;
$first_link="/comments/singer/".$_GET['singerid']."/1";
}下面是使用function返回页面按钮的Heredoc:
function pageButtons($link,$page,$button_class,$mobile){
return <<
{$page}
{$mobile}
HTML;
}最后,显示结果的代码:
pages
1){
if(($_GET['page']+1)-3>1){
echo $comments->pageButtons($first_link,"First","btn-outline-light","");
}
if(($_GET['page']+1)-1>1){
echo $comments->pageButtons($prev_link,"Prev","btn-outline-light","");
}
if(($_GET['page']+1)-2>1){
echo $comments->pageButtons($prev_prev_link,$_GET['page']-2,"btn-outline-light",$_GET['page']-2);
}
if(($_GET['page']+1)-1>1){
echo $comments->pageButtons($prev_link,$_GET['page']-1,"btn-outline-light",$_GET['page']-1);
}
}
?>
value="">
$_GET['page']){
if($page_diff>=1){
echo $comments->pageButtons($next_link,$_GET['page']+1,"btn-outline-light",$_GET['page']+1);
}
if($page_diff>=2){
echo $comments->pageButtons($next_next_link,$_GET['page']+2,"btn-outline-light",$_GET['page']+2);
}
if($page_diff>=1){
echo $comments->pageButtons($next_link,"Next","btn-outline-light","");
}
if($page_diff>=3){
echo $comments->pageButtons($last_link,"Last","btn-outline-light","");
}
}
?>要使select工作,有一个简短的Jquery代码,如下所示:
$(".all-pages").on("change",function(){
$(this).find("option").each(function(){
if($(this).is(":selected")){
location.href=$(this).attr("value");
}
});
});再加上一点CSS
@media (max-width: 776px){
.page-buttons-container .page-buttons{
width:20px !important;
border-radius: 100% !important;
padding:0 !important;
}
.all-pages{
border-radius: 100%;
width: 25px;
height:25px;
border-width: 2px;
}
}
.all-pages {
-webkit-appearance: none;
-moz-appearance: none;
text-indent: 1px;
text-overflow: '';
text-indent: 8px;
width: 30px;
height:30px;
border-width: 2px;
}我想征求你对这条代码的意见/评论。它是否足够好,或者仍然有方法来改进它,使它更简单,更容易理解/可读性,或者其他什么?
如果您有任何问题,或者您不理解代码中的某些内容,请随意提问。
发布于 2019-11-09 17:50:43
首先,很高兴你决定问。而且,使用count(*)获取计数也是非常好的。
对于剩下的人,请跟随评审。
$total变量的代码是货物邪教代码的一个很好的例子。$all变量countComments()中使用fetchAll()是没有意义的。所以代码应该是$total = $comments->countComments($ID,$type);那是先发的,希望有人能把剩下的
https://codereview.stackexchange.com/questions/232112
复制相似问题