Forum Moderators: coopster

Message Too Old, No Replies

Can I Show a Portion of an Item from a DB?

         

galileo5

3:59 pm on Oct 29, 2006 (gmt 0)

10+ Year Member



Good morning.

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.

mcibor

4:24 pm on Oct 29, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I strongly suggest not to do that.

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.

galileo5

6:10 pm on Oct 29, 2006 (gmt 0)

10+ Year Member



I apologize for the lack of clarity.

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.

galileo5

6:21 pm on Oct 29, 2006 (gmt 0)

10+ Year Member



Just to be even more clear:

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.

mcibor

10:27 pm on Oct 30, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



You would have to catch it into a variable.
Maybe fopen will do, but I'm not sure.

Then parse the file with the function I wrote, next send headers and voile!

Should work
Michal

Steerpike

11:40 pm on Oct 30, 2006 (gmt 0)

10+ Year Member



You should have a table similar to this:

productId ¦ productName ¦ image1 ¦ image2 ¦ image3 ¦ image4
1 ¦ my thing ¦ 1.jpg ¦ 2.jpg ¦ 3.jpg ¦ 4.jpg

Then, in your 'preview page'
$sql = "select image1 from products where productId =".$id;

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?