Array_unique being ignored

5 posts by 2 authors in: Forums > CMS Builder
Last Post: February 28, 2012   (RSS)

Hello all,

I have a dropdown menu that pulls a list of "programs" I have entered into the CMS as a list item. It works fine except that if a program is used more than once, the drop down shows each occurrence of the program. I want it to show just once. I found some code for array_unique but I just can't seem to get it to work.


<?php
list($software_serialRecords, $software_serialMetaData) = getRecords(array(
'tableName' => 'software_serial',
'orderBy' => 'program',
));
?>


<?php foreach ($software_serialRecords as $record): ?>
<?php if (($CURRENT_USER['fullname']) == ($record['user'])): ?>

<?php $programLimit = array(); ?>
<?php $programLimit[] = $record['program']; ?>
<?php $programLimit = array_unique($programLimit); ?>

<?php for ($i = 0; $i<count($programLimit); $i++) { ?>

<option value="software_serials_program.php?program=<?php echo $record['program'] ?>"><?php echo ($programLimit[$i]) ?></option>

<?php ;} ?>

<?php endif ?>

<?php endforeach ?>


The above code works just fine in populating the drop down with the correct programs... but it completely ignores the array_unique code I have used and shows the duplicates. Any idea on why?

Thanks!
Gab

Re: [Gabums85] Array_unique being ignored

By Jason - February 24, 2012

Hi Gab,

The problem here is that your variable $programLimit is being created and populated inside your foreach loop. This means that it will only ever hold 1 value at a time, which is why duplicates are getting through.

Try this instead

<?php $programs = array_unique(array_pluck($software_serialRecords, 'program')); ?>

<?php foreach ($programs as $program) :?>
<option value = "software_serials_program.php?program=<?php echo $program;?>"><?php echo $program;?></option>
<?php endforeach ?>


This will create an array of unique program values from your record set, and then output them one at a time.

Hope this helps
---------------------------------------------------
Jason Sauchuk - Project Manager
interactivetools.com

Hire me! Save time by getting our experts to help with your project.
http://www.interactivetools.com/consulting/

Re: [Gabums85] Array_unique being ignored

By Jason - February 27, 2012

Hi,

I'm not suer what you mean by the "main account". Do you meant the admin account in CMS Builder?

If so, try this:

<?php

$where = "";

if (!$CURRENT_USER['isAdmin']) {
$where = " user = '".mysql_escape($CURRENT_USER['fullname'])."'";
}

list($software_serialRecords, $software_serialMetaData) = getRecords(array(
'tableName' => 'software_serial',
'orderBy' => 'program',
'where' => $where,
));

?>


You should be able to use some variation of this to do what you want.

Hope this helps
---------------------------------------------------
Jason Sauchuk - Project Manager
interactivetools.com

Hire me! Save time by getting our experts to help with your project.
http://www.interactivetools.com/consulting/

Re: [Jason] Array_unique being ignored

Hi Jason,

Thank you for that code. What I meant when I said "main account" could not really be referred to as "isAdmin" since many of us users are "admin" on the account. But I was able to take that code you provided and get it to do what I needed. Here it is just in case anyone else wants to know

<?php

$where = "";

if (($CURRENT_USER['fullname']) != 'Admin Account Name') {
$where = " user = '".mysql_escape($CURRENT_USER['fullname'])."'";
}

list($software_serialRecords, $software_serialMetaData) = getRecords(array(
'tableName' => 'software_serial',
'orderBy' => 'program',
'where' => $where,
));

?>


Thank you again very much for your help!
Gab