Array_unique being ignored

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

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: [Jason] Array_unique being ignored

By Gabums85 - February 24, 2012 - edited: February 24, 2012

Thank you for the wonderfully speedy reply. I used the code you provided and it does work in removing the duplicates. However, I do need the have the foreach statement (I believe) in order to have certain programs be listed depending on the user. So this code:
<?php foreach ($software_serialRecords as $record): ?>
<?php if (($CURRENT_USER['fullname']) == ($record['user'])): ?>

I still need to use. I'm not sure how though. I've tried putting it before and after the code you provided but it doesn't work. I've also tried:
<?php if (($CURRENT_USER['fullname']) == ($software_serialRecords['user'])): ?>
So I wouldn't have to depend on the foreach statement, but nothing shows up in the drop down menu.


\\\\\\\\\ UPDATE \\\\\\\\

I added this line:
'where' => mysql_escapef(" user = ? ", $CURRENT_USER['fullname'] ),
to here:
<?php
list($software_serialRecords, $software_serialMetaData) = getRecords(array(
'tableName' => 'software_serial',
'orderBy' => 'program',
'where' => mysql_escapef(" user = ? ", $CURRENT_USER['fullname'] ),
));
?>

and now it shows the listings for the specific user. HOWEVER, I also need the option to have the main account show ALL listings. So if your signed in under just you user name, only your records show up (which it does) but when you are signed in to the main account then all the listings show up... Still trying to figure that part out...

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