让DEDE随机更新文章

By | 2009/12/17

   使用DEDE的朋友都知道, DEDE的index.php,如果带上参数index.php?upcache=1,  就能更新静态首页index.html.   现在本人改造了一下, 让他也能随机更新几篇文章.

//自动生成HTML版
if(isset($_GET['upcache']))
{
    require_once (dirname(__FILE__) . "/include/common.inc.php");
    require_once DEDEINC."/arc.partview.class.php";
    $GLOBALS['_arclistEnv'] = 'index';
    $row = $dsql->GetOne("Select * From `#@__homepageset`");
    $row['templet'] = MfTemplet($row['templet']);
    $pv = new PartView();
    $pv->SetTemplet($cfg_basedir . $cfg_templets_dir . "/" . $row['templet']);
    $pv->SaveToHtml(dirname(__FILE__).'/index.html');
    //随机更新文章
    require_once(DEDEINC."/arc.archives.class.php");
    $row = $dsql->GetOne("select * from `#@__arctiny` order by id desc limit 1");
    $lastid = $row['id'];
    for ($i=1; $i<=8; $i++) {
        $id = mt_rand(1, $lastid);
        echo $id;
        $ac = new Archives($id);
        $rurl = $ac->MakeHtml();
    }
   
    include(dirname(__FILE__).'/index.html');
    exit();
}

  原理很简单, 就是利用mt_rand函数随机选定文章进行更新.

更新:

上面的方法有个问题没有考虑到, 就是如果ID不连续, 或者删除了文章后. 会取出不存在文章.更改如下:

    //随机更新文章
    require_once(DEDEINC."/arc.archives.class.php");   
    for ($i=1; $i<=8; $i++) {
        $row = $dsql->GetOne("select * from `#@__arctiny` where arcrank > -1 order by rand() limit 1");
        $id = $row['id'];
        $ac = new Archives($id);
        $rurl = $ac->MakeHtml();
    }

这是利用Mysql的查询条件rand()来实现随机取出数据.  不过这个查询的效率很低.  好在本人不是很在意效率. 这个方法还可以在sql中还可以指定文章的属性等.

原创文章 转载请注明出处: 登高望远 [ http://www.dengor.com/archives/460.html ]