所以这就是我想要做的。我有一个包含公司列表的表格。我有另一个表,其中包含特定员工的公司列表。这是表格
公司
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; ?>