wecenter随机文章的实现

sl514
sl514 这家伙很懒,还没有设置简介

4 人点赞了该文章 · 3717 浏览

首先在/models/article.php里增加方法
	public function get_radom_article(){
		$table1 = $this->get_table('article') ;
		$sql = 'SELECT * 
FROM `'.$table1 .'` AS t1 JOIN (SELECT ROUND(RAND() * ((SELECT MAX(id) FROM `'.$table1 .'`)-(SELECT MIN(id) FROM `'.$table1 .'`))+(SELECT MIN(id) FROM `'.$table1 .'`)) AS id) AS t2 
WHERE t1.id >= t2.id 
ORDER BY t1.id LIMIT 10';//效率比order by radom高
 return $this->query_all($sql);
		
	}
然后在要显示随机文章页面的app下的模块的main函数的方法里加入
TPL::assign('radom_articles', $this->model('article')->get_radom_article());
然后写一个页面 建议这个路径 views/default/block/sider_radom_post.tpl.htm 里写上
<?php if ($this->radom_articles) { ?>
	<!-- 相关问题 -->
	<div class="aw-mod aw-text-align-justify question-related-list"  style="padding:10px;">
		<div class="mod-head">
			<h3>随机文章</h3>
		</div>
		<div class="mod-body">
			<ul style="font-size:12px; color:#999;">
				<?php 
				$indx = 1;
				foreach($this->radom_articles AS $key => $val) { ?>
				<li><span class='artLi artLi<?php echo $indx; ?>'><?php echo $indx++; ?></span><a  href="a/<?php echo $val['id']; ?>.html" title="<?php echo $val['title']; ?>"><?php echo  $val['title']; ?></a></li>
				<?php } ?>
			</ul>
		</div>
	</div>
	<!-- end 相关问题 -->
	<?php } ?>
最后就可以啦。 参考效果如:http://onijiang.com/的右侧栏  

发布于 2015-04-25 21:50

免责声明:

本文由 sl514 原创发布于 WeCenter ,著作权归作者所有。

登录一下,更多精彩内容等你发现,贡献精彩回答,参与评论互动

登录! 还没有账号?去注册

net
2017-06-09 15:55
/models/article.php 这一步换成这样应该就可以了 public function get_radom_article(){ $table1 = $this->get_table('article') ; $sql = 'SELECT t1.* FROM `'.$table1 .'` AS t1 JOIN (SELECT ROUND(RAND() * ((SELECT MAX(id) FROM `'.$table1 .'`)-(SELECT MIN(id) FROM `'.$table1 .'`))+(SELECT MIN(id) FROM `'.$table1 .'`)) AS id) AS t2 WHERE t1.id >= t2.id ORDER BY t1.id LIMIT 10';//效率比order by radom高 return $this->query_all($sql); }
lau360
2017-04-13 10:02
试过了可以,楼主最后一段的代码加入到相关页面的模板文件即可
青鳥
2016-09-22 10:25
实现不了啊
芋头
2015-07-18 18:57
模仿:】 /**自定义问题随机**/ public function get_radom_question() { $table1 = $this->get_table('question') ; $sql = 'SELECT * FROM `'.$table1 .'` AS t1 JOIN (SELECT ROUND(RAND() * ((SELECT MAX(question_id) FROM `'.$table1.'`)-(SELECT MIN(question_id) FROM `'.$table1 .'`))+(SELECT MIN(question_id) FROM `'.$table1 .'`)) AS question_id) AS t2 WHERE t1.question_id >= t2.question_id ORDER BY t1.question_id LIMIT 10'; return $this->query_all($sql); }
seosns
2015-04-26 10:02
三叔吊炸天了