views:

192

answers:

3

Hi... I have a form that searches a mysql db for whatever the user is after, and returns the results on the same page in a div (using ajax then php to search mysql)...

The results are ads, as cars for example, and I want each ad to have a unique image associated with it so that it displays the images next to the ads...

Storing images in BLOBS arent really a good solution I have heard...

How would I do this the best way?

Thanx

+3  A: 

A fast way will be to store images in a folder giving unique filenames in folder or separate folders if you want to put images of different categories in separate places. After searching the available ads, read associated unique file-names and server to client.

Suppose you save images related to car ads in folder /images/cars/. User searches for Audi S6 and the result returns 5 ads.

Now we will proceed based on the naming of the image files. If you give file related to each ad unique name and put that name into record for that ad then simply get that name and create image URL as follows:

/images/cars/ + result_row('image_name')

If you are naming images based on id of ad record then use this scheme:

/images/cars/ + result_row('id')

If you mean that at first you were sending image bytes in response, then you don't need to do that. simply send the path constructed and use it in src property of img tag.

PS:- My PHP skill are not very good now!

TheVillageIdiot
+1: This is a very common method, and will save you a lot of headache compared to cramming the files into a database. I maintain a huge Sybase database that stores image data, and while its the right way to do it for our application, it's a real pain in the butt to maintain...Assuming you don't need a lot of revision tracking and such, a directory is the way to go.
Satanicpuppy
We (myself and a colleague) committed this folly of putting images into DB. Though it is a desktop app, it just kill it.
TheVillageIdiot
How is this done then if the image is already stored and a user searches and submits the form... PHP searches mysql - mysql returns results - then what? depending on results get the right image from server??? thanks
Camran
A: 

Each unique ad will have a unique identifier in the database. You could create image subdirectories for a given set of images and save the images based on their unique identifier.

i.e. advertisement 506 could be stored in /img/506.jpg

Ultimately you would query the database for the advertisement and then you would assume you are loading the image with filename identifier + '.jpg'

cballou
+3  A: 

Typically, you would want to store image records in the database as details about the image and then a file path to the actual image. Then, store the images in the regular file system.

As a side best-practice, you typically will want to store the path relative to some common root, then you can append the file root so the store of images can be moved around. ie> store ads/cars/1005.jpg and then append a root of 'C:/myApp/images/'

Mike Clark