所以这就是我想要做的。我有一张包含公司列表的表格。我有另一张表,其中包含特定员工的公司列表。这是表格
公司
company_id
名称
employees_companies
empcomp_id
employee_id
company_id
目前我有一个列有所有公司列表的列表框。当用户设置新员工时,他们可以从列表框中选择一个或多个公司。提交表单时,我创建一个包含所有选定公司的数组,循环遍历它们并将新记录添加到employees_companies表中。一切都很好。我遇到的问题是用户可以编辑这些公司的表单。首先,我在列表框中填入所有公司的列表。接下来,我需要为该用户突出显示在employees_companies表中列出的公司。现在,无论查询中返回了多少记录,都只会突出显示一条记录。
以下是获取所有公司和员工公司列表的代码
// get list of all companies
mysql_select_db($database_freight, $freight);
$query_rsCompanyList = "SELECT * FROM companies ORDER BY scac ASC";
$rsCompanyList = mysql_query($query_rsCompanyList, $freight) or die(mysql_error());
$row_rsCompanyList = mysql_fetch_assoc($rsCompanyList);
$totalRows_rsCompanyList = mysql_num_rows($rsCompanyList);
// get employees companies
$colname_rsEmployeeCompanyList = "17";
if (isset($_GET['employee_id'])) {
$colname_rsEmployeeCompanyList = $_GET['employee_id'];
}
mysql_select_db($database_freight, $freight);
$query_rsEmployeeCompanyList = sprintf("SELECT * FROM employees_companies WHERE employee_id = %s", GetSQLValueString($colname_rsEmployeeCompanyList, "int"));
$rsEmployeeCompanyList = mysql_query($query_rsEmployeeCompanyList, $freight) or die(mysql_error());
$row_rsEmployeeCompanyList = mysql_fetch_assoc($rsEmployeeCompanyList);
$totalRows_rsEmployeeCompanyList = mysql_num_rows($rsEmployeeCompanyList);
这是表格
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>test</title>
</head>
<body>
<form id="frmAdd" name="frmAdd" method="post" action="test.php">
<select name="company_id[]" size="4" multiple="multiple" id="company_id[]">
<?php do { ?>
<option value="<?php echo $row_rsCompanyList['company_id']?>"
<?php
do {
if (!(strcmp($row_rsCompanyList['company_id'], $row_rsEmployeeCompanyList['company_id']))) {echo "selected=\"selected\"";}
} while ($row_rsEmployeeCompanyList = mysql_fetch_assoc($rsEmployeeCompanyList)); ?>><?php echo $row_rsCompanyList['name']?></option>
<?php
} while ($row_rsCompanyList = mysql_fetch_assoc($rsCompanyList));
$rows = mysql_num_rows($rsCompanyList);
if($rows > 0) {
mysql_data_seek($rsCompanyList, 0);
$row_rsCompanyList = mysql_fetch_assoc($rsCompanyList);
}
?>
</select>
<input type="submit" name="button" id="button" value="Submit" />
</form>
</body>
</html>
=============================================== ================================ 几乎得到了它,但只有一个问题。公司名称未填充在列表框中。我注意到你的代码没有关闭选项标签。我添加了它并输入$ company_id作为值,因此它使用company_id填充列表框,但我如何回应公司名称。
这是我创建数组的地方
// get list of all companies
mysql_select_db($database_freight, $freight);
$query_rsCompanyList = "SELECT company_id, name FROM companies ORDER BY scac ASC";
$rsCompanyList = mysql_query($query_rsCompanyList, $freight) or die(mysql_error());
$all_company_ids = array();
while ($row_rsCompanyList = mysql_fetch_array($rsCompanyList)) {
$all_company_ids[] = $row_rsCompanyList['compnay_id'];
}
// get list of all employees companies
$colname_rsEmployeeCompanyList = "17";
if (isset($_GET['employee_id'])) {
$colname_rsEmployeeCompanyList = $_GET['employee_id'];
}
mysql_select_db($database_freight, $freight);
$query_rsEmployeeCompanyList = sprintf("SELECT company_id FROM employees_companies WHERE employee_id = %s", GetSQLValueString($colname_rsEmployeeCompanyList, "int"));
$rsEmployeeCompanyList = mysql_query($query_rsEmployeeCompanyList, $freight) or die(mysql_error());
$emp_company_ids = array();
while ($row_rsEmployeeCompanyList = mysql_fetch_array($rsEmployeeCompanyList)) {
$emp_company_ids[] = $row_rsEmployeeCompanyList['compnay_id'];
}
这是表单选项
<?php foreach($all_company_ids as $company_id): ?>
<option value="<?php echo $company_id; ?>" <?php if(in_array($company_id, $emp_company_ids)) echo "selected=\"selected\""; ?>><?php echo $company_id; ?></option>
<?php endforeach; ?>
答案 0 :(得分:2)
我认为这就是您想要的:显示所有公司ID并仅突出显示属于员工的那些 如果我是对的,这是一种更简单的方法:
<?php
$all_company_ids = array(); // You should populate this as you wish, but it's better not to do it in your form!
$emp_company_ids = array(); // Again get this items from the database!
?>
<select name="company_id[]" size="4" multiple="multiple" id="company_id[]">
<?php foreach($all_company_ids as $company_id): ?>
<option value="<?php echo $company_id; ?>" <?php if(in_array($company_id, $emp_company_ids)) echo "selected=\"selected\""; ?>></option>
<?php endforeach; ?>
</select>
请注意,我没有详细介绍如何获取所有公司ID或员工的公司ID。
这个想法非常简单:
您首先获得所有公司ID。然后循环遍历它们并将每个项目显示为option
,如果项目属于该员工,则选择它!
我自己没有尝试过这个代码,但由于这个想法很简单,它应该可以正常工作。
修改强>
对于公司名称,一种简单的方法是将$all_company_ids
与公司的id
和name
一起填充为关键值对,如下所示:
$all_companies = array();
while ($row_rsCompanyList = mysql_fetch_array($rsCompanyList)) {
$all_companies[$row_rsCompanyList['compnay_id']] = $row_rsCompanyList['name'];
}
然后您可以显示如下名称:
<select name="company_id[]" size="4" multiple="multiple" id="company_id[]">
<?php foreach($all_companies as $company_id => $company_name): ?>
<option value="<?php echo $company_id; ?>" <?php if(in_array($company_id, $emp_company_ids)) echo "selected=\"selected\""; ?>><?php echo $company_name ?></option>
<?php endforeach; ?>
</select>
请注意,我将$all_company_ids
变量重命名为$all_companies
。我也修复了选项标签。