DBookatay Posted December 28, 2011 Share Posted December 28, 2011 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! Quote Link to comment https://forums.phpfreaks.com/topic/253919-help-with-an-image-script/ Share on other sites More sharing options...
sunfighter Posted December 28, 2011 Share Posted December 28, 2011 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 ?> Quote Link to comment https://forums.phpfreaks.com/topic/253919-help-with-an-image-script/#findComment-1301890 Share on other sites More sharing options...
The Little Guy Posted December 28, 2011 Share Posted December 28, 2011 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; } Quote Link to comment https://forums.phpfreaks.com/topic/253919-help-with-an-image-script/#findComment-1301907 Share on other sites More sharing options...
PFMaBiSmAd Posted December 28, 2011 Share Posted December 28, 2011 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. Quote Link to comment https://forums.phpfreaks.com/topic/253919-help-with-an-image-script/#findComment-1301908 Share on other sites More sharing options...
DBookatay Posted December 28, 2011 Author Share Posted December 28, 2011 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! Quote Link to comment https://forums.phpfreaks.com/topic/253919-help-with-an-image-script/#findComment-1302008 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.