Foreach Loop

I have some code I trying to rework, having problems figuring out how to restructure it. I have product skus that are for example (22222, 33333-2, etc.) These skus all have images associated and named with the sku number. There are also additional photos for each sku and they are named (22222_1, 22222_2 and 33333-2_1, 33333-2_1) now I need to change the naming of the additional images to things like (22222_1a ad 33333-2_1s and 33333-2_2s). I can not figure out how to restructure the foreach loop. Currently it has a counter working so it displays the images based on the skus sequentially if there is _1 image. Since I will not be renaming all the old images I need it work for both the naming and the new naming. I’ve tried multiple things - I think it keeps getting hung up because of the $counter - but not sure how to use that with the new numbering system.

[php]if (file_exists(“images/productimages/med_$product[sku].jpg”)) { //Display dropout photo
echo ("

<td align=“center”>");
$counter = 0 ;
foreach (glob("images/productimages/thumb$product[sku]*.jpg") as $filename) { //Loop through set photos
if($counter++ < 0) continue ;
echo ("<img src="$filename" width=“70px” height=“70px”);" alt="" . $product[‘name’] . " alternate image" id="" . $product[‘sku’] . "
” . ($counter) . “” class=“productDetailThumb” />");
}

//Display dropout thumb
echo ("<img src=“images/productimages/thumb_$product[sku].jpg” width=“70px” height=“70px”);" alt="" . $product[‘name’] . " alternate image" id="$product[sku]" class=“productDetailThumb”/>

You must be a registered customer and logged in to view larger images.

RegisterLog in

I'll register later.

\""
"); }

if (file_exists(“images/productimages/med_$product[sku]1.jpg")) { //Display thumbnails if set photo exisits
echo ("<table class=“productDetailImgThumbs”>

"); }

//Download Large Image
if (hasAccessByLevel(6, ‘LTE’, $userName)){
echo ("<ul style=“margin-top:20px;”>

  • <a href=“images/productimages/large_$product[sku].jpg” class=“largeImage”>Download large image (right click, save as)
  • “);
    if (file_exists(“images/productimages/large_$product[sku]1.jpg")) { //if set photo exists
    $counter = 0 ;
    foreach (glob("images/productimages/large
    $product[sku]*.jpg") as $filename) { //Loop through set photos
    if($counter++ < 0) continue ;
    echo ("
  • <a href="images/productimages/large
  • $product[sku]_$counter.jpg” class=“largeImage”>Download large set photo
    “);
    }
    }
    echo (””);
    }[/php]

    You should escape out of the php so you dont have to do all that quote escaping. Your code will be much cleaner and more readable. I can tell you do not have error reporting turned on or you would be aware of another problem.

    This:

    $product[sku]

    Should be

    $product['sku]

    Thank you I will check on that - it is not my code I am just now having to edit someone else’s code. Still confused on how to achieve the end result I am looking for.

    Sponsor our Newsletter | Privacy Policy | Terms of Service