PDA

View Full Version : do until first instance?


Crash1hd
08-16-2003, 05:49 AM
I was wondering how would I reright the following code so that it would only show the first instance acording to the url

<td width="90%" Class=Center>
<%do Until GBRS.Eof%>
<%If GBRS.Fields("MphotoID") = request.querystring("MphotoID") Then%>
<img src="Pictures/<%response.write GBRS.fields("pictURL")%>" border="0" name="bigPhoto" height="361">
<%End if%>
<%GBRS.movenext%><%loop%>
</td>

in the url is MphotoID=1 in one url and the other is MphotoID=2

and in the database has a field called MphotoID and in there each line of info has a different number some are 1 and some are 2 and it works great for this code

<%do until GRS.EOF%>
<%If GRS.Fields("MphotoID") = request.querystring("MphotoID") Then%>
document.write("<a onmouseover='window.status=\"Click to view image\"; return true' onmouseout='window.status=\"\"; return true' href='javascript: showupdate(<%response.write GRS.fields("ID")%>)'><img src='http://www.alwaysremember.ca/Memorials/Pictures/<%response.write GRS.fields("pictURL")%>' height='80' width='<%response.write (GRS.fields("PicWidth")/(GRS.fields("PicHeight")/80))%>' border='0'></a>&nbsp;")
<%End if%>
<%GRS.movenext%>
<%loop%>

as I need it to show all but the first code I only want it to show the first instance of that number

and yes its inside of a script thats just the part of the code needed to explain the problem! :)

raf
08-16-2003, 09:07 PM
If i understand it correct, the records are selected from a db. Can you sart them by MphotoID ?

Cause then you could build in a check inside your loop. Like

<%
dim prev
prev = 999999999999999
do until GRS.EOF
if GRS.Fields("MphotoID") <> prev then
your code for the limage etc
prev = GRS.Fields("MphotoID")
end if
GRS.movenext
loop
%>


So each time the record has a new image linkt to it, the imagecode will be processed. If the next record has the same image, it will ga straight to the nex record.

PS : No need to open and close ASP-mode for each line or inserted value. Just concatinating us fine. Like

response.write("<img src=""Pictures/" & GBRS.fields("pictURL") & """ border=""0"" ...")

Crash1hd
08-16-2003, 10:48 PM
Ok nope thats not doing it!

I only want it to loop the first instance in the database that equals the number in the URL I guess the bigest problem is that the data thats in the MphotoID is going to be different numbers and will vary in order it may go 1 2 9 4 3 2 6 2 5 3 or any number of order so that is not a choice what I am looking for is it to only show the data that referes to the appropriate number so say its 2 it would show 3 images in the above exampe but if it was 1 it would only show 1 image however I only want the first image of each set to show not the rest!

:)

Crash1hd
08-16-2003, 11:27 PM
OK I solved it with the following code notice the difference

<%Do until GBRS.EOF%>
<%If GBRS.Fields("MphotoID") = request.querystring("MphotoID") Then%>
<%If GBRS.Fields("ID") = "1" Then%>
<img src="Pictures/<%response.write GBRS.fields("pictURL")%>" border="0" name="bigPhoto" height="361">
<%End IF%>
<%End IF%>
<%GBRS.movenext%>
<%loop%>

I added a If statment as I realized that as I add each one I need to organize them within themselves or it just wouldnt work

raf
08-17-2003, 10:27 AM
I don't get it.

With your code, you only display the images from records with ID = 1. So why don't you then ad it as a condition is your select ?

select yourvariables from yourtable where ID = 1

whammy
08-18-2003, 02:10 AM
I agree with raf. You should be using that in the SQL Query - that way you get the record you want.

BUT - before just using

select yourvariables from yourtable where ID = 1

You need to use some function (such as my ExtractNumbers() function) to help prevent against SQL Injection attacks... like

"SELECT yourvariables FROM yourtable WHERE ID = " & ExtractNumbers(ID)

Also, this brings up another issue. If there's personal data that will be displayed on the page, you also need to use another condition in the query... like

"SELECT yourvariables FROM yourtable WHERE ID = " & ExtractNumbers(ID) & " AND LastName = '" & ExtractWordChars(LastName) & "'"

... otherwise someone can just type in random numbers in the querystring, and possibly bring up other users' personal information!!! NOT good.

For those functions see:

http://www.solidscripts.com/downloads/functions.txt

Also, you said that there may be duplicates for this number. In that case you'd want to use:

"SELECT Max(PhotoID) AS YourVariable FROM YourTable WHERE PhotoID = " & ExtractNumbers(PhotoID) & " AND LastName = '" & ExtractWordChars(LastName) & "'"

Or something to that effect.