文章很多时,读者不知道该看哪篇文章,我们可随机给读者显示一篇文章,在网上好的都是介绍如何在一个固定地方随机展示一下文章,今天给大家分享一种可以直接进到某文章页面的随机跳转。

利用MySQL的随机排列

首先是随机查询一篇文章出来。

  • <?php
  • $db = Typecho_Db::get();
  • $sql = $db->select()->from('table.contents')
  • ->where('status = ?','publish')
  • ->where('type = ?', 'post')
  • ->where('created <= unix_timestamp(now())', 'post')
  • ->limit(1)
  • ->order('RAND()');
  • $result = $db->fetchAll($sql);
  • ?>
复制

PS:307是临时跳转的状态码

利用PHP的随机函数

然而昨天发现MySQL的查询有缓存,所以上边的方法很可能多次查询出来的文章是同一篇。
所以换个思路,直接查询文章cid的大小范围,然后直接在这个范围内取随机数,然后检测该随机数是否在cid列中存在,若存在,该cid是否对应的是文章,如果是,输出结果,不是,继续随机。

  • <?php
  • $db = Typecho_Db::get();
  • $sql = $db->select('MAX(cid)')->from('table.contents')
  • ->where('status = ?','publish')
  • ->where('type = ?', 'post')
  • ->where('created <= unix_timestamp(now())', 'post');
  • $result = $db->fetchAll($sql);
  • $max_id = $result[0]['MAX(`cid`)'];//POST类型数据最大的CID
  • $sql = $db->select('MIN(cid)')->from('table.contents')
  • ->where('status = ?','publish')
  • ->where('type = ?', 'post')
  • ->where('created <= unix_timestamp(now())', 'post');
  • $result = $db->fetchAll($sql);
  • $min_id = $result[0]['MIN(`cid`)'];//POST类型数据最小的CID
  • $result = NULL;
  • while($result == NULL) {
  • $rand_id = mt_rand($min_id,$max_id);
  • $sql = $db->select()->from('table.contents')
  • ->where('status = ?','publish')
  • ->where('type = ?', 'post')
  • ->where('created <= unix_timestamp(now())', 'post')
  • ->where('cid = ?',$rand_id);
  • $result = $db->fetchAll($sql);
  • }
  • ?>
复制

使用方法

$result[0]就是我们取到的一篇文章。
如果你想直接展示(例如在404页面),请使用

  • <?php _e($result[0]['content']) ?>
复制

不过我是想实现手气不错的功能,然而数据库中没有直接保存文章的链接,只有SLUG,所以得通过Typecho的内置函数转换一下。

  • <?php $target = Typecho_Widget::widget('Widget_Abstract_Contents')->filter($result['0']); ?>
复制

最后跳转即可。

  • <?php $this->response->redirect($target['permalink'],307); ?>
复制