array_groupBy and display image
4 posts by 2 authors in: Forums > CMS Builder
Last Post: September 23, 2020 (RSS)
Hi All.
I'm grouping records by brand with the code below:
// load records from 'independent_research_reports'
list($researchRecords, $researchMetaData) = getRecords(array(
'tableName' => 'independent_research_reports',
'where' => "`typeNum` = '1'",
'loadUploads' => true,
'allowSearch' => false,
));
$researchSeries = array_groupBy($researchRecords, 'brandsNum:label', true);
Sor to display the brand, I use;
<?php foreach($researchSeries as $series => $items): ?>
<div class="row">
<div class="col"><h3><?php echo htmlencode($series) ?></h3></div>
</div>
<?php foreach ($items as $record): ?>.....<?php endforeach ?>
<?php endforeach ?>
My question is, how do I go about displaying an image with each brand?
<?php foreach($researchSeries as $series => $items): ?>
<div class="row">
<div class="col">[IMAGE HERE]
<h3><?php echo htmlencode($series) ?></h3></div></div>
<?php foreach ($items as $record): ?>.....<?php endforeach ?>
<?php endforeach ?>
Tim (toledoh.com.au)
By Carl - September 18, 2020 - edited: September 23, 2020
Hi Toledoh,
Were you only wanting to display a single image? In your getRecords function, you are including the uploads in your results so the following code should do the trick you're wanting. Note: I haven't tested it, so you might need to change some things to get it to work. If you want to display multiple images, you can just loop the $image variable and get every image attached to the record.
<?php foreach($researchSeries as $series => $items): ?>
<div class="row">
<div class="col">
<?php
$image = $items['images'];
if (!empty($images[0]['urlPath'])):
?>
<img src="<?php echo $images[0]['urlPath']; ?>">
<?php
endif;
?>
<h3><?php echo htmlencode($series) ?></h3></div>
</div>
<?php foreach ($items as $record): ?>.....<?php endforeach ?>
<?php endforeach ?>
PHP Programmer
interactivetools.com
Thanks Carl - however that's not working.
I kind of think that this is because the code you've supplied is to display the image per research record - whereas I'm trying to get the image associated to each "brand" based on the grouping
$researchSeries = array_groupBy($researchRecords, 'brandsNum:label', true);
Thoughts?
Tim (toledoh.com.au)
By Carl - September 23, 2020
What if you created a $researchRecords loop first and then loop over other items? It might be easier if you submitted a support request so I can see how everything is being joined together and I can more specifically help with a code snippet.
https://www.interactivetools.com/support/request/
PHP Programmer
interactivetools.com