Displaying 1 upload from first record only
10 posts by 4 authors in: Forums > CMS Builder
Last Post: May 7, 2014 (RSS)
By Kittybiccy - May 2, 2014
Hi guys,
This is probably really simple....on my home page I'm pulling in the first 3 records from a larger news section. I would like 1 upload from the first record only to display. How could I acheive this?
Many thanks in advance!
Hannah
By moh3 - May 2, 2014
use the following:
<?php
foreach ($data as $record):
echo $record['image'][0]['urlPath'];
endforeach;
?>
$data is the group of records from a specific section
image is the upload field name
By rconring - May 4, 2014
Just after the news viewer you need to get the first record and then the first image of the first record as such:
<?php
list($newsRecords, $newsMetaData) = getRecords(array(
'tableName' => 'news', // THIS WOULD BE YOUR VIEWER CODE
'limit' => '3',
'loadUploads' => true,
'allowSearch' => false,
));
// ADD THIS BELOW YOUR VIEWER AND SUBSTITUTE YOUR RECORD AND FIELD NAMES
// get first record that has image
$imageRecord = @$newsRecords[0];
// get the first image in first record
$displayImage = $imageRecord['ImageFieldName'][0];
?>
Then, in the body, use $displayImage as your image variable like so:
<img src="<?php echo $displayImage['urlPath'] ?>" width="<?php echo $displayImage['width'] ?>" height="<?php echo $displayImage['height'] ?>">
You would probably want to check for the existence of the graphic before you use it so as not to create an error.
Hope this is what you are after.
Conring Automation Services
----------------------------------------
Software for Business and Industry Since 1987
By Kittybiccy - May 6, 2014
I think this is nearly there but all that happens is it displays the image for the first record for the other two news records ie. displays that 1 image for all 3 records displayed in the side bar. Do I need to add an aditional bit of code or adjust slightly?
Thanks!
By rconring - May 6, 2014
I think you need this logic from another post:
http://www.interactivetools.com/forum/forum-posts.php?postNum=2231833#post2231833
See if that helps.
Conring Automation Services
----------------------------------------
Software for Business and Industry Since 1987
By Damon - May 6, 2014 - edited: May 7, 2014
Hi,
Try this code but change the variables to match what you have:
<?php $recordCount = 1; ?>
<?php foreach ($newsRecords as $record): ?>
<?php if ($recordCount == 1): // first record ?>
<?php foreach ($record['uploads'] as $index => $upload): ?>
<img src = "<?php echo $upload['urlPath'];?>" width = "<?php echo $upload['width'];?>" height = "<?php echo $upload['height'];?>" />
<?php endforeach; ?>
<strong><?php echo $record['title']; ?></strong><br />
<?php echo $record['content']; ?><br />
<br />
<?php else: ?>
<strong><?php echo $record['title']; ?></strong><br />
<?php echo $record['content']; ?><br />
<br />
<?php endif; ?>
<?php $recordCount++; // increase the counter so images are not displayed for records greater than 1 ?>
<?php endforeach; ?>
So what happens is a counter is set at 1 and the first if statement checks to see if the counter matches 1. If it does, the green code is used, and if it doesn't, the orange code.
Before the ending foreach statement we have $recordCount++; which will increment the $recordCount variable to 2, then 3. This prevents the code for only record 1 from being used.
Hope this helps!
Damon Edis - interactivetools.com
Hire me! Save time by getting our experts to help with your project.
http://www.interactivetools.com/consulting/
By Kittybiccy - May 7, 2014
Hi Damon,
It doesn't like the imagecount and throws up an undefined variable when I add that code in. This is the page I'm working on..... can you see what's going wrong?
http://farleighschool.com/NEW_SITE/index.php
Thanks!
By rconring - May 7, 2014
It should have been $recordsCount not $imageCount
Here, try this:
<?php $recordCount = 1; ?>
<?php foreach ($newsRecords as $record): ?>
<?php if ($recordCount == 1): // only on first record ?>
<?php if($record['uploads']): // if images actually exist?>
<?php $upload = $record['uploads'][0] // get the first image in the array?>
<img src = "<?php echo $upload['urlPath'];?>" width = "<?php echo $upload['width'];?>" height = "<?php echo $upload['height'];?>" />
<?php endif // end, if images exist ?>
<?php endif // end if first record ?>
<!-- Print the content for all -->
<strong><?php echo $record['title']; ?></strong><br />
<?php echo $record['content']; ?><br />
<br />
<?php $recordCount++; // increase the counter so images are not displayed for records greater than 1 ?>
<?php endforeach; ?>
I didn't test it but it should work.
Conring Automation Services
----------------------------------------
Software for Business and Industry Since 1987
By Damon - May 7, 2014
Hi Ron,
Thanks for catching that variable name that was wrong. :)
Damon Edis - interactivetools.com
Hire me! Save time by getting our experts to help with your project.
http://www.interactivetools.com/consulting/