随机文章

随机文章

wecenter随机文章的实现

sl514 发表了文章 • 5 个评论 • 1064 次浏览 • 2015-04-25 21:50 • 来自相关话题

首先在/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/的右侧栏
  查看全部
首先在/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/的右侧栏
 

wecenter随机文章的实现

sl514 发表了文章 • 5 个评论 • 1064 次浏览 • 2015-04-25 21:50 • 来自相关话题

首先在/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/的右侧栏
  查看全部
首先在/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/的右侧栏