1

This is an architecture question, but its solution lies in ColdFusion and MySQL structure--or at least I believe so.

I have a products table in my database, and each product can have any number of screen-shots. My current method to display product screen-shots is the following:

I have a single folder where all screen-shots associated with all products are contained. All screen-shots are named exactly the same as their productID in the database, plus a prefix.

For example: Screen-shots of a product whose productID is 15 are found in the folder images, with the name 15_screen1.jpg, 15_screen2.jpg, etc...

In my ColdFusion page I have hard-coded the image path into the HTML (images/); the image name is broken into two parts; part one is dynamically generated using the productID from the query; and part two is a prefix, and is hard-coded. For example:

<img src"/images/#QueryName.productID#_screen1.jpg">
<img src"/images/#QueryName.productID#_screen2.jpg"> etc...

This method works, but it has several limitations the biggest listed bellow:

  1. I have to hard-code the exact number of screen-shots in my HTML template. This means the number of screen shots I can display will always be the same. This does not work if one product has 10 screen shots, and another has 5.
  2. I have to hard-code image prefixes into my HTML. For example, I can have up to five types of screen-shots associated with one product: productID=15 may have 15_screen1.jpg, 15_screen2.jpg, and 15_FrontCover.jpg, 15_BackCover.jpg, and 15_Backthumb.jpg, etc...

I thought about creating a paths column in my products table, but that meant creating several hundreds of folders for each product, something that also does not seem efficient.

Does anyone have any suggestions or ideas on the correct method to approach this problem?

Many thanks!

4

3 回答 3

2

怎么样...

使用一个图像表,一个产品到多个图像(带有可选的 sortOrder 列?),并使用 imageID 作为 jpeg 文件名?

更新:

有一个 ImageClass 表,多个 Image 到一个 ImageClass。

Image
-----
ID
productID
imageClassID (FK to ImageClass)

使用后端业务逻辑来强制某些类只能有一个图像。

或者...如果您真的想强制某些类只能使用一个图像,那么可以进行更复杂的设计:

Product
------
ID
name
...
frontCoverImageID
backCoverImageID
frontThumbImageID
backThumbImageID

Image
-----
ID
productID
isScreenShot (bit)    // optional, but easier to query later...

但是,我更喜欢第一个,因为您以后可以拥有许多您认为合适的类,而无需重构数据库。

于 2010-02-19T19:51:21.167 回答
1

保留有关数据库中有多少和哪些图像的信息绝对是要走的路。

除此之外,如果您想使用命名约定将图像与产品相关联,并且图像的数量是任意的,那么为每个产品创建一个文件夹可能是一个更好的主意:

/images/products/{SKU1}/frontview.jpg
/images/products/{SKU1}/sideview.jpg
/images/products/{SKU2}/frontview.jpg

等等。然后用于<cfdirectory>收集给定产品的图像。您可能还想为图像命名00_frontview.jpg01_sideview.jpg这样您就可以对它们在页面上显示的顺序进行排序和控制。

于 2010-02-19T20:31:01.923 回答
1

使用cfdirectory标签检查文件系统:

<!--- get a query resultset of images in filesystem --->
<cfdirectory action="list" name="images" directory="images">

<!--- get images for specific product --->
<cfquery name="productImages" dbtype="query">
select *
from images
where name like '#productid#%'
</cfquery>

<cfoutput query="productImages">
    <img src="#productimages.directory#/#productimages.name#" />
</cfoutput>

您甚至可以尝试使用filtercfdirectory 的属性来尝试省略 QoQ

于 2010-02-19T23:19:08.250 回答