Hide option from select menu with PHP
4 posts by 3 authors in: Forums > CMS Builder
Last Post: January 6, 2020 (RSS)
By gversion - December 6, 2019
Hello,
I have created a dropdown list in CMSB with different options and the options are pulled from the database.
One of the options is called "Management" and I would like to hide this from the front-end so it only displays for certain types of users. I am using the Website Membership plugin.
Could someone please help me update the code below to achieve this?
<?php
// get field options
$tablename = 'units';
$fieldname = 'unit_name';
$selectedValue = @$_REQUEST['unit_name];
$valuesAndLabels = getListOptions($tablename, $fieldname);
foreach($valuesAndLabels as $key => $val) {
if(!is_numeric($key)) {
unset($valuesAndLabels[$key]);
}
}
$optionsHTML = getSelectOptions($selectedValue, array_keys($valuesAndLabels), array_values($valuesAndLabels));
?>
Thank you,
Greg
By daniel - December 10, 2019
Hi Greg,
I think something like the following should do the trick:
<?php
global $CURRENT_USER;
// get field options
$tablename = 'units';
$fieldname = 'unit_name';
$selectedValue = @$_REQUEST['unit_name'];
$valuesAndLabels = getListOptions($tablename, $fieldname);
foreach($valuesAndLabels as $key => $val) {
if(!is_numeric($key)) {
unset($valuesAndLabels[$key]);
}
if($val == 'Management' && $CURRENT_USER['member_type'] != 'value') {
unset($valuesAndLabels[$key]);
}
}
$optionsHTML = getSelectOptions($selectedValue, array_keys($valuesAndLabels), array_values($valuesAndLabels));
?>
A few things to note:
- You only need to add "global $CURRENT_USER;" if it doesn't already appear earlier in the code
- You'll need to update "$CURRENT_USER['member_type'] != 'value'" to match the field name and value you're using in your members table
Let me know if you have any questions!
Thanks,
Technical Lead
interactivetools.com
Hi Daniel,
That's exactly what I was hoping for, thank you very much!
Regards,
Greg
By mbareara - January 6, 2020
this is the code
<?php
list($localiRecords, $localiMetaData) = getRecords(array(
'tableName' => 'locali',
'limit' => '4',
'where' => mysql_escapef('city = ? AND num != ?', $localiRecord['city']),
));
?>
<?php foreach ($localiRecords as $record): ?>