Click to See Complete Forum and Search --> : Images By Catory (Help Plz)


kwadman
09-08-2005, 04:20 PM
Hi, What i'm trying to do is display images by Catory

My database is a like the one below:

-------------------------------------
- Title - Image URL - Catory -
-------------------------------------
- Image1 - /i/1.jpg - Cat1 -
- Image2 - /i/2.jpg - Cat1 -
- Image4 - /i/3.jpg - Cat2 -
- Image5 - /i/4.jpg - Cat2 -
-------------------------------------

Now what i get on the output of my ASP is

Click me for Cat1
Click me for Cat1
Click me for Cat2
Click me for Cat2

You see all i want is one record of Cat1 and one of Cat2

I think i need to have a IF statement or somthing that says somthing like, If Cat1 is
already displayed then move to the next record and don't print Catory name until EOF.

Thats where i get stuck !!!

<<<<<<<<<<<< MY ASP CODE SO FAR IS >>>>>>>>>>>>>>>
<!--#include Virtual="/Style/Header.asp" -->
<center><table width="1" Height="50">
<%
set adorecordset = server.createobject("ADODB.Recordset")

sqlstring = "SELECT * FROM gallery"

adorecordset.open sqlstring,adoconnection
adorecordset.movefirst

while not adorecordset.eof
%>
<tr>
[<a href= "/Gallery.asp?cat=<%=adorecordset("cat")%>"><%=adorecordset("cat")%></a>]
</td>
</tr>

<%
adorecordset.movenext
wend
set adorecordset = nothing
%>
</table>
</center>

<!--#include Virtual="/Style/Footer.asp" -->

silverbullet24
09-08-2005, 05:01 PM
try

sqlstring = "SELECT DISTINCT * FROM gallery"

kwadman
09-08-2005, 05:06 PM
Sorry didn't make any diffrence, I did just past it in. (Is that what i was ment to do?)

Bullschmidt
09-08-2005, 10:29 PM
Perhaps this may hopefully give you some ideas:

Classic ASP Design Tips - Grouping Data
http://www.bullschmidt.com/devtip-groupdata.asp

Here is a way to group data so that it looks something like this:

Crafts:
Andrews, Beth
Thomas, Jean

Meals:
Johnson, Sally
Martin, Cindy

Sports:
Smith, Tom
Jones, George

Within the recordset looping compare the field you want to group on such as Category with the Category of the previous record (perhaps stored in a variable called CategoryPrev) and only if it's different then do something like show a new group header.

<%
' Set Category for this record.
Category = objRS("Category")

' Show new group header if needed.
If Category <> CategoryPrev Then
%>
<tr>
<td colspan="20">
<br>
<b><%= Category %>:</b>
</td>
</tr>
<%
End If

' Set CategoryPrev here to be Category for use next time thru the loop.
CategoryPrev = Category
%>

And the above assumes that things are sorted correctly in the SQL statement with perhaps something like ORDER BY Category, LName, FName.

And of course one could get even more advanced using the same concept to create multiple levels of grouping.

kwadman
09-10-2005, 07:45 AM
Thanks for the code although i got the DISTINCT working..
I'm going to add this post to help others witht the same issue

The DISTINCT needed to be like this:
sqlstring = "SELECT DISTINCT cat FROM gallery Order By cat"

My code for the page is as follows:
You may find a non relevant IF Statement but it's because i used the code from my gallery.asp and editted it quickly (Basicly all the Table stuff can go if your doing your own.

Anyway the code...
<<<<<<<<<<<<<<< PAGE CODE START >>>>>>>>>>>>>>>>>>>>>
<!--#include Virtual="/Style/Header.asp" -->

<td><table width="481" border="0" cellspacing="0" cellpadding="0">
<tr>
<td width="25"><img src="images/spacer.gif" width="25" height="1"></td>
<td width="130"><img src="images/spacer.gif" width="130" height="1"></td>
<td width="326"><img src="images/spacer.gif" width="135" height="1"></td>
</tr>
<tr>
<td>&nbsp;</td>
<td colspan="2"><img src="images/inside01.jpg" width="350" height="67"></td>
</tr>
<tr>
<td>&nbsp;</td>
<td colspan="2"><div class="align-justify" id="main">
<table width="100%" border="0" cellspacing="0" cellpadding="0">
<tr>

<td width="71%" class="align-justify"><p class="text3"><strong>category List</div></strong></p>
<p><div>Take a look at my Pictures...</div></p></td>
</tr>
</table>
<center>
<table width="1" Height="50">
<%
set adorecordset = server.createobject("ADODB.Recordset")

sqlstring = "SELECT DISTINCT cat FROM gallery Order By cat"

adorecordset.open sqlstring,adoconnection
adorecordset.movefirst


rowcount = 1
while not adorecordset.eof

%>
<tr>

<%
for i = 1 to 4
%>
<td align="center" valign="top" width="93%">
<%
if rowcount <= 1 then
%><img border="0" src="/images/spacer.gif" width="100" height="1">

<%
end if
%>

[<a href="/Gallery.asp?cat=<%=adorecordset("cat")%>"><%=adorecordset("cat")%></a>]

<%
adorecordset.movenext
if adorecordset.eof then exit for
%>

</td>


<%
next
%>

</tr>

<%
rowcount = rowcount + 1
Wend
%>
</table>
</center>
<%
set adorecordset = nothing
%><!--#include Virtual="/Style/Footer.asp" -->
<<<<<<<<<<<<<<< END OF CODE >>>>>>>>>>>>>>>>>>>>>>>>>>

You can view the page at www.ibuddy.co.uk
You can message me from there for help etc...

I don't mind packaging up my site (FULL Template with ASP + Database) if anyone wants it...

just contact me
kyle.wadman@ibuddy.co.uk