wecenter随机文章的实现

首先在/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 } ?>
 
最后就可以啦。

1 个评论

能否再分享一个随机问题的实现方法?

要回复文章请先登录注册