views:

36

answers:

2

I created a simple news website. I store both videos and images in IMAGES table. Videos added have videos and images added have images stored in a column called ImagesType. Images and Videos attached to a news is stored in ImagesID column of the NEWS table. My problem occurs when I need to display the first image of a news.

i.e.
IMAGES table:

ImagesID ImagesLgURL                        ImagesType  
1        /FLPM/media/videos/0H7T9C0F.flv    videos     
2     /FLPM/media/images/8R5D7M8O.jpg    images  
3        /FLPM/media/images/0E7Q9Z0C.jpg    images  

NEWS table  
NewsID   ImagesID  NewsTitle  
1        1;2;      Street Chic: Paris            ERROR     
2        3;        Paris Runway                  NO ERROR  

The following code give me an error with the 2nd news item because the first ImageID stored in the list is not an image but a video. I need to figure out a way to skip the video item and display the next image.

I hope I made sense.

SQL = "SELECT NEWSID, CATEGORIESID, IMAGESID, NEWSTITLE, NEWSSHORTDESC, NEWSACTIVE, NEWSDATEENTERED"
SQL = SQL & " FROM NEWS N"
SQL = SQL & " WHERE NEWSACTIVE = 1"
SQL = SQL & " ORDER BY NEWSDATEENTERED DESC"
Set objNews = objConn.Execute(SQL)

Do While intLooper1 <= 3 And Not objNews.EOF 

IMAGES =   Split(Left(objNews("IMAGESID"),Len(objNews("IMAGESID"))-1), ";") 

SQL = "SELECT ImagesID, ImagesName, ImagesLgURL, ImagesSmURL, ImagesType"
SQL = SQL & " FROM IMAGES I"
SQL = SQL & " WHERE ImagesID = " & IMAGES(0) & " AND ImagesType = 'images'"
Set objLgImage = objConn.Execute(SQL)

<div>
<a href="?Section=news&SubSection=redirect&NEWSID=<%=objNews("NEWSID")%>">
<img src="<%=objLgImage("ImagesLgURL")%>" alt="<%=objLgImage("ImagesName")%>"  />
</a>
</div>
<%
    objLgImage.Close
    Set objLgImage = Nothing

    intLooper1 = intLooper1 + 1
    objNews.MoveNext 
    Loop
%>
A: 

You really should have a Video table and an Images table. That would be a much better design and it would solve your problem in a much cleaner solution.

fuzzy lollipop
Perhaps, but if many of the columns are the same between the two types it might better to simply rename the table to something like "media".
NobodyMan
+1  A: 

You should have the NewsID in the images Table and put a ForeingKey on it

**NEWS table**
NewsID   NewsTitle  
----------------------------
1        Street Chic: Paris            
2        Paris Runway    

**Images table**
ImageID (PK)  NewsID (FK) ImagesLgURL                        ImagesType  
-------------------------------------------------------------------------
1             1           /FLPM/media/videos/0H7T9C0F.flv    videos     
2             1           /FLPM/media/images/8R5D7M8O.jpg    images  
3             2           /FLPM/media/images/0E7Q9Z0C.jpg    images  

Then you can easily select the first photo of each news

SQL = "SELECT NEWSID, CATEGORIESID, NEWSTITLE, NEWSSHORTDESC, NEWSACTIVE,"     
SQL = SQL & " NEWSDATEENTERED,"
SQL = SQL & " (SELECT TOP 1 ImageID from Images where Images.NewsID = N.NewsID 
                 AND Images.ImageType = 'Images') as ImageID"
SQL = SQL & " FROM NEWS N"
SQL = SQL & " WHERE NEWSACTIVE = 1"
SQL = SQL & " ORDER BY NEWSDATEENTERED DESC"
Set objNews = objConn.Execute(SQL)
Eduardo Molteni