How can I get the nth upload for a record?
6 posts by 3 authors in: Forums > CMS Builder
Last Post: June 14, 2013 (RSS)
By rconring - June 14, 2013
I have seen the answer to this somewhere in the forum but cannot seem to find it. In a record with uploads, I need to grab the nth upload from the array. I have been using a foreach loop and skipping all but the one I want which works, but seems a lot of code to accomplish the taks. Since CMSB is handling the related uploads to the record internally, I cant figure out how to simply get a particular upload without the loop.
Help!
Conring Automation Services
----------------------------------------
Software for Business and Industry Since 1987
By gkornbluth - June 14, 2013 - edited: June 14, 2013
Hi Ron,
here's a recipe from my CMSB Cookbook http://www.thecmsbcookbook.com that adresses that question.
Hope it helps,
Jerry Kornbluth
DISPLAY A SPECIFIC IMAGE FROM A SPECIFIC RECORD
My client wanted to be able to display a specific image from a group of records and multi image uploads.
I couldn’t get this to work until Chris Waddell from Interactive Tools unlocked the secret.
He said:
If $affiliationsRecords is a list of all your records, then $affiliationsRecords[0] will give you the first record, $affiliationsRecords[1] will give you the second, etc. It's a little confusing that the counting starts at 0 instead of 1.
$affiliationsRecords[3]['logo'][2] will grab the fourth record ([3]), its list of images (['logo']), and select its third image ([2]).
Since $affiliationsRecords[1]['logo'] refers to the list of images. You need to use $affiliationsRecords[1]['logo'][0] if you want to refer to a specific image ([0] gives you the first one, etc.)
You’ll need to define a variable for the specific image. To call the 3rd image from the 4th record I used
_____ code ________________________________________________
<?php $myImage = $affiliationsRecords[3]['logo'][2]; ?>
__________________________________________________________
The code that you’d use in the body of your viewer page where you want the image to display would be:
_____ code ________________________________________________
<?php $myImage = $affiliationsRecords[3]['logo'][2]; ?>
<img src="<?php echo $myImage['thumbUrlPath'] ?>" width="<?php echo $myImage['thumbWidth'] ?>" height="<?php echo $myImage['thumbHeight'] ?>" alt="" />
__________________________________________________________
You can also define other variables, like this one for the link to a detail page relating to the same record:
_____ code ________________________________________________
<?php @$mylink = $affiliationsRecords[4]['_link']; ?>
__________________________________________________________
Then the code including a linked image might look like this:
_____ code ________________________________________________
<?php @$myImage = $affiliationsRecords[3]['logo'][2]; ?>
<?php @$mylink = $affiliationsRecords[3]['_link']; ?>
<a href="<?php echo $mylink ?>"><img src="<?php echo $myImage['thumbUrlPath'] ?>" width="<?php echo $myImage['thumbWidth'] ?>" height="<?php echo $myImage['thumbHeight'] ?>" alt="" border="0"/></a>
__________________________________________________________
Take advantage of a free 3 month trial subscription, only for CMSB users, at: http://www.thecmsbcookbook.com/trial.php
By Chris - June 14, 2013
Hi Ron,
The code Jerry posted is for working with Nths uploads belonging to Nths records in a list of records, if you're FOREACHing over your list of records and want to display the Nth upload of each one, you can use $record['uploadFieldName'][4]. Personally, I like to assign an upload to a variable (like $upload) so I don't have to repeat the ['uploadFieldName'][4] part, like this:
<?php foreach( $affiliationsRecords as $affiliationsRecord ): ?>
Title: <?php echo htmlencode($affiliationsRecord['title']) ?>
2nd Upload:
<?php $upload = @$affiliationsRecord['uploadFieldName'][1] ?>
<?php if ($upload): ?>
<img src="<?php echo $upload['thumbUrlPath'] ?>" width="<?php echo $upload['thumbWidth'] ?>" height="<?php echo $upload['thumbHeight'] ?>" />
<?php else: ?>
None!
<?php endif ?>
<?php endforeach ?>
Does that help? Please let us know if you have any questions!
Chris
By gkornbluth - June 14, 2013
Hi Chris,
Thanks for the addition, I'll add that approach to the recipe.
Jerry Kornbluth
Take advantage of a free 3 month trial subscription, only for CMSB users, at: http://www.thecmsbcookbook.com/trial.php
By rconring - June 14, 2013
Thanks Chris ... that's exactly what I did. I set a variable for the second photo in the array when I declared the viewer so I could check for it's existence later in the page and if so, create a More Photos div. Jerry steered me to what I remember seeing ... just had a senior cerebral lapse.
Thanks again!
Conring Automation Services
----------------------------------------
Software for Business and Industry Since 1987
By rconring - June 14, 2013 - edited: June 14, 2013
That's Jerry, what I needed ... couldn't remember where I saw it.
Thanks for the post and have a wonderful weekend!
Conring Automation Services
----------------------------------------
Software for Business and Industry Since 1987