蒸五花肉怎么做好吃:ASP 排行榜 问题

来源:百度文库 编辑:高校问答 时间:2024/04/28 16:43:41
<%
set rs1=server.createobject("adodb.recordset")
sql="SELECT top 10 * from ID order by hits desc"
rs1.open sql,conn,1,1
%>
<tr>
<td width="100%" align="center" background="images/menu.gif" height="20" bgcolor="#008CC8"><span id="glowtext">点击排行</span></td>
</tr>
<%
if not rs1.eof then
do while not rs1.eof
%>
<tr>
<%=rs1("who")%>
</tr>
<%
rs1.movenext
loop
else
response.write "<td align='center'>暂无资料</td>"
end if
rs1.close
%>
这样写它把所有数据都输出来了,
然后我把do while not rs1.eof
替换为
i=0
do while i < 10
i=i+1
这样可以输出10条了 可是在少于10条的时候就会出错,我应该怎么才能在少于10条的时候正常输出呢,而在多于10条的时候只列出10条呢?

举个例子:
比如数据库中的记录点击数字段的数据是这样的
1,2,3,5,6,6,6,6,9,10,12
这时如果"select top 5"的话实际上会输出
1,2,3,5,6,6,6,6
这样你应该就明白了为什么会输出超过10条了吧

<%
set rs1=server.createobject("adodb.recordset")
sql="SELECT top 10 * from ID order by hits desc"
rs1.open sql,conn,1,1
%>
<tr>
<td width="100%" align="center" background="images/menu.gif" height="20" bgcolor="#008CC8"><span id="glowtext">点击排行</span></td>
</tr>
<%
if not rs1.eof then
do while not rs1.eof or i<10
%>
<tr>
<%=rs1("who")%>
</tr>
<%
rs1.movenext
i=i+1
loop
else
response.write "<td align='center'>暂无资料</td>"
end if
rs1.close
%>

把你的
i=0
do while i < 10
i=i+1
替换为
i=0
do while (not rs1.eof) and (i < 10)
i=i+1

即可呵呵.
另外,SQL的TOP问题,为什么会全部显示的原因,是因为你没有对主键建立索引或者根本没有主键导致的,设置正确的主键试试看~,希望对LZ有帮助.

把你的
i=0
do while i < 10
i=i+1
替换为
i=0
do while (not rs1.eof) and (i < 10)
i=i+1

即可呵呵.
另外,SQL的TOP问题,为什么会全部显示的原因,是因为你没有对主键建立索引或者根本没有主键导致的,设置正确的主键试试看~,希望对LZ有帮助.