在 PHP 中创建一个选择菜单,默认从 MySQL 数据库中选择 [英] Creating a select menu in PHP with default selected from MySQL db

查看:49
本文介绍了在 PHP 中创建一个选择菜单,默认从 MySQL 数据库中选择的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在为一个站点创建一个管理菜单,我有几个下拉列表需要为每个不同的字段创建,默认选择的选项对于出现的每条记录都不同(这与权限管理员的想法非常相似phpBB 中的面板).换句话说,我有一个表格,列出了用户可能属于的所有组,并且在每一行中都有用于更改该组权限的选择菜单.我目前正在为每组不同的菜单使用如下所示的功能:

I am creating an admin menu for a site and I have several drop down lists that need to be created for each different field with the default selected option being different for each record that appears (this is very similar idea to the permission admin panel in phpBB). In other words, I have a table that lists all of the groups that a user could belong to, and in each row there are select menus for changing the permissions of that group. I am currently using functions for each different set of menus that looks like this:

function rfiPermissions($x){
$query = "SELECT rfi_permission FROM groups WHERE id = '$x'";
$result = mysql_query($query) or die();
$row = mysql_fetch_array($result);
$perm = $row[0];
$option = array();
$option[0] = "<option value='0'>None</option>";
$option[1] = "<option value='1'>Basic</option>";
$option[2] = "<option value='2'>Supplemental</option>";
$option[3] = "<option value='3'>Full</option>";
switch($perm){
    case 0:
        echo $option[0].$option[1].$option[2].$option[3];
        break;
    case 1:
        echo $option[1].$option[0].$option[2].$option[3];
        break;
    case 2:
        echo $option[2].$option[1].$option[0].$option[3];
        break;
    default:
        echo $option[3].$option[1].$option[0].$option[2];
        break;
}
}

我一直在尝试找出一种更精简的方法来执行此操作,因为我需要在我的站点中多次实施此操作,但我一直无法这样做.有没有更好的方法来做到这一点,我可以简单地将选项需要的数组传递给函数,然后从数据库中回显一个值,从数组中删除它,然后回显其余的选项?

I have been trying to figure out a more stream-lined way of doing this because I need to implement this many more times in my site, but I have been unable to do so. Is there a better way of doing this where I can simply pass an array of what the options need to be into a function, then echo the one value from the db, remove it from the array, then echo the rest of the options?

推荐答案

用 php 设置 selected 属性比更改顺序更容易.另外,作为用户,我喜欢一致性,如果我查看下拉菜单并且每次查看它的顺序都发生变化,我会感到沮丧.

It is easier to set the selected attribute with php than to change the order. Plus, as a user I like consistency and if I looked at a drop down and the order changed each time I looked at it I would be frustrated.

$option[0] = "<option value='0' ".($perm==0 ? " selected " : "") .">None</option>";
$option[1] = "<option value='1' ".($perm==1 ? " selected " : "") .">Basic</option>";
$option[2] = "<option value='2' ".($perm==2 ? " selected " : "") .">Supplemental</option>";
$option[3] = "<option value='3' ".($perm==3 ? " selected " : "") .">Full</option>";

这是在if简写中完成的,并且可以扩展以提高可读性

This is done in if short hand and can be expanded for readability

这篇关于在 PHP 中创建一个选择菜单,默认从 MySQL 数据库中选择的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆