本帖最后由 slavewen 于 2010-11-24 16:24 编辑
最近想给uchome增加一个“随便看看”的随机浏览插件,网上四处找遍,只找到一个标为 “随机访问 (C) Djunny 2009.6.5”的东东,非常简单,就一个rand.php文件,但是上传之后发现,它根本无法使用。不管怎么刷新,永远只显示uid为1的用户和blogid为1的文章。
今天有空看了下代码,弄了半天也没找出问题。最后一狠心,利用中午休息时间,自己写了一个,基本实现了日志、心情、投票、群组帖子和分享五大模块的随机浏览。在此共享给大家。
将下列代码存为rand.php,上传到uchome根目录,然后在template/default/header.htm中找到:
<li><a href="index.php">首页</a></li>
<!--{/if}-->
下边添加一行:
<li><a href="rand.php">随机</a></li>
ok!
- <?php
- include_once('./common.php');
- $random_dos = array('blog', 'doing', 'poll', 'thread', 'share');
- $rand = rand(0, count($random_dos)-1);
- switch($random_dos[$rand]){
- case 'blog':
- $url_format = 'space.php?do=blog&id=%d&uid=%d';
- $maxid = $_SGLOBAL['db']->result($_SGLOBAL['db']->query("SELECT MAX(blogid) FROM sh_blog"),0);
- $id = round(rand(1, $maxid));
- $sql = 'SELECT blogid,uid FROM sh_blog WHERE blogid ='.$id;
- $query = $_SGLOBAL['db']->query($sql);
- if($value=$_SGLOBAL['db']->fetch_array($query)){
- if($value[blogid]){
- header('location: '.sprintf($url_format, $value[blogid], $value[uid]));
- }else{
- header('location: network.php');
- }
- }else{
- header('location: network.php');
- }
- break;
- case 'thread':
- $url_format = 'space.php?do=thread&id=%d&uid=%d';
- $maxid = $_SGLOBAL['db']->result($_SGLOBAL['db']->query("SELECT MAX(tid) FROM sh_thread"),0);
- $id = round(rand(1, $maxid));
- $sql = 'SELECT tid,uid FROM sh_thread WHERE tid ='.$id;
- $query = $_SGLOBAL['db']->query($sql);
- if($value=$_SGLOBAL['db']->fetch_array($query)){
- if($value[tid]){
- header('location: '.sprintf($url_format, $value[tid], $value[uid]));
- }else{
- header('location: network.php');
- }
- }else{
- header('location: network.php');
- }
- break;
- case 'poll':
- $url_format = 'space.php?do=poll&pid=%d&uid=%d';
- $maxid = $_SGLOBAL['db']->result($_SGLOBAL['db']->query("SELECT MAX(pid) FROM sh_poll"),0);
- $id = round(rand(1, $maxid));
- $sql = 'SELECT pid,uid FROM sh_poll WHERE pid ='.$id;
- $query = $_SGLOBAL['db']->query($sql);
- if($value=$_SGLOBAL['db']->fetch_array($query)){
- if($value[pid]){
- header('location: '.sprintf($url_format, $value[pid], $value[uid]));
- }else{
- header('location: network.php');
- }
- }else{
- header('location: network.php');
- }
- break;
- case 'doing':
- $url_format = 'space.php?do=doing&doid=%d&uid=%d';
- $maxid = $_SGLOBAL['db']->result($_SGLOBAL['db']->query("SELECT MAX(doid) FROM sh_doing"),0);
- $id = round(rand(1, $maxid));
- $sql = 'SELECT doid,uid FROM sh_doing WHERE doid ='.$id;
- $query = $_SGLOBAL['db']->query($sql);
- if($value=$_SGLOBAL['db']->fetch_array($query)){
- if($value[doid]){
- header('location: '.sprintf($url_format, $value[doid], $value[uid]));
- }else{
- header('location: network.php');
- }
- }else{
- header('location: network.php');
- }
- break;
- case 'share':
- $url_format = 'space.php?do=share&sid=%d&uid=%d';
- $maxid = $_SGLOBAL['db']->result($_SGLOBAL['db']->query("SELECT MAX(sid) FROM sh_share"),0);
- $id = round(rand(1, $maxid));
- $sql = 'SELECT sid,uid FROM sh_share WHERE sid ='.$id;
- $query = $_SGLOBAL['db']->query($sql);
- if($value=$_SGLOBAL['db']->fetch_array($query)){
- if($value[sid]){
- header('location: '.sprintf($url_format, $value[sid], $value[uid]));
- }else{
- header('location: network.php');
- }
- }else{
- header('location: network.php');
- }
- break;
- default:
- header('location: network.php');
- }
- ?>
复制代码
|