Jump to content

Help with an image script


DBookatay

Recommended Posts

I have 23 products in a db, all with a unique ID that Im creating a user site for. Some of the items have 1 or 2 images and others have as many as 30 images.

In my db I have a field called "pics". All the images are saved as a .jpg, according to their id on the server. So if item 1 has 10 images the files are: "1_001.jpg," "1_002.jpg," ect.

 

How do I create a script that will display the correct amount of images based on the number in the db.

I was going to do something like this:

if ($row['pics'] =="1") {
$pictures = '<img src="pictures/'.$id.'_001.jpg" width="200" alt="" />';
} elseif ($row['pics'] =="2") {
$pictures = '<img src="pictures/'.$id.'_001.jpg" width="200" alt="" /><img src="pictures/'.$id.'_002.jpg" width="200" alt="" />';
} elseif ($row['pics'] =="3") {
$pictures = '<img src="pictures/'.$id.'_001.jpg" width="200" alt="" /><img src="pictures/'.$id.'_002.jpg" width="200" alt="" /><img src="pictures/'.$id.'_003.jpg" width="200" alt="" />';
}

but that would just be retarded...

 

Thank you!

Link to comment
https://forums.phpfreaks.com/topic/253919-help-with-an-image-script/
Share on other sites

If this don't solve your problem at lest it should point you in the right direction. Tell me how it works out for you OK?

<?php

$pictures = '';
$id = '7';  // <====  This is your product id
$pnum = 14;    // <==== This is the query returned number of pictures per the product

for($i = 1; $i < $pnum+1; $i++)
{
$picnum = $id.'_'.substr('000' . $i, -3).'.jpg';
$pictures .= '<img src="pictures/'.$picnum.'" width="200" alt="" />';
}
echo $pictures;  // <=== This shoud be your pictures

?>

in your table, I assume you have a column that tells which product the image belongs to; if not, add one.

 

 

$product_id = (int)$_GET['id'];
$sql = mysql_query("select * total from images where product_id = $product_id");
while($row = mysql_fetch_assoc($sql)){
echo <<<OPT
<a href="/images/{$row["pics"]}"><img src="/images/{$row["pics"]}" /></a>
OPT;
}

A bigger question would be, how do you want to display/layout these multiple images on your page?

 

Getting an array of the matching files would be a simple glob statement - $files = glob("pictures/{$id}_*.jpg"); You would then just iterate over the array of files and output the corresponding <img ...> tags.

If this don't solve your problem at lest it should point you in the right direction. Tell me how it works out for you OK?

<?php

$pictures = '';
$id = '7';  // <====  This is your product id
$pnum = 14;    // <==== This is the query returned number of pictures per the product

for($i = 1; $i < $pnum+1; $i++)
{
$picnum = $id.'_'.substr('000' . $i, -3).'.jpg';
$pictures .= '<img src="pictures/'.$picnum.'" width="200" alt="" />';
}
echo $pictures;  // <=== This shoud be your pictures

?>

 

This did the job, thanks!

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.