Different Image Ranges

Hey guys,

Third question, same topic ; )

Well, I have a PHP snippet that show's the 2nd image in an array. There are 4 images in that array:

Image 1: Outfit
Image 2: Jacket
Image 3: Shirt
Image 4: Jeans

So the 2nd Image is the one that is always suppose to be shown on a Product List page, which is the Jacket.

The problem is, that some products do not have the Outfit image, so there are only 3 images in that array:

Image 1: Jacket
Image 2: Shirt
Image 3: Jeans

The PHP Snippet that I am using is:
        <div>
          <?
            $images = $product->images;
            if (count($images) > 0):
            $image_1 = $images[1]->getThumbnailPath(235, 'auto');
          ?>
          <a href="<?=  $product->page_url('/product') ?>">
            <img src="<?= $image_1 ?>" alt="<?= h($product->name) ?>" />
            <div class="desc">
              <h4><?= h($product->name) ?></h4>
            </div>
          </a>
            <? endif ?>  
        </div>

Open in new window


The image array starts at 0

So if there are only 3 images in the array, then the image that will be shown on the Product List is the Shirt, and not the Jacket.

I know this may be confusing but, is there a way for me to show the 1st image if there are 3 images in the array, and if there are 4 images in the array, then the 2nd image show from that array?

Thanks!
areyouamacAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

parnassoCommented:
I get it, show the 1st image if there are 3 images and  if there are 4 images in the array, then the 2nd image show.

$indexToShow = count($images) - 3;

$image_1 = $images[$indexToShow]->getThumbnailPath(235, 'auto');

Another solution will be to have an associative array and get the image using a key:

$image_1 = $images['Jacket']->getThumbnailPath(235, 'auto');

Hope this helps you
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
areyouamacAuthor Commented:
Thanks!

Here was the code I used to show the 1st image of 3, and 2nd image of 4.
<?
            $images = $product->images;
            if (count($images) > 0):
              $indexToShow = count($images) - 2;
            $image_1 = $images[$indexToShow]->getThumbnailPath(235, 'auto');
          ?>

Open in new window


if it's not too much trouble, how would I use this if there were say, 5 or 6 images? And how does it work?

Thanks again!

0
parnassoCommented:
If there are 5 or 6 images, the problems is to know the order in which the images come, I mean:

one possible ordenation could be:

Image 1: Outfit
Image 2: Jacket
Image 3: Shirt
Image 4: Jeans
Image 5: Hat
Image 6: Gloves

But it could be possible to have:

Image 1: Hat
Image 2: Gloves
Image 3: Shirt
Image 4: Jeans
Image 5: Outfit
Image 6: Jacket

To avoid such a problems I would recommend to use an associative array of images, and get each image using the key.

Thanks for the points!
0
Determine the Perfect Price for Your IT Services

Do you wonder if your IT business is truly profitable or if you should raise your prices? Learn how to calculate your overhead burden with our free interactive tool and use it to determine the right price for your IT services. Download your free eBook now!

areyouamacAuthor Commented:
I think that would be the best way to do it also.

The thing is, all of the product images are added via CMS, so when I add a Jacket image to the list, the image names will automatically be given a random name:

IE. DB_Image_202, DB_Image_398

I however, can set the image title, and description of the images

So, can I assign a unique variable in the Title, say "Item A Jacket" for one, and "Item B Jacket" for another, and which ever Title has "Jacket" in it, then will select that image?

Or will I have to give all Jacket images the same Title (i.e. Jacket) for them to show using the associative array?

Thanks!

0
parnassoCommented:
Agghhh the CMS!  Now I understand. The second thing, putting the Title as Jacket will work but you're losing one field of information, and most important, will you add the images to the CMS or is it someone else who do it? (I mean someone who understand that the title has to be Jacket, Shirt ...)

I think you alrealdy know that it might be better that the image had a category field, and when you add the image to the database you add the category (Jacket, Outfit, Jeans) too. Is it possible?
0
areyouamacAuthor Commented:
Hey,

I'll be adding it to the CMS. The individual items (jacket, Shirt, Jeans) won't be in there own category. Those would be part of an outfit... which it will be in its own category (The category will be based on the outfit, ie. sold online, not sold online, sold somewhere else)

Thanks!
0
areyouamacAuthor Commented:
Hey parnasso,

Here is a quick update:

I am using this snippet here:
<?
   $image_1 = $product->images->find_by_title('Jacket')->getThumbnailPath(180, 'auto');
?>

Open in new window


What is happening now is, the first product's image "Jacket" shows for all other products, regardless of the category it is in.

ie. Item A's "Front" picture, shows for Item B, Item C, etc.

This snippet is also used to show the all products (corresponding to the Category Page product list):
<? 
    $products = $products instanceof Db_ActiveRecord ? $products->find_all() : $products;
    foreach ($products as $product): 
    ?>

Open in new window


Thanks for the help once again!
0
parnassoCommented:
I don't follow you. Do you mean that your method 'find_by_title' never returns null even if the Item has not a Jacket Image?
0
areyouamacAuthor Commented:
Hey parnasso,

I actually have the right way to do, but I am a little confused on how code it:
$grouped_images = array();
foreach ($product->images as $index=>$image)
{
    $array_index = $image->title ? $image->title : $index;
    $grouped_images[$array_index] = $image;
}

Open in new window


if (isset($grouped_images['Front']))

Open in new window


So, the $grouped_images array will contain all product images grouped by the title value.

Is this the way to properly code it?

<div >
<? $grouped_images = array();
   foreach ($product->images as $index=>$image)
    {
      $array_index = $image->title ? $image->title : $index;
      $grouped_images[$array_index] = $image;
    }

     $images = $product->images;
     $image_1 = $grouped_images['Front'] -> getThumbnailPath(180, 'auto');
?>

  <a href="<?=  $product->page_url('/product') ?>">
    <img src="<?= $image_1 ?>" alt="<?= h($product->name) ?>" />
    <div class="desc">
      <h4><?= h($product->name) ?></h4>
      <p class="shortdesc"><?= h($product->short_description) ?></p>
    </div>
  </a>
<? endif ?>
</div>

Open in new window


Thanks for the help!
0
areyouamacAuthor Commented:
I tried the coding above, but I am receiving an Unhandled Error:

Syntax Error for the  <? endif ?> in Line 20.

What do you think?
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
PHP

From novice to tech pro — start learning today.