phpcms/libs/functions/extention.func.php里面添加一个随机文章列表的函数:

/* * 随机读取文章列表 *
* @param $modelid 模型ID 必须
* @param $catid 栏目ID 可选,为0或未指定调去全模型的数据
* @param $siteid 站点ID 可选,默认为1
* @param $thumb 是否必须有缩微图 可选,默认为0
* @param $num 调用文章数量 可选,默认为10
* @return array 返回随机文章列表数组
*/
function suiji($modelid, $catid, $siteid = 1, $thumb = 0, $num = 10) {if(!$modelid = intval($modelid))return false;        $sitid = intval($siteid) ? intval($siteid) : 1;
                $catid = intval($catid);
                $num = intval($num);
                $thumb = intval($thumb) ? " AND thumb != ''" : '';
                $CATEGORYS = getcache('category_content_'.$siteid,'commons');
                if($CATEGORYS[$catid]['child']){
                                $catids_str = $CATEGORYS[$catid]['arrchildid'];
                                $pos = strpos($catids_str,',')+1;
                                $catids_str = substr($catids_str, $pos);
                                $sql = " AND catid IN ($catids_str)".$thumb;
                } elseif($catid) {
                                $sql = " AND catid='$catid'".$thumb;
                } else {
                                $sql = $thumb;
                }
                $c_db = pc_base::load_model('content_model');
                 $c_db->set_model($modelid);
                
                $id = '';
                
                for($i=1;$i<=$num;$i++)
                {
                                $not = $id ? " AND id NOT IN ($id) " : '';
                                $rs = $c_db->query("SELECT * FROM $c_db->table_name AS t1 JOIN (SELECT ROUND(RAND()*(SELECT MAX(id) FROM $c_db->table_name WHERE status = 99 $sql)) AS id2) AS t2 WHERE t1.id>=t2.id2 and t1.status = 99 $sql $not ORDER BY t1.id ASC LIMIT 1");
                                while($r = mysql_fetch_array($rs,MYSQL_ASSOC)){
                                                $return[]=$r;
                                                $id = $id ? $id.','.$r['id'] : $r['id'];
                                }
                }
                 return $return;
}

使用举例:

<div class="box">
     <h5 class="title-2">随机文章</h5>
     <ul class="content">
     {php $data = suiji(1,0,1,0,15)}
                {loop $data $r}
                <li> · <a href="{$r[url]}" target="_blank" title="{$r[title]}">{str_cut($r[title], 51, '')}</a></li>
                {/loop}
        </ul>
</div>

本修改不影响以后v9升级,因为extention.func.php文件就是官方专门用了让开放者放自己的扩展函数库的。

暂无评论