Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
Home
Discussion GroupsGeneralPHPASPPerlColdFusionFlashHTML, CSS, ScriptsBrowsers

Webmaster Forum / ASP / Database Access / August 2008



Tip: Looking for answers? Try searching our database.

Display Records in certain order (string contained in field)

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Randy - 02 Aug 2008 17:28 GMT
Just can't figure out how to do this..........
I have a simple asp page that desplays images from database.

The query on my page:
-------------------------
imagequery = "SELECT * FROM FILE_LIST WHERE CLASS_ID=" & FORM_ID
------------------------
The table FILE_LIST contains a field called TITLE which never gets
displayed.
(the form_id comes from a selection on previous page)
I display the images with a do loop as follows:
---------------------------
<%
set rstemp=conntemp.execute(imagequery)
If rstemp.EOF Then
Form_file_ID = 0
Else
do while not rstemp.eof
%>
<CENTER><IMG SRC="imagehelper.asp?FILE_id=<% = rstemp("FILE_id")
%>"></CENTER>
<%
rstemp.movenext
loop
END IF
rstemp.close
set rstemp=nothing
%>
--------------------------
I want to have the images that have the string 'photo' within the  'TITLE'
field disply FIRST
(eg. one with the title 'A photo of a cat' would desplay before 'A drawing
of a Cat' )
and THEN the ones that don'tt.   I think I can accomplish this some kind of
if statement in the loop,
but I just cant get it to work without the ones containing  'photo'
displaying twice.
I hope I've explained this adequately.
Thanks in advance to anyone who has any ideas.
Bob Barrows [MVP] - 02 Aug 2008 18:24 GMT
> Just can't figure out how to do this..........
> I have a simple asp page that desplays images from database.

What database? Type and version please ... these are almost always relevant
and they are definitely relevant for this question.

> The query on my page:
> -------------------------
> imagequery = "SELECT * FROM FILE_LIST WHERE CLASS_ID=" & FORM_ID

Nothing to do with your problem but ...
http://www.aspfaq.com/show.asp?id=2096

> ------------------------
> The table FILE_LIST contains a field called TITLE which never gets
[quoted text clipped - 4 lines]
> I hope I've explained this adequately.
> Thanks in advance to anyone who has any ideas.

I cannot get specific, but the sql dialect for whatever database you are
using undoubtedly has string functions to enable you to accomplish this in
your sql statement. Like this:

Access:
imagequery = "SELECT * FROM FILE_LIST " & _
"WHERE CLASS_ID=" & FORM_ID & _
" ORDER BY iif(Instr([TITLE],"photo")>0,0,1) ASC

SQL Server:
imagequery = "SELECT * FROM FILE_LIST " & _
"WHERE CLASS_ID=" & FORM_ID & _
" ORDER BY CASE WHEN TITLE Like '%photo%' " & _
"THEN 0 ELSE 1 END ASC"

Signature

Microsoft MVP - ASP/ASP.NET
Please reply to the newsgroup. This email account is my spam trap so I
don't check it very often. If you must reply off-line, then remove the
"NO SPAM"

Randy - 02 Aug 2008 21:15 GMT
Thanks very much.  Yes I should have specified which database type.  As it
turns out I just switched from Access to SQL Server, and this query works
perfectly.
Also looked at the article you quoted. I'm in the process now of getting rid
of all those SELECT *'s.

>> Just can't figure out how to do this..........
>> I have a simple asp page that desplays images from database.
[quoted text clipped - 32 lines]
> " ORDER BY CASE WHEN TITLE Like '%photo%' " & _
> "THEN 0 ELSE 1 END ASC"
Bob Barrows [MVP] - 02 Aug 2008 21:59 GMT
> Thanks very much.  Yes I should have specified which database type. As it
> turns out I just switched from Access to SQL Server, and this
> query works perfectly.
> Also looked at the article you quoted. I'm in the process now of
> getting rid of all those SELECT *'s.

Excellent. While you're at it, you might want to check this out:

Further points to consider:
Your use of dynamic sql is leaving you vulnerable to hackers using sql
injection:
http://mvp.unixwiz.net/techtips/sql-injection.html
http://www.sqlsecurity.com/DesktopDefault.aspx?tabid=23

See here for a better, more secure way to execute your queries by using
parameter markers:
http://groups-beta.google.com/group/microsoft.public.inetserver.asp.db/msg/72e36
562fee7804e


Personally, I prefer using stored procedures:
http://groups.google.com/group/microsoft.public.inetserver.asp.general/msg/5d3c9
d4409dc1701?hl=en


Signature

Microsoft MVP - ASP/ASP.NET
Please reply to the newsgroup. This email account is my spam trap so I
don't check it very often. If you must reply off-line, then remove the
"NO SPAM"

 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2009 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.