Can’t make counting image count work correctly

6 posts by 2 authors in: Forums > CMS Builder
Last Post: April 2, 2010   (RSS)

Hi All,

It seems I'm stumped again...

Here’s what I’m trying to do.

I have a multi-record editor called Portfolio Images that has an image upload field and a group-code field.

A detail viewer is set up to show thumbnails for all the images that match a particular group code.

If there’s only one thumbnail shown on the detail viewer I need to display one block of text.

If there’s more than one thumbnail shown, I need to display another block of text.

I’ve tried: <?php foreach ($portfolio_imagesRecords as $record): ?>
<?php $count = 0; ?>
<?php foreach ($record['image'] as $upload): ?>
<?php if (++$count > 1): ?>There is more than one thumbnail<?PHP else: ?>There is only one thumbnail
<?php endif ?>
<?php endforeach; ?>
<?php endforeach; ?>


But if there’s more than one thumbnail then I get a jumble of both text blocks.

I tried one or more <?PHP break ?> but that didn’t work either.

I sure could use some help.

Thanks,

Jerry Kornbluth
The first CMS Builder reference book is now available on-line!







Take advantage of a free 3 month trial subscription, only for CMSB users, at: http://www.thecmsbcookbook.com/trial.php

Re: [gkornbluth] Can’t make counting image count work correctly

By Jason - March 30, 2010

Hi Jerry,

I think the problem is that you are testing the value of $count inside of a loop. Therefore, the first time it goes through the loop it will display the first text message and then display the second one the second time around. Try re-arranging the code like this:
<?php foreach ($portfolio_imagesRecords as $record): ?>
<?php $count = 0; ?>
<?php foreach ($record['image'] as $upload): ?>
<?php $count++; ?>
<?php endforeach; ?>
<?php echo $count>1 ? "There is more than one thumbnail" : "There is only one thumbnail" ; ?>
<?php endforeach; ?>


Let me know if that works for you.
---------------------------------------------------
Jason Sauchuk - Project Manager
interactivetools.com

Hire me! Save time by getting our experts to help with your project.
http://www.interactivetools.com/consulting/

Re: [Jason] Can’t make counting image count work correctly

WOW...

Thanks Jason

I'll implement it (and document it in the Cookbook) shortly.

Best,

Jerry
The first CMS Builder reference book is now available on-line!







Take advantage of a free 3 month trial subscription, only for CMSB users, at: http://www.thecmsbcookbook.com/trial.php

Re: [gkornbluth] Can’t make counting image count work correctly

Jason,

My fault in explanation. A few things yet to solve.

WHAT'S HAPPENING
The" count" function actually counts the number of images in each “Portfolio Image” record assigned to a particular group_code (as the code says it will).

WHAT'S ACTUALLY WANTED
What I really want to show, regardless of how many images are actually uploaded in each record, is:

If there is more than one portfolio_images record assigned to a particular group_code, then display, “There is more than one record assigned to this group“.

If there’s only one portfolio_images record assigned to a group_code, then display, “There is only one record assigned to this group”.

BACKGROUND
I assign each image in the Portfolio_images table to it’s appropriate group by checking a group_code list checkbox in the image’s record.

EXAMPLE
The page at http://www.susansaladino.com/artwork%20with%20image%20count.php?group_code=Last-Birds, uses a <?php break ?> to display only the first image thumbnail in each record assigned to the code_group Last-Birds.

There are as many repeats of the message as there are records (as you can see in the example above), and the messages change dependent on how many images are actually uploaded in each record. There are 2 images uploaded in the first record, and 1 image in each of the subsequent records.

Obviously, I’d like to display either message in the viewer only once.

Again, sorry for the confusion.

Best,

Jerry
The first CMS Builder reference book is now available on-line!







Take advantage of a free 3 month trial subscription, only for CMSB users, at: http://www.thecmsbcookbook.com/trial.php

Re: [Jason] Can’t make counting image count work correctly

By gkornbluth - April 2, 2010

Absolutely perfect.

I already had another counter on the page so I called this one counter2 so that they didn't conflict.

Thanks again,

Jerry Kornbluth
The first CMS Builder reference book is now available on-line!







Take advantage of a free 3 month trial subscription, only for CMSB users, at: http://www.thecmsbcookbook.com/trial.php