manhunt234 Posted May 7, 2012 Share Posted May 7, 2012 I am trying to display all images located in two sub-folders. Both sub-folders are located in one folder. Here is the code I have right now $columncount = 0; $dynamicList = '<table width="744" border="0" cellpadding="6"><tr>'; while($row = sqlsrv_fetch_array($stmt)){ $id = $row["productID"]; $product_name = $row["product_name"]; $product_price = $row["product_price"]; $dynamicList .= '<td width="135"><a href="product_details.php?productID=' . $id . '"><img src="images/products/Men/' . $id . '.jpg" alt="' . $product_name . '" width="129" height="169" border="0"></a></td> <td width="593" valign="top">' . $product_name . '<br> £' . $product_price . '<br> <a href="product_details.php?productID=' . $id . '">View Product Details</a></td>'; if($columncount == 2){ $dynamicList .= '</tr><tr>'; $columncount = 0; }else $columncount++; } $dynamicList .= '</tr></table>'; echo $dynamicList; As you can see I get all images from "products/Men/". But my products folder also contains a sub-folder named Women. How can I display all images from both Men and Women sub-folders located in products folder I tried displaying products like this: <img src="images/products/ But that doesn't work at all. How can I get to display all images from both sub-folders in PHP? Quote Link to comment https://forums.phpfreaks.com/topic/262183-display-images-from-different-sub-folders-located-in-one-folder/ Share on other sites More sharing options...
QuickOldCar Posted May 7, 2012 Share Posted May 7, 2012 I guess can use file_exists() Providing that each id is unique to both men and women. I used default.jpg as a filler image if none of the 2 images were found, just so can display something. Just make an image for it. if (file_exists("images/products/Men/".$id.".jpg")) { $image_source = "images/products/Men/".$id.".jpg"; } elseif (file_exists("images/products/Women/".$id.".jpg")) { $image_source = "images/products/Women/".$id.".jpg"; } else { $image_source = "images/default.jpg"; } And for this line you can use this instead $dynamicList .= '<td width="135"><a href="product_details.php?productID=' . $id . '"><img src="$image_source" alt="' . $product_name . '" width="129" height="169" border="0"></a></td> If that doesn't work for you, then add a field to designate if is Men or Women in your database so you can use those values to determine which folder. And an even better suggestion would be to save the exact image location in the database. Quote Link to comment https://forums.phpfreaks.com/topic/262183-display-images-from-different-sub-folders-located-in-one-folder/#findComment-1343611 Share on other sites More sharing options...
QuickOldCar Posted May 7, 2012 Share Posted May 7, 2012 oops, sorry, the line should be this $dynamicList .= '<td width="135"><a href="product_details.php?productID=' . $id . '"><img src="' . $image_source . '" alt="' . $product_name . '" width="129" height="169" border="0"></a></td> Quote Link to comment https://forums.phpfreaks.com/topic/262183-display-images-from-different-sub-folders-located-in-one-folder/#findComment-1343613 Share on other sites More sharing options...
manhunt234 Posted May 7, 2012 Author Share Posted May 7, 2012 Thank you, QuickOldCar. It all works perfectly well now! Quote Link to comment https://forums.phpfreaks.com/topic/262183-display-images-from-different-sub-folders-located-in-one-folder/#findComment-1343616 Share on other sites More sharing options...
QuickOldCar Posted May 7, 2012 Share Posted May 7, 2012 Are welcome, mark as solved Quote Link to comment https://forums.phpfreaks.com/topic/262183-display-images-from-different-sub-folders-located-in-one-folder/#findComment-1343617 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.