Repeat results again - loop?

Hi guys

I hope this is possible. Let’s say I have the following code on my page:

<%
  sqlStr = "SELECT top 6 imageurl from mytable where orderid = '123456' GROUP BY carimage"

  oRs.Open sqlStr, oDBConn 
  If oRs.eof then
%>
No images
<%
  Else

  Do while not oRs.eof
  %>

<img src="<%=oRs("imageurl")%>"><br>

<%
  oRs.MoveNext
  loop 

  End If
%>

If I have 6 records in my table which have an orderid value of “123456” then 6 images will appear on my page like so

<#img src=“http://www.mywebsite.co.uk/image1.jpg”>
<#img src=“http://www.mywebsite.co.uk/image2.jpg”>
<#img src=“http://www.mywebsite.co.uk/image3.jpg”>
<#img src=“http://www.mywebsite.co.uk/image4.jpg”>
<#img src=“http://www.mywebsite.co.uk/image5.jpg”>
<#img src=“http://www.mywebsite.co.uk/image6.jpg”>

…which is fine. However, if I only have 4 records then only 4 images will appear on my page. What I would love is that IF there is less then 6 images then the results loop round again making up the 6. So, for example, if there are only 4 records then the first 2 images of that 4 will appear again which makes up the 6. So in this example my page would look like this:

<#img src=“http://www.mywebsite.co.uk/image1.jpg”>
<#img src=“http://www.mywebsite.co.uk/image2.jpg”>
<#img src=“http://www.mywebsite.co.uk/image3.jpg”>
<#img src=“http://www.mywebsite.co.uk/image4.jpg”>
<#img src=“http://www.mywebsite.co.uk/image1.jpg”>
<#img src=“http://www.mywebsite.co.uk/image2.jpg”>

I’m hoping my explanation makes sense? If so, how would I do this? Any help would be fully appreciated

Best regards

Rod from the UK

This is a stab in the dark as I haven’t done VBScript in ages. But maybe something like so

<%
  sqlStr = "SELECT top 6 imageurl from mytable where orderid = '123456' GROUP BY carimage"
  oRs.Open sqlStr, oDBConn 
  If oRs.eof then
%>
No images
<%
  Else
      counter = 1
      Do while counter <= 6
  %>
<img src="<%=oRs("imageurl")%>"><br>
<%
          counter = counter + 1
          oRs.MoveNext     
          If oRs.eof then
              oRs.MoveFirst
          End If
      loop 
  End If
%>

Hi cpradio

Absolutely spot on!

Thank you so much, really appreciate your help!

Best regards

Rod from the UK

1 Like

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.