Duplicate Items from a for each array

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

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

https://preview.ibb.co/nx8r3a/Ashampoo_Snap_2017_09_04_19h52m13s_001.png

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>
</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">
</colgroup><thead>
<tr>
<th>Documento</th>
<th>Descarga</th>
</tr>
</thead>

<tbody>
<tr>
<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): ?>
<?php
$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>
</tr>
<?php endforeach ?>

By Dave - September 5, 2017

Hi Alan, 

Try defining your array before your first foreach. eg: 

<?php $alreadyListed = []; ?>

That way it won't be undefined when you check if the first time.

Let me know if that works for you.

Dave Edis - Senior Developer
interactivetools.com

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

http://preview.ibb.co/b3vQWv/Ashampoo_Snap_2017_09_05_22h35m06s_001.png

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

<?php
  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
interactivetools.com

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
interactivetools.com