Page 1 of 1

Display images from two sub-folders located in one folder

Posted: Sun May 06, 2012 9:47 pm
by manhunt234
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

Code: Select all

$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:

Code: Select all

<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?

Re: Display images from two sub-folders located in one folde

Posted: Sun May 06, 2012 11:07 pm
by manhunt234
Fixed the problem by using file_exists() function

Code: Select all

$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"]; 

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"; 
                } 

         $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> 
<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;