Forum Moderators: coopster
I would like to insert image1, image2, image3, and image4 (jpegs) into a database as a single item. From there, I want one page of my site to display only image1 as a preview of the entire item shown on another page of my site.
Is this possible? I'm not even sure if this is the right forum for this question. I apologize in advance if it's not.
Thank you for your help.
Although it's possible to store binary data in a database, but it's not a very popular thing to do.
However if you are sure you want to do that, then I would get the whole record:
if($select_image!= (int)$_GET['image']) $select_image = 1;
//you can also check if it doesn't exceed the amount of pictures
SELECT images from images WHERE id=1
...
$record = mysql_fetch_assoc(...
$begin = array();
while ($begin[] = strpos("JFIF", $record['images']!== false) continue;
$begin[] = strlen($record['images']) + 6;
$image = substr($record['images'], $begin[$select_image - 1] - 6, $begin[$select_image] - 6);
then write appropriate headers and echo the $image
The + 6 and - 6 are, because the jpg file doesn't start with JFIF, but 6 characters earlier (I may be here wrong by one in any direction - it may be 5 or 7, am not sure.
Just remember to create a special images.php for that and call the image with
<img src="images.php?image=1"
Hope this helps
Michal
PS. The best method however is to store the image as files somewhere and in the db just their names
You can store not the original name, but eg. hash of a random.
I'm not saving the binaries into my database -- they're actually saved at an image-hosting site so that it wouldn't take up the space of my domain.
Would your suggestion still apply?
I didn't mean for you to go through all that trouble without clarification.
Sorry.
My item (www.example.com/viewArticle.php?id=56) would have these multiple images displayed at once.
On my main site (www.example.com), however, I would like only the first image to be shown as a preview of that item (id=56).
I hope this helps.
Thanks again so much for your help.
productId ¦ productName ¦ image1 ¦ image2 ¦ image3 ¦ image4
1 ¦ my thing ¦ 1.jpg ¦ 2.jpg ¦ 3.jpg ¦ 4.jpg
Then, in a more detailed page:
$sql = "select * from products where productId =".$id;
To display the image
<img src="path/to/image/hosting/<? echo $image;?>">
That, to me, seems to do what you want, no?