导读:
全网独家,真实有效的Discuz!帖子分页功能,网上也有其他的文章模块分页方法,但没有针对帖子的。我自己的论坛网站需要做分页,无奈discuz官方一直都不搞分页的,唯有自己DIY一波了。这几天我也百度了很多资料,才最终研究出来,现在把经验分享给大家!
步骤:
1、在网站根目录新建一个名为code.php的入口文件 ,代码如下:
<?php
/**
* 新增加的code.php入口文件
*/
define('APPTYPEID', 2);
define('CURSCRIPT', 'code');
require './source/class/class_core.php';
require DISCUZ_ROOT.'./source/function/function_code.php';
/*创建及初始化对象*/
$discuz = C::app();
$discuz->reject_robot();
$discuz->init();
/*分类数组*/
$modarray = array('1', '2','3','4','5','6','7','8','9','10');
if(empty($_GET['mod']) || !in_array($_GET['mod'],$modarray)){
$_GET['mod'] = 'index';
}
/*------------------------设置常量-------------------------*/
/*DIY模块标识*/
define('DIY_MOD', $_GET['mod']);
/*DIY模块id(注明:下面的495是我帖子DIY模块的id,查看方法:在DIY模式下,把鼠标放到模块上,右上角会显示这个id值出来)*/
define('DIY_BID', isset($_GET['bid'])?$_GET['bid']:495);
/*每页显示多少条数据*/
define('DIY_SIZE', isset($_GET['size'])?$_GET['size']:10);
/*---------------------------------------------------------*/
/*执行钩子*/
runhooks();
/*论坛说明*/
$navtitle = str_replace('{bbname}', $_G['setting']['bbname'], $_G['setting']['seotitle']['code']);
$_G['disabledwidthauto'] = 1;
/*转向控制器*/
require_once libfile('code/index', 'module');
?>
复制代码
2、source\function\下新建 function_code.php ****(感觉这文件可有可无,还是按要求写一下吧) ,代码如下:
<?php
/**
* 定义不同的mod方法
*/
if(!defined('IN_DISCUZ')) {
exit('Access Denied');
}
?>
复制代码
3、source\module\下创建****code文件夹 ,code文件夹里新建****code_index.php , (这个是后端控制器) 代码如下:
<?php
/**
* 后端控制器
*/
if(!defined('IN_DISCUZ')) {
exit('Access Denied');
}
list($navtitle, $metadescription, $metakeywords) = get_seosetting('portal');
if(!$navtitle) {
$navtitle = $_G['setting']['navs'][1]['navname'];
$nobbname = false;
} else {
$nobbname = true;
}
if(!$metakeywords) {
$metakeywords = $_G['setting']['navs'][1]['navname'];
}
if(!$metadescription) {
$metadescription = $_G['setting']['navs'][1]['navname'];
}
//============================分页 start ============================//
require_once libfile('function/discuzcode');
require_once libfile('function/home');
//DIY模块标识
$mod=DIY_MOD;
//DIY模块id
$bid=DIY_BID;
//每页显示条数
$perpage = DIY_SIZE;
//允许显示的最大页数
$maxpages = 0;
//最多显示多少页码(超过就用...隐藏)
$page = 10;
//所有记录数量
$count = C::t('code')->fetch_count_sql($bid);
if($count>0){
// 计算总页数
$totalPage = (int)(($count + $perpage -1) / $perpage);
//页码控制
$curpage = isset($_GET['page'])?$_GET['page']:1;
if($curpage<1) $curpage = 1;
if($totalPage<(int)$curpage) $curpage = $totalPage;
//开始记录
$start = ($curpage-1)*$perpage;
//分页判断
ckstart($start, $perpage);
//此数组用于显示具体页数
$pageArray=array();
for ($i=1; $i<=$totalPage; $i++)
{
array_push($pageArray,$i);
}
}
//跳转的路径
$mpurl = 'code.php?mod='.$mod.'&bid='.$bid;
//最后一页,自动跳转
$autogoto = FALSE;
//是否简洁模式(简洁模式不显示上一页、下一页和页码跳转)
$simple = FALSE;
//分页
$multipage = multi($count, $perpage, $curpage, $mpurl, $maxpages , $page, $autogoto, $simple);
/**
* 分页函数multi()说明
* @param $count - 总数
* @param $perpage - 每页数
* @param $curpage - 当前页
* @param $mpurl - 跳转的路径
* @param $maxpages - 允许显示的最大页数
* @param $page - 最多显示多少页码(超过就用...隐藏)
* @param $autogoto - 最后一页,自动跳转
* @param $simple - 是否简洁模式(简洁模式不显示上一页、下一页和页码跳转)
* @return - 返回分页代码
*/
//============================分页 end ============================//
//跳转到前端视图
include_once template('diy:code/'.$mod);
?>
复制代码
4、\source\class\table\下新建****table_code.php , (这个是数据访问层) 代码如下:
<?php
require_once libfile('function/home');
/**
* code.php页面的分页查询
*/
if(!defined('IN_DISCUZ')) {
exit('Access Denied');
}
class table_code extends discuz_table
{
public function __construct() {
$this->_table = 'common_block_item';//表名
$this->_pk = 'itemid'; //表的主键
parent::__construct();
}
//查询记录总条数
public function fetch_count_sql($bid) {
$where="where bid=".$bid." ORDER BY itemid DESC";
return DB::result_first('SELECT COUNT(*) FROM %t %i ', array($this->_table, $where));
}
//查询数据
public function fetch_all_by_sql($start, $limit,$bid) {
return DB::fetch_all('SELECT * FROM %t where bid='.$bid.' ORDER BY itemid DESC'.DB::limit($start, $limit), array($this->_table));
}
}
?>
复制代码
5、打开\source\class\table\下的****table_common_block_item.php ,这个文件是查询DIY帖子模块的,找到****function fetch_all_by_bid( $bids , $sort = false )方法,修改成下面的代码:
public function fetch_all_by_bid($bids, $sort = false) {
$sql='SELECT * FROM '.DB::table($this->_table).' WHERE '.DB::field('bid', $bids).($sort ? ' ORDER BY displayorder, itemtype DESC' : ' ORDER BY itemid DESC');
/*===========================帖子模块增加分页功能 start =========================*/
if(in_array(DIY_BID,$bids)){
/*获取当前页*/
$page = isset($_GET['page'])?$_GET['page']:0;
if($page<1){
$page=1;
}
/*每页显示多少条*/
$limit=DIY_SIZE;
/*开始记录*/
$start = ($page-1)*$limit;
$sql.=DB::limit($start, $limit);
/* runlog("sql","sql语句:".$sql); */
/*替换排序*/
$sql = str_replace("DESC","ASC",$sql);
}
/*=================================== end ======================================*/
$result=DB::fetch_all($sql, null, $this->_pk);
return $result;
}
复制代码
6、模板目录 template\default\下创建****code文件夹 ,code文件夹里新建****index.htm , (这个是前端视图) 代码如下:
<!--{template common/header}-->
<style id="diy_style" type="text/css"></style>
<style type="text/css">
body{ background:#F6F7F9!important;}
.wp, #wp{ width:100%;}
.nexwp{ width:1180px; margin:20px auto 0 auto; }
.nex_portbox{ margin-top:20px;}
.nex_portbox ul{ width:1205px;}
.nex_portbox ul li{ float:left; width:223.2px; margin:0 16px 16px 0; background:#fff; box-shadow: 0 2px 3px rgba(0,0,0,0.06);-webkit-transition:all 0.3s linear;-moz-transition:all 0.3s linear;-o-transition:all 0.3s linear;-ms-transition:all 0.3s linear;transition:all 0.3s linear;border-radius:0 0 4px 4px;}
.nex_LG_piv a{ display:block; width:223.2px; height:167px; position:relative;}
.nex_LG_piv a img{width:223px; height:167px; border-radius:4px 4px 0 0;}
.nex_portbox ul li:hover .nex_LG_piv a img{ opacity:0.8;}
.nex_LG_info{ border-bottom: 1px solid #ededed; padding: 11px 16px 13px; height: 75px; position: relative;}
.nex_LG_info h5{ height:22px; line-height:22px; overflow:hidden; }
.nex_LG_info h5 a{ font-size: 14px;color: #333; font-weight:400;}
.nex_LG_info h5 a:hover{ color:#0EC5A1;}
.nex_LG_type{ font-size: 12px; color: #bbb; margin-bottom: 17px; height: 18px;}
.nex_LG_item span{font-size: 12px; color: #bbb;margin-right: 10px; padding-left:25px;}
.nex_LG_item span.nex_statistics_view{ background:url(./template/wfdsoft_freegift_171220/neoconex/portal_list_view/viewx.png) left center no-repeat;}
.nex_LG_item span.nex_statistics_comment{ background:url(./template/wfdsoft_freegift_171220/neoconex/portal_list_view/reply.png) left center no-repeat;}
.nex_LG_btms{height: 24px; line-height: 24px; padding: 14px 16px; font-size: 12px; border-radius:0 0 4px 4px; position: relative;}
.nex_LG_btms a{ display:block; float:left;}
.nex_LG_btms a img{ width:24px; height:24px; border-radius:100%; display:block; float:left;}
.nex_LG_btms a em{ display:block; float:left; margin-left:10px; height:24px; line-height:24px; font-size:12px; color:#333;}
.nex_LG_btms a:hover em{ color:#0EC5A1;}
.nex_LG_btms span{ display:block; width:70px; overflow:hidden; text-align:right; float: right; font-size:12px; color:#bbb; height:24px; line-height:24px;}
.nex_LG_txts{ height:30px; line-height:30px; margin-bottom:20px; font-size:16px; color:#666; font-weight:400;}
.nex_LG_Links ul{ width:1205px;}
.nex_LG_Links ul li{ float:left; width:223.2px; margin:0 16px 16px 0; background:#fff; box-shadow: 0 2px 3px rgba(0,0,0,0.06);-webkit-transition:all 0.3s linear;-moz-transition:all 0.3s linear;-o-transition:all 0.3s linear;-ms-transition:all 0.3s linear;transition:all 0.3s linear;border-radius:0 0 4px 4px;}
.nex_LG_Links ul li:hover .nex_LG_piv a img{ opacity:0.8;}
.nexads{ width:1180px; margin:10px 0;}
.nexads img{ width:1180px;}
.nexpubtop {
height: 38px;
margin-bottom: 20px;
}
.nexpubtop span {
display: block;
float: left;
padding-left: 45px;
height: 38px;
line-height: 38px;
}
.nexpubtop ul {
float: right;
margin-top: 6px;
}
.nexpubtop ul li.cur {
background: #0EC5A1;
color: #fff;
border: 1px solid #0EC5A1;
}
.nexpubtop ul li {
float: left;
padding: 0 18px;
margin-left: 10px;
border: 1px solid #ededed;
height: 25px;
line-height: 25px;
border-radius: 2px;
font-size: 14px;
color: #666;
cursor: pointer;
}
.nexsucaibox ul{display:none;}
</style>
<div class="nexwp">
<div id="pt" class="bm cl">
<div class="z">
<a href="./" class="nvhm" title="{lang homepage}">$_G[setting][bbname]</a> <em>›</em>
全部分类
</div>
</div>
<div class="nexpubtop">
<ul>
<li class="cur"><a href="code.php?bid=495">全部分类</a></li>
<li><a href="code.php?mod=1&bid=496">分类一</a></li>
<li><a href="code.php?mod=2&bid=497">分类二</a></li>
<li><a href="code.php?mod=3&bid=498">分类三</a></li>
<li><a href="code.php?mod=4&bid=499">分类四</a></li>
<li><a href="code.php?mod=5&bid=500">分类五</a></li>
<div class="clear"></div>
</ul>
<div class="clear"></div>
</div>
<div class="nexsucaibox nex_LG_Links">
<ul style="display:block;">
<!--[diy=nex_code_list1]--><div id="nex_code_list1" class="area"></div><!--[/diy]-->
<div class="clear"></div>
</ul>
</div>
<script type="text/javascript">
//切换导航
jq(".nexpubtop ul li").each(function(s){
jq(this).click(function(){
jq(this).addClass("cur").siblings().removeClass("cur");
})
});
</script>
<div class="clear"></div>
<!--分页-->
<div class="pgs cl">
<!--{if $multipage}-->$multipage<!--{/if}-->
</div>
</div>
<div class="nexwp">
<div class="nex_LG_txts">猜你喜欢</div>
<div class="nex_LG_Links">
<ul>
<!--[diy=nex_LG_Links]--><div id="nex_LG_Links" class="area"></div><!--[/diy]-->
<div class="clear"></div>
</ul>
</div>
<!--ads-->
<div class="nexads">
<div class="w1180">
<!--[diy=nexads]--><div id="nexads" class="area"></div><!--[/diy]-->
</div>
</div>
</div>
<!--{template common/footer}-->
复制代码
7. 重要的一点,需要关掉redis缓存才能使用分页,修改config/config_global.php里$_config[‘memory’][‘redis’]的配置,如下:
// -------------------------- CONFIG MEMORY --------------------------- //
/*--- 这里不建议开启redis缓存,因为开了之后,就不能实现分页实时显示了。---*/
$_config['memory']['prefix'] = 'uXJfik_';
//$_config['memory']['redis']['server'] = '127.0.0.1';
$_config['memory']['redis']['server'] = '';
$_config['memory']['redis']['port'] = 6379;
$_config['memory']['redis']['pconnect'] = 1;
$_config['memory']['redis']['timeout'] = '0';
$_config['memory']['redis']['requirepass'] = '';
$_config['memory']['redis']['serializer'] = 1;
$_config['memory']['memcache']['server'] = '';
$_config['memory']['memcache']['port'] = 11211;
$_config['memory']['memcache']['pconnect'] = 1;
$_config['memory']['memcache']['timeout'] = 1;
$_config['memory']['apc'] = '0';
//$_config['memory']['apcu'] = '1';
$_config['memory']['apcu'] = '0';
$_config['memory']['xcache'] = '0';
$_config['memory']['eaccelerator'] = '0';
$_config['memory']['wincache'] = '0';
$_config['memory']['yac'] = '0';
//$_config['memory']['file']['server'] = 'data/cache/filecache';
$_config['memory']['file']['server'] = '';
// -------------------------- CONFIG SERVER --------------------------- //
复制代码
大功告成,欧耶!所有代码都做了详细的备注,希望对你有用。大家参考别人的代码要注重于逻辑是怎么写的,欢迎访问我的网站看效果,网址:zy13.net/code.php
© 版权声明
文章版权归作者所有,未经允许请勿转载。
THE END