分享一段更高效的帝国CMS调用上一篇下一篇方法,支持重复多次调用,但只查一次数据库。
查询SQL
<?php//上下翻页$goPage = [ 'prev' => ['text' => '上一题'], 'next' => ['text' => '下一题']];$sql = $empire->query("(SELECT titleurl, id, title FROM `{$dbtbpre}ecms_news` WHERE `id` < " . $navinfor['id'] . " AND `classid` = " . $navinfor['classid'] . " ORDER BY `id` DESC LIMIT 1) UNION (SELECT titleurl, id, title FROM `{$dbtbpre}ecms_news` WHERE `id`> " . $navinfor['id'] . " AND `classid` = " . $navinfor['classid'] . " ORDER BY `id` LIMIT 1)");if(0 < $empire->num1($sql)) { while($r = $empire->fetch($sql)){ $key = isset($r['id']) && $r['id'] > $navinfor['id'] ? 'next' : 'prev'; $goPage[$key]['title'] = $r['title']; $goPage[$key]['titleurl'] = sys_ReturnBqTitleLink($r); }}?>
显示代码
<?php foreach($goPage as $type => $page):?> <div class="col-sm-6 mb-5"> <div class="card"> <div class="card-body position-relative"> <?php if(isset($page['title'])):?> <a href="<?php echo $page['titleurl'];?>" tltle="<?php echo $page['title'];?>" class="btn btn-danger stretched-link"><?php echo $page['text'];?></a> <?php else:?> <a href="javascript:;" class="btn btn-primary disabled" role="button" aria-disabled="true"><?php echo $page['text'];?></a> <?php endif;?> </div> </div> </div> <?php endforeach;?>