简体   繁体   English

PHP中的EMIT选择选项

[英]EMIT Select Option in PHP

My PHP code is returning a list of users out of a MySQL database and putting them in a select list using a JQuery combo box each of them in their own field. 我的PHP代码是从MySQL数据库中返回用户列表,并使用JQuery组合框将其置于选择列表中,每个用户都位于自己的字段中。 There are a couple names out of the DB that need to stay in the DB but don't really need to be in the drop down list as selectable names... is there a way to emit options in my code before the MySQL Query even runs? 数据库中有几个名称需要保留在数据库中,但实际上并不需要作为可选名称出现在下拉列表中……在MySQL查询之前,是否有一种方法可以在我的代码中发出选项运行? Or what is the best way to handle this? 或处理此问题的最佳方法是什么?

Here is my current code: 这是我当前的代码:

// Write out our query.
$query = "SELECT DISTINCT FULLNAME FROM cimssystem.cimsusers ORDER BY FULLNAME;";

// Execute it, or let it throw an error message if there's a problem.
$stmt = $pdo->query( $query );

$datalist = "<select id='name-select' name='name'>";

$datalist .= "\n<option value='0' selected></option>";

foreach ($stmt as $row) {
    $datalist .= "\n<option value='{$row['FULLNAME']}'>{$row['FULLNAME']}</option>";
}

$datalist .= "\n</select>";

echo $datalist;

?>  

这里有很多可能性,但这只是一个示例: $query = "SELECT DISTINCT FULLNAME FROM cimssystem.cimsusers WHERE FULLNAME NOT IN ('name1', 'name2', 'name3') ORDER BY FULLNAME;";

If you are capable of modifying the database, you can add an 'Enabled' column and set the values to true/false accordingly for each entry. 如果您有能力修改数据库,则可以添加“已启用”列,并将每个条目的值分别设置为true / false。 You can then simply modify the query to add a check for that column's values. 然后,您可以简单地修改查询以添加对该列值的检查。 I find this easier than modifying the query to exclude a specific list of people, and it reduces the possibility of a typo on my end in the query. 我发现这比修改查询以排除特定的人员列表更容易,并且它减少了在查询中出现错字的可能性。

I was so caught up in my code I didn't think about going back and selecting them in the database! 我被代码深深吸引,以至于我不打算回去在数据库中选择它们! Just have to make sure to move the order by to the end of the statement for this to work correctly! 只需确保将订单移动到语句的末尾即可正常工作!

SELECT DISTINCT FULLNAME FROM cimssystem.cimsusers 
WHERE FULLNAME NOT IN ('Berryville CSR Intern', 'name', 'name')
ORDER BY FULLNAME ;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM