Home > ASP Development > making the output up to 5..

making the output up to 5..



Sorry for the pnats title, didnt know how to describe this problem i am facing.



I have this piece of code




Code:



x=1
cmd.commandtext="select top 5 * from tunes where artist='" & rs("artist") & "' and id <> " & tempid & " and not left(catno,2)='vs'and number_in_stock >0 "

set rs=cmd.execute
do while not rs.eof
if x=1 then response.write("<B>Just a selection of other items by '" & tartist & "'</b><BR><table width=100% cellspacing=0 cellpadding=3>")
response.write ("<tr><TD><li>" & rs("artist") & " - " & rs("title") & " - " & lookupformat(rs("format")) & "</td><Td width=10% ><a href=fulldetails.asp?id=" & rs("id") & "><img src=images/view.gif border=0></a>&nbsp;")

if cint(rs("number_in_stock")) > 0 then
Response.Write "<a href=icart.asp?id=" & rs("catno") & "&function=addmini&price=" & rs("priceout") & " target=""newcart""><img src=images/buy.gif border=0></a>"
else
Response.Write "<a href=reserve.asp?id=" & rs("catno") & "&function=add&price=" & rs("priceout") & "&title=" & server.urlencode(rs("title")) & "&artist=" & server.urlencode(rs("artist")) & "&noinstock=" & server.urlencode(rs("number_in_stock")) & "&description=" & server.urlencode(rs("comments")) & "><img src=images/reserve.gif border=0></a>"
end if


response.write("</td></tr>" & vbcrlf)
rs.movenext
x=x+1
loop
if not x=1 then response.write("</table><br>")






which works fine, it pulls the top 5 from the db where there are matches in stock, what i have been asked is if there are only say 2 in stock is to make it up to 5 by pulling the remainder where there stock is 0.



any help, assistance or the right direction greatly appreciated on this one.



Smalls

    
Guest


this should work with the else statement.



AARRRGGGGHHHHH !!!!



Smalls

Was this answer helpful ? Yes No   
Guest


Quote:
Originally Posted by smallboy
this should work with the else statement.



AARRRGGGGHHHHH !!!!



Smalls




Its because if two records found, it only goes round the loop twice

You need to add an extra loop AFTER the While Wend one




Code:



<%

x=1
cmd.commandtext="select top 5 * from tunes where artist='" & rs("artist") & "' and id <> " & tempid & " and not left(catno,2)='vs'and number_in_stock >0 "

set rs=cmd.execute
do while not rs.eof
if x=1 then response.write("<B>Just a selection of other items by '" & tartist & "'</b><BR><table width=100% cellspacing=0 cellpadding=3>")
response.write ("<tr><TD><li>" & rs("artist") & " - " & rs("title") & " - " & lookupformat(rs("format")) & "</td><Td width=10% ><a href=fulldetails.asp?id=" & rs("id") & "><img src=images/view.gif border=0></a>&nbsp;")

if cint(rs("number_in_stock")) > 0 then
Response.Write "<a href=icart.asp?id=" & rs("catno") & "&function=addmini&price=" & rs("priceout") & " target=""newcart""><img src=images/buy.gif border=0></a>"
else
Response.Write "<a href=reserve.asp?id=" & rs("catno") & "&function=add&price=" & rs("priceout") & "&title=" & server.urlencode(rs("title")) & "&artist=" & server.urlencode(rs("artist")) & "&noinstock=" & server.urlencode(rs("number_in_stock")) & "&description=" & server.urlencode(rs("comments")) & "><img src=images/reserve.gif border=0></a>"
end if


response.write("</td></tr>" & vbcrlf)
rs.movenext
x=x+1
loop

if x < 5 then
for i = x to 5
response.write "<tr><td>"
response.write "YOUR STUFF HERE"
response.write "</td></tr>"
next
end if

if not x=1 then response.write("</table><br>")
%>






Should give you the idea

Was this answer helpful ? Yes No   
Guest


Hi Elijah



I have the code as this




Code:



if x < 5 then
cmd.commandtext="select top 5 * from tunes where artist='" & rs("artist") & "' and id <> " & tempid & " and not left(catno,2)='vs'and number_in_stock =0 "

set rs=cmd.execute
do while not rs.eof

for i = x to 5
response.write "<tr><td>"
Response.Write "<a href=reserve.asp?id=" & rs("catno") & "&function=add&price=" & rs("priceout") & "&title=" & server.urlencode(rs("title")) & "&artist=" & server.urlencode(rs("artist")) & "&noinstock=" & server.urlencode(rs("number_in_stock")) & "&description=" & server.urlencode(rs("comments")) & "><img src=images/reserve.gif border=0></a>"
response.write "</td></tr>"
next
rs.movenext
x=x+1
loop
end if
if not x=1 then response.write("</table><br>")






obviously the for next loop cant exist as it is pulling records from the db, so how can i control then loop so it only goes from 'x' to 5 ??



cheers so far,



Smalls.

Was this answer helpful ? Yes No   
Guest


DO the for loop (from x to 5) AFTER the do loop

Was this answer helpful ? Yes No   
Guest


Quote:
Originally Posted by elijathegold
DO the for loop (from x to 5) AFTER the do loop




sorry m8, i dont follow you on this one, must be having a thickkie day.



Smalls

Was this answer helpful ? Yes No   
Guest


No worries we all do it sometimes... so, heres the logic

Get top 5 records from database
loop through recordset
write stuff to browser
add 1 to count of items produded
end of loop through recordset

if count of items is less than 5
loop from count of items to 5
write stuff to browser
end of loop from count of items
end if

Two distinct loops. 1 to do the records and 1 that adds the extras
if there are < 5 records.

Was this answer helpful ? Yes No   
Guest


how about doing away with the for..next and changeing the select to this..



cmd.commandtext="select top (5-x) * from tunes ...



Smalls

Was this answer helpful ? Yes No   
Guest


Quote:
Originally Posted by smallboy
how about doing away with the for..next and changeing the select to this..



cmd.commandtext="select top (5-x) * from tunes ...



Smalls




I think I have had a Homer moment two...



you could union two sql statements




Code:



select top 5 * from tunes where artist='" & rs("artist") & "' and id <> " & tempid & " and not left(catno,2)='vs' and number_in_stock > 0
union
select top 5 * from tunes where artist='" & rs("artist") & "' and id <> " & tempid & " and not left(catno,2)='vs' and number_in_stock = 0






and then just output the first 5 records in the recordset.



much simpler

Was this answer helpful ? Yes No   
Guest


i changed it as above and commented out the for..next and now get an exception error 80020009



Smalls.

Was this answer helpful ? Yes No   
Guest
 
 
Home - About Infoqu - Contact - Privacy Statement - Link to Infoqu - Bookmark Infoqu

Copyright 2007-2008 by Infoqu. All rights reserved