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.
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
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
This will create an array of unique program values from your record set, and then output them one at a time.
Hope this helps
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/
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:
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:
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:
to here:
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...
<?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:
You should be able to use some variation of this to do what you want.
Hope this helps
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/
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
Thank you again very much for your help!
Gab
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