2

所以这就是我想要做的。我有一个包含公司列表的表格。我有另一个表,其中包含特定员工的公司列表。这是表格

公司
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; ?>
4

1 回答 1

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用公司的idname作为键填充,值对如下:

$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. 我也修复了选项标签。

于 2012-10-08T22:16:44.417 回答