简体   繁体   English

循环时将查询与PHP进行比较

[英]Comparing queries with PHP while loop

Here is my table setup: 这是我的表设置:

  • employee: Employee information, name, address, etc... 员工:员工信息,姓名,地址等。
  • group: List of distribution groups. 组:通讯组列表。
  • employee2group: table that links employees to multiple groups. employee2group:将员工链接到多个组的表。

I have it setup this way so I can add groups in the future, without having to add columns to my employee table. 我以这种方式进行设置,以便将来可以添加组,而不必在我的员工表中添加列。 This may seem like common sense to you all, but I just started with PHP a couple months ago, so everything is still new and confusing to me. 这对你们所有人来说似乎都是常识,但几个月前我刚开始使用PHP,所以一切都还是新的,让我感到困惑。

So, I'm working on my update form, which will display a list of check boxes that is populated with a simple SELECT * FROM group query. 所以,我正在处理我的更新表单,它将显示一个复选框列表,其中填充了一个简单的SELECT * FROM group查询。 The idea is to show the ones that an employee is part of as "checked" when I view the employee in the update form. 我的想法是,当我在更新表单中查看员工时,显示员工所属的“已检查”。

Currently, my while loop to show the list of check boxes is this: 当前,我的while循环显示复选框列表是这样的:

<?php
 $group_list_query = "SELECT * FROM group"
 $group_list_result = mysql_query($group_list_query, $cmsWrite)
 while ($row = mysql_fetch_assoc($group_list_result)) {
  echo "<input type=\"checkbox\" name=\"distro_{$row['group_name']}\"> {$row['group_name']}";
 }
?>

Pretty simple. 很简单。 I might have some syntax errors in there, but in my code they don't exist, because it works fine. 我可能在其中存在一些语法错误,但是在我的代码中它们不存在,因为它可以正常工作。

So what I need to do, is run another query that returns ONLY the names of the groups that the employee belongs to: 所以我需要做的是运行另一个只返回员工所属组的名称的查询:

SELECT group.group_name 
FROM group JOIN employee2group ON group.group_id = employee2group.group_id 
WHERE employee2group.employee_id ='{$_GET['employee_id']}'

Then, I need to compare the two queries, and output a normal check box when there isn't a match, and output a checked check box when there is a match. 然后,我需要比较两个查询,并在没有匹配时输出一个普通的复选框,并在匹配时输出一个选中的复选框。

I tried doing a while statement that set $row = query1 and $row2 = query2 , and compare the $row and $row2 values, but then it only returned instances where both queries had results, instead of all of them. 我尝试使用设置$row = query1$row2 = query2的while语句,并比较$row$row2值,但之后它只返回两个查询都有结果的实例,而不是所有查询。

I hope this makes sense, I've been trolling the internet for a while, and haven't found anything that pertains to my problem. 我希望这是有道理的,我已经拖了一段时间互联网,并没有找到任何与我的问题有关的东西。

Thanks for reading! 谢谢阅读!

Two solutions : 两种解决方案

1 : Get a array of the groupIds that user is a member of, then when looping through the group list. 1:获取用户所属的groupId的数组,然后在遍历组列表时进行。 Check if the id is in that array with in_array, if it is dont display the check box 使用in_array检查ID是否在该数组中,如果不显示该复选框

2 : Do a left join on the employee2group table and check if the value = null or not, if its null display the check box. 2:在employee2group表上进行左联接,并检查该值是否等于null,如果其null显示复选框。

-- -

Off question topic but you should also look at using bound paramaters rather than just including them inthe sql statement like that, leaves it open to sql injection otherwise. 毫无疑问,但您还应该考虑使用绑定的参数,而不是像这样仅将它们包括在sql语句中,否则将其留给sql注入。

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

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