Jump to content

Display images from different sub-folders located in one folder


manhunt234

Recommended Posts

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?

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.

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>

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.