Show only one image
3 posts by 2 authors in: Forums > CMS Builder
Last Post: July 2, 2015 (RSS)
By dccreatives - July 1, 2015
I have a page that shows search results. I have a field on the items page called 'main_image'. I recently created a lightbox with Main Image which rotates a few images. I only want to show the first image on my results. What should I add to the php script on the page to only show the first image, if there are more than one uploaded? I want to show each listing with only one image, it shouldnt show all the images.
<?php
list($itemsRecords, $itemsMetaData) = getRecords(array(
'tableName' => 'items',
'debugSql' => false,
'where' => "hide_from_search_page != '1' AND $where",
'allowSearch' => false,
'perPage' => '100',
));
?>
<link href="axis.css" rel="stylesheet" type="text/css" />
<table width="750" align="center" cellpadding="2" cellspacing="2" class="margin-table-search-results">
<tr> <?php foreach ($itemsRecords as $record): ?>
<td width="175" align="left">
<?php foreach ($record['main_image'] as $upload): ?>
<?php if ($upload['isImage']): ?>
<a href="<?php echo $record['link'] ?>"><img src="<?php echo $upload['urlPath'] ?>" alt="" class="img-bord" width="150" height="150" border="0" /></a> <?php endif ?>
<?php endforeach ?>
By Dave - July 1, 2015
Hi dccreatives,
There's a couple ways to show the first image.
One is to just "break" out of the foreach loop after the first iteration:
<?php foreach ($record['main_image'] as $upload): ?>
<?php if ($upload['isImage']): ?>
<a href="<?php echo $record['link'] ?>"><img src="<?php echo $upload['urlPath'] ?>" alt="" class="img-bord" width="150" height="150" border="0" /></a> <?php endif ?>
<?php break; ?>
<?php endforeach ?>
Another would be to not use a foreach loop and refer to the first image directly (untested code):
<?php $upload = @$record['main_image'][0]; // php array's start counting at zero
<?php if (@$upload['isImage']): ?>
<a href="<?php echo $record['link'] ?>"><img src="<?php echo $upload['urlPath'] ?>" alt="" class="img-bord" width="150" height="150" border="0" /></a>
<?php else: ?>
No image!
<?php endif ?>
Let me know if one of those options works for you. Cheers!
interactivetools.com