六月 4th, 2007为LBS^2添加日志的Top 10阅读排行
要为自己的blog添加阅读排行的功能,只需要修改cache.asp和global.asp,做下面几件事:
在Cache.asp中:
1. 在lbsCache类中,添加一个hotArticles的数组属性,简单起见,你可以在this.wordFilter = new Array(); 下面一行添加下面的代码:
this.hotArticles = new Array();
2. 在load 方法的最后,添加下面的代码(this.loaded = true;)下边添加:
this.loadHotArticles();
3. 在loadFromDB方法中加入下面一行代码:
this.loadHotArticles();
4. 现在,需要添加loadHotArticles和fillHotArticles两个方法来实现从数据库中获取排行信息的功能了。比如,你可以在loadUserGroup方法上面一行,添加下面的代码:
// Load hotArticles —————————-
this.loadHotArticles = function(){
this.hotArticles = new Array();
//this.saveToCache(lbsNamespace+”hotArticles”,”none”);
// Don’t change the order of the fields in SQL string
var tmpA=connBlog.query(“SELECT TOP 10 log_id, log_title, log_viewCount FROM [blog_Article] ORDER BY log_viewCount DESC”,undefined,undefined,true);
if(tmpA!=null){
if(tmpA.ubound(2)>-1){
//this.saveToCache(lbsNamespace+”hotArticles”,tmpA);
this.fillHotArticles(tmpA);
}
}
delete tmpA;
}// Fill hotArticles —————————-
this.fillHotArticles = function(arr){
if(arr==”none”) return;
var count=arr.ubound(2);
for(var i=0;i<=count && i<10;i++){
this.hotArticles[i]={ ”id”: arr.getItem(0,i),
“title”: arr.getItem(1,i),
“count”: arr.getItem(2,i)
};
}
}
其中i<10中显示的最多显示几页!
按上面的步骤修改完Cache.asp之后,我们需要的获取阅读排行的功能就有了,我们只需要在global.asp中引用这个排行数组,并将结果显示在Sidebar里就行了。你将下面的代码添加在panelSearch的下面:
<div id=”panelHotArts” class=”panel”>
<h5>阅读排行</h5>
<div class=”panel-content”>
<ul>
<%for(var i=0;i<theCache.hotArticles.length;i++){
with(theCache.hotArticles[i]){
%>
<li><a href=”article.asp?id=<%=encodeURIComponent(id)%>”><%=func.HTMLEncodeLite(title)%></a> [<%=count%>]</li>
<%
}
}%>
</ul>
</div>
</div>
至此,就完成了添加阅读排行所需的所有工作,可以刷新首页,看看效果了。
Leave a Reply
You must be logged in to post a comment.