帝国巧用数组查询上一篇下一篇(此举能减少SQL查询)上一篇和下一篇,最多两条,最少一条 有UNION呢?合并语法,减少一个查询,执行时间会变长一点,相对于两个查询还是更划算的,下面开始上代码:
- <?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 = $r['id'] > $navinfor['id'] ? 'next' : 'prev';
- $goPage[$key]['title'] = $r['title'];
- $goPage[$key]['titleurl'] = sys_ReturnBqTitleLink($r);
- }
- }
- ?>
显示页码
- <nav class="blog-pagination" aria-label="Pagination">
- <?php foreach($goPage as $type => $page):?>
- <a class="btn btn-lg<?php echo !isset($page['title']) ? ' btn-outline-secondary disabled': ' btn-outline-danger';?> me-2" href="<?php echo $page['titleurl'] ?? '';?>"><?php echo $page['text'];?></a>
- <?php endforeach;?>
- </nav>
注:为空就是表示没有了!可以加栏目链接