之前分享过在phpcmsv9中实现tags标签页面的纯静态化实现方法,不过当初没有把分页处理,在phpcmsv9中的全局函数中有分页导航生成函数所以就没写分页的实现方法了。不过前段时间有不少网友来问我,tags页面纯静态页面分页怎么处理。所以,博主我觉得还是有必要分享把分页的实现方法也写除来吧。
(其实有了分页导航函数,分页的实现是非常简单,只不过现在很多年轻人太懒了,心月博主只好辛苦一点,把分页的实现也一起分享出来吧)
在之前分享的tags标签页面的纯静态化实现方法,主要以功能实现为主,所以在界面显示上有些瑕疵,这次随便把之前的界面显示问题一起解决了吧。
修改后的效如同批量生产文章内容页
【分页和不分页不同有两处】
1、页面数据量的不同
2、有无分页导航
把分页和改造后的代码一起放上来吧:
(phpcms/modules/content/create_html.php)
(下面这些方法是我自己加上去的,所以你们自己的这个文件中是没有的,加到create_html.php 文件下面的最后一个方法后面)
/** * 批量生成标签页(tag与文章关联的页面) */ public function tag() { if(isset($_POST['dosubmit'])){ $keyword_db = pc_base::load_model('keyword_model'); extract($_POST,EXTR_SKIP); $page = max(intval($page), 1); $offset = $pagesize*($page-1); $message = L('start_update'); $forward = "?m=content&c=create_html&a=tag&pagesize=$pagesize&dosubmit=1&page=$page"; if(!isset($total)) { $total = $keyword_db->count('videonum>2'); $pages = ceil($total/$pagesize); } //获取tag $this->html = pc_base::load_app_class('html'); $keyword_db = pc_base::load_model('keyword_model'); // $t = $keyword_db->select("videonum>2","*","$offset,$pagesize"); $t = $keyword_db->select("","*","$offset,$pagesize"); if (!$t || empty($t)) showmessage(L('create_update_success'),'?m=content&c=create_html&a=tag',200); foreach ($t as $tags){ $tag = $tags['keyword']; $datas = $this->getTagList($tag); if (!empty($datas)) { //$tag = iconv('utf-8', 'gbk', $tag); $this->html->tag('tag', $tag, $datas); }else{ continue; } } $page++; $creatednum = $offset + count($data); $percent = round($creatednum/$total, 2)*100; $message = '【批量更新tags页面】 '.L('have_update_items',array('total'=>$total,'creatednum'=>$creatednum,'percent'=>$percent)); $forward = "?m=content&c=create_html&a=tag&dosubmit=1&pagesize=$pagesize&page=$page&pages=$pages&total=$total"; showmessage($message,$forward,200); }else{ include $this->admin_tpl('create_html_tag'); } } //tags列表 有分页 public function tagsIndex() { if (isset($_POST['dosubmit'])) { $keyword_db = pc_base::load_model('keyword_model'); $this->html = pc_base::load_app_class('html'); $keyword_db = pc_base::load_model('keyword_model'); if (!empty($_POST['total'])) { $total = intval($_POST['total']); }else{ $total = $keyword_db->count(); } $pagesize = !empty($_POST['pagesize']) && $_POST['pagesize']>1 ? intval($_POST['pagesize']) : 20; $now = $page = !empty($_POST['page']) && $_POST['page']>0 ? intval($_POST['page']) : 1; $offset = ($page-1)*$pagesize; $pages = ceil($total/$pagesize); $urlrule = '/tag/index.html~/tag/tag_{$page}.html'; //分页导航 $pageNav = pages($total, $page, $pagesize, $urlrule); $t = $keyword_db->select("","*","$offset, $pagesize"); if (!empty($t)) { $size = $this->html->tag_index('tag', 'tag标签列表', $t, $page, $pageNav); $page += 1; $forward = "?m=content&c=create_html&a=tagsIndex&dosubmit=1&pagesize=$pagesize&page=$page&total=$total"; showmessage("共{$total}个标签/{$pages}页,已更新{$now}页",$forward,200); }else{ $forward = "?m=content&c=create_html&a=tagsIndex"; showmessage("tags列表更新完成",$forward,200); } }else{ include $this->admin_tpl('create_html_tag_index'); } } //tags列表 不分页 public function tagsIndex2() { $keyword_db = pc_base::load_model('keyword_model'); $this->html = pc_base::load_app_class('html'); $keyword_db = pc_base::load_model('keyword_model'); $t = $keyword_db->select("","*"); $size = $this->html->tag_index('tag', 'tag标签列表', $t); showmessage('标签列表页更新成功,大小:'.sizecount($size)); } /** * 获取关键词关联文章数据 * @param $tagName * @return mixed */ public function getTagList($tagName) { $tag = $tagName; $keyword_db = pc_base::load_model('keyword_model'); $keyword_data_db = pc_base::load_model('keyword_data_model'); //获取标签id $r = $keyword_db->get_one(array('keyword'=>$tag, 'siteid'=>$this->siteid), 'id'); if (!$r['id']) showmessage('不存在此关键字!'); $tagid = intval($r['id']); $page = max($_GET['page'], 1); $pagesize = 20; $where = '`tagid`=\''.$tagid.'\' AND `siteid`='.$this->siteid.' group by contentid'; $infos = $keyword_data_db->listinfo($where, '`id` DESC'); $arrRes['total'] = count($infos); if ($arrRes['total']<3) { return null; } if (is_array($infos)) { $datas = array(); foreach ($infos as $info) { list($contentid, $modelid) = explode('-', $info['contentid']); $this->db->set_model($modelid); $res = $this->db->get_one(array('id'=>$contentid), 'id, title, description, url, inputtime, style', '', 'inputtime desc',' title'); $res['title'] = str_replace($tag, '<font color="#f00">'.$tag.'</font>', $res['title']); $res['description'] = str_replace($tag, '<font color="#f00">'.$tag.'</font>', $res['description']); $datas[] = $res; } } $arrRes['info'] = $datas; return $arrRes; }
(phpcms/modules/content/classes/html.class.php)
/** * 生成tag 文章关联页面 有分页 * @param $file * @param $tagName * @param string $data * @return bool|int|void */ public function tag($file, $tagName, $data='', $pagesize = 10) { // $file = PHPCMS_PATH.safe_replace($file).'/'.$tagName.'.html'; $datas = $data['info']; $total = $data['total']; $SEO = seo($this->siteid); $siteid = $this->siteid; $style = $this->sitelist[$siteid]['default_style']; $tag = $tagName; if (!$total || !trim($tagName)) { return false; } //分页 array_chunk(input, size) //tag与文章关联的列表分页在这里处理 // 通过 array_chunk() 数组函数把与tag关联的所有文章按照每页显示的数量进行拆分 $pagesize = 1; $urlrule = '/tag/'.$tagName.'.html~/tag/'.$tagName.'_{$page}.html'; $tagDatas = array_chunk($datas, $pagesize); $pageNow = 1; //数据被分组拆分后循环生成页面 foreach ($tagDatas as $key => $value) { if ($pageNow>1) { $htmlfile = PHPCMS_PATH.safe_replace($file).'/'.$tagName.'_'.$pageNow.'.html'; }else{ $htmlfile = PHPCMS_PATH.safe_replace($file).'/'.$tagName.'.html'; } $tagData = $value; //分页导航 $pages = pages($total, $pageNow, $pagesize, $urlrule); ob_start(); include template('content','tag_list',$style); $this->createhtml($htmlfile); ob_clean(); $pageNow += 1; } return true; } /** * 生成tag 文章关联页面 没有分页 * @param $file * @param $tagName * @param string $data * @return bool|int|void */ public function tag2($file, $tagName, $data='', $pagesize = 10) { $file = PHPCMS_PATH.safe_replace($file).'/'.$tagName.'.html'; $datas = $data['info']; $total = $data['total']; $SEO = seo($this->siteid); $siteid = $this->siteid; $style = $this->sitelist[$siteid]['default_style']; $tag = $tagName; if (!$total || !trim($tagName)) { return false; } //不分页 ob_start(); include template('content','tag',$style); return $this->createhtml($file); } /** * tags列表 */ public function tag_index($file, $tagName, $data='',$page=1,$pageNav='') { if ($page>1) { $file = PHPCMS_PATH.safe_replace($file).'/tag_'.$page.'.html'; }else{ $file = PHPCMS_PATH.safe_replace($file).'/index.html'; } $SEO = seo($this->siteid); $tagDatas = $data; $siteid = $this->siteid; $pages = $pageNav; $style = $this->sitelist[$siteid]['default_style']; if (empty($tagDatas)) { return false; } $CATEGORYS = $this->categorys; ob_start(); include template('content','tag',$style); return $this->createhtml($file); }
另外还需要在content模块里添加两个后台模板文件:
文件内容
1、create_html_tag.tpl.php ( 文件位置:phpcms/modules/content/templates/)
<?php defined('IN_ADMIN') or exit('No permission resources.'); include $this->admin_tpl('header','admin');?> <div class="pad-10"> <div class="explain-col"> <?php echo L('html_notice');?> </div> <div class="bk10"></div> <div class="table-list"> <table width="100%" cellspacing="0"> <form action="?m=content&c=create_html&a=tag" method="post" name="myform"> <input type="hidden" name="dosubmit" value="1"> <input type="hidden" name="type" value="lastinput"> <thead> <tr> <th align="center" width="150"></th> <th align="center" width="386"></th> <th align="center"></th> </tr> </thead> <tbody height="200" class="nHover td-line"> <tr> <td align="center" rowspan="6"> </td> </tr> <tr> <td align="center" rowspan="6"></td> <td><font color="red"><?php echo L('every_time');?> <input type="text" name="pagesize" value="10" size="4"> <?php echo L('information_items');?></font></td> </tr> <tr> <td><?php echo L('update_all');?> <input type="button" name="dosubmit1" value=" <?php echo L('submit_start_update');?> " class="button" onclick="myform.type.value='all';myform.submit();"></td> </tr> <?php if($modelid) { ?> <tr> <td><?php echo L('last_information');?> <input type="text" name="number" value="100" size="5"> <?php echo L('information_items');?><input type="button" class="button" name="dosubmit2" value=" <?php echo L('submit_start_update');?> " onclick="myform.type.value='lastinput';myform.submit();"></td> </tr> <tr> <td><?php echo L('update_time_from');?> <?php echo form::date('fromdate');?> <?php echo L('to');?> <?php echo form::date('todate');?><?php echo L('in_information');?> <input type="button" name="dosubmit3" value=" <?php echo L('submit_start_update');?> " class="button" onclick="myform.type.value='date';myform.submit();"></td> </tr> <tr> <td><?php echo L('update_id_from');?> <input type="text" name="fromid" value="0" size="8"> <?php echo L('to');?> <input type="text" name="toid" size="8"> <?php echo L('in_information');?> <input type="button" class="button" name="dosubmit4" value=" <?php echo L('submit_start_update');?> " onclick="myform.type.value='id';myform.submit();"></td> </tr> <?php } ?> <tr> <td></td> </tr> </tbody> </form> </table> </div> </div> <script language="JavaScript"> <!-- window.top.$('#display_center_id').css('display','none'); function change_model(modelid) { window.location.href='?m=content&c=create_html&a=show&modelid='+modelid+'&pc_hash='+pc_hash; } //--> </script>
2、create_html_tag——index.tpl.php ( 文件位置:phpcms/modules/content/templates/)(如果不做tags列表分页的话可以不用加这个文件)
<?php defined('IN_ADMIN') or exit('No permission resources.'); include $this->admin_tpl('header','admin');?> <div class="pad-10"> <div class="explain-col"> <?php echo L('html_notice');?> </div> <div class="bk10"></div> <div class="table-list"> <table width="100%" cellspacing="0"> <form action="?m=content&c=create_html&a=tagsIndex" method="post" name="myform"> <input type="hidden" name="dosubmit" value="1"> <input type="hidden" name="type" value="lastinput"> <thead> <tr> <th align="center" width="150"></th> <th align="center" width="386"></th> <th align="center"></th> </tr> </thead> <tbody height="200" class="nHover td-line"> <tr> <td align="center" rowspan="6"> </td> </tr> <tr> <td align="center" rowspan="6"></td> <td><font color="red">每页tag数量 <input type="text" name="pagesize" value="20" size="4"> </font></td> </tr> <tr> <td><?php echo L('update_all');?> <input type="button" name="dosubmit1" value=" <?php echo L('submit_start_update');?> " class="button" onclick="myform.type.value='all';myform.submit();"></td> </tr> <?php if($modelid) { ?> <tr> <td><?php echo L('last_information');?> <input type="text" name="number" value="100" size="5"> <?php echo L('information_items');?><input type="button" class="button" name="dosubmit2" value=" <?php echo L('submit_start_update');?> " onclick="myform.type.value='lastinput';myform.submit();"></td> </tr> <tr> <td><?php echo L('update_time_from');?> <?php echo form::date('fromdate');?> <?php echo L('to');?> <?php echo form::date('todate');?><?php echo L('in_information');?> <input type="button" name="dosubmit3" value=" <?php echo L('submit_start_update');?> " class="button" onclick="myform.type.value='date';myform.submit();"></td> </tr> <tr> <td><?php echo L('update_id_from');?> <input type="text" name="fromid" value="0" size="8"> <?php echo L('to');?> <input type="text" name="toid" size="8"> <?php echo L('in_information');?> <input type="button" class="button" name="dosubmit4" value=" <?php echo L('submit_start_update');?> " onclick="myform.type.value='id';myform.submit();"></td> </tr> <?php } ?> <tr> <td></td> </tr> </tbody> </form> </table> </div> </div> <script language="JavaScript"> <!-- window.top.$('#display_center_id').css('display','none'); function change_model(modelid) { window.location.href='?m=content&c=create_html&a=show&modelid='+modelid+'&pc_hash='+pc_hash; } //--> </script>
分页效果:
phpcmsv9 tags页面纯静态化分页就分享到这里,有什么不同的可以文章后面留言或者或者返回意见留言。