技术文摘

帝国cms内容页调用随机文章代码的一种思路

作者:雨祺   发表于:
浏览:78次    字数:694  原创
级别: 站长   总稿: 68 篇,  月稿: 0
帝国cms内容页调用随机文章代码的一种思路。建站过程中,为了SEO优化不得不在内容页加入一些随机信息显示,以便于搜索引擎的抓取,但是数据量大了后,用rand随机会超级慢,怎么办呢?以下是一种思路:
1、在userfun.php中添加自定义函数:
  1. function zhann_get_keyid($tbname,$limit=15){ 
  2.     global $empire,$dbtbpre; 
  3.     $maxid=$empire->gettotal("select max(id) as total from {$dbtbpre}ecms_{$tbname} "); 
  4.     $ids=array(); 
  5.     $i=0;      
  6.     while($i<$limit){ 
  7.             $key=rand(1,$maxid); 
  8.             $key=$empire->gettotal("select id as total from {$dbtbpre}ecms_{$tbname} where id<=$key order by id desc limit 1 "); 
  9.             if(in_array($key,$ids))continue
  10.             $ids[]=$key; 
  11.             $i++; 
  12.     } 
  13.     $rand=implode(',',$ids);    
  14.     return $rand; 
2、模板中调用代码
  1. <?php 
  2. // 获取随机数字 
  3. $randomNumbers = zhann_get_keyid('cidian'); 
  4. // 构建SQL查询语句 
  5. $sql = $empire->query("select * from {$dbtbpre}ecms_cidian where id in ({$randomNumbers}) order by onclick limit 15"); 
  6. // 循环输出结果 
  7. while($r = $empire->fetch($sql)){ 
  8. ?> 
  9. <li><a href="<?=$r['titleurl']?>" title="<?=$r['title']?>"><?=$r['title']?></a></li> 
  10. <?php 
  11. ?> 

【审核人:站长】

收藏   加好友   生成海报   分享
点赞(0)
打赏
Tags: CMS 文章 帝国CMS 帝国

发布者资料

热门文章

技术文摘

查看更多技术文摘
首页
栏目
搜索
会员
投稿