Duplicate Items from a for each array

8 posts by 2 authors in: Forums > CMS Builder
Last Post: September 21, 2017   (RSS)

By AlanAlonso - September 4, 2017

I have this code with a little error, clic image link to see the error.


Please help me.

<?php foreach ($transparenciaRecords as $record): ?><?php if(!in_array(strtolower($record['a_o']), $alreadyListed)): ?><div class="block">
<div class="container">
<h2 class="block-title block-title--single">Transparencia <?php echo $record['a_o'] ?></h2>
</div><?php $alreadyListed[] = strtolower($record['a_o']); ?>
<?php endif; ?>
<div class="block">
<div class="container">
<!-- Wide table with range of cols -->
<div class="table-responsive">
<table class="table table-bordered table--wide table-present">
<colgroup class="col-sm-width">
</colgroup><colgroup class="col-sm-width">

<td><?php echo $record['a_o'] ?> - <?php echo $record['carpeta'] ?> - <?php echo $record['titulo'] ?></td>
<td><a class="btn btn-primary btn-sm" target="_blank" href="<?php foreach ($record['archivo'] as $upload): ?>
$extension = $upload['extension'];
if($extension == 'pdf' || $extension == 'rar') {
echo $upload['urlPath'];
} else {
echo "https://view.officeapps.live.com/op/view.aspx?"; echo $upload['filename'];
?><?php endforeach ?>">Ver Documento</a></td>
<?php endforeach ?>

By AlanAlonso - September 5, 2017

Thank you Dave this solved the Undefined variable error, but it still shows a 2016 record on 2015 records.


By Dave - September 6, 2017

Can you try sorting by year in descending order? 

'orderBy' => 'a_o DESC, carpeta',

Let me know if that works for you.

Dave Edis - Senior Developer

By AlanAlonso - September 6, 2017

Thank you Dave, I tested the code but the  sorting by year in descending order  I have the same problem.

  require_once "/lib/viewer_functions.php";
  list($transparenciaRecords, $transparenciaMetaData) = getRecords(array(
    'tableName'   => 'transparencia',
    'allowSearch' => '0',
    'orderBy' => 'a_o DESC, carpeta'

By Dave - September 11, 2017

If you add this code just after that it will show debug output with the contents of $transparenciaRecords.

<?php showme($transparenciaRecords); ?>

Are the years in descending order in that output?  Also, can you confirm a_o is the year field? 

Dave Edis - Senior Developer

By AlanAlonso - September 20, 2017

Hello Dave,

a_o  is the year field

I look that years are in descending order in that output.

By Dave - September 21, 2017

Hmm, I'm not sure.  You could try outputting each record in your foreach loop as well to see if the order is getting changed somehow:

<?php foreach ($transparenciaRecords as $record): ?>
  <?php showme($record); ?>

And also view-source on the page and make sure the HTML itself is in the right order and it's not just a display issue.  Other than that we'd need to spend some time and debug it to know.  The code looks good from as far as I can tell.  Let me know what you find!

Dave Edis - Senior Developer