0

编辑:添加了第一个 SQL 查询。

我网站的一部分有两个下拉菜单。两者中的所有选项都是使用 SQL 查询填充的。Dropdown#1 是类部分的列表(例如 A1)。一旦教授选择了一个部分,Dropdown#2 就会填充学生 ID(例如 1234567)。

学生信息见表 1。这些信息中有“教授姓名”列。为了将学生与班级部分相关联,我需要将“professorName”列与表 2 中的相同列进行匹配,因为班级部分仅在表 2 中找到。

直到这里一切都很好,因为在我的查询结束时,我输入了 ORDER BY student ID。但是,其中两个班级部分与两个不同的教授相关联。为了处理这个问题,我使用下面的代码来循环遍历每个教授的名字。

 $from site = $_POST['section'];

    $query = ("SELECT professorName FROM Table 2 WHERE classSection='$fromsite'");
    $NumberofProfessorNames = $objMSSQL->getAffectedRows();

    echo $NumberofProfessorNames;

    for ($j=0; $j<$NumberofProfessorNames; $j++)
    {
    $section= $query[$j][professorName];

    $output = $objMSSQL->getTable("SELECT DISTINCT StudentID from table1 WHERE professorName='$section' ORDER BY StudentID");

    for ($i=0; $i<$objMSSQL->getAffectedRows(); $i++)
    {
    echo "<option value='".$output[$i][studentID]."'>".$output[$i][studentID]."</option>";
    }
    }

问题是,对于仅有的两个甚至是必要的部分(因为有两个professorNames),因为它是这样循环的,所以它最终在下拉列表#2中是这样排序的:

1234567
2345678
3456789
4567890
1234123
2345765
3456999
4567000

我有限的编程经验使我无法理解如何解决这个看似简单的问题。

谢谢您的帮助。

4

3 回答 3

2

在第二个查询中连接 table1 和 table2 并且只查询数据库一次,而不是遍历教授并为每个查询 table1。例如:

$query = [... FROM Table2...];
$NumberofProfessorNames = $objMSSQL->getAffectedRows();

echo $NumberofProfessorNames;

$output = $objMSSQL->getTable("
    SELECT DISTINCT StudentID 
    from table1 
        join table2
            on ...
    WHERE [the same clause you used in $query]
    ORDER BY StudentID"
);

for ($i=0; $i<$objMSSQL->getAffectedRows(); $i++)
{
    echo "<option value='".$output[$i][studentID]."'>".$output[$i][studentID]."</option>";
}

它比生成 WHERE IN 子句更优雅(而且几乎可以肯定更有效)。

于 2013-03-26T14:51:44.077 回答
1

Yu可以这样做:

$section = "('";
for ($j=0; $j<$NumberofProfessorNames; $j++)
{
   $section.= $query[$j][professorName] . "','";
}
$section = substr($section, 0, -3) . ')'; //$section contains ('prof1','prof2')

$output = $objMSSQL->getTable("SELECT DISTINCT StudentID from table1 WHERE professorName IN $section ORDER BY StudentID");

for ($i=0; $i<$objMSSQL->getAffectedRows(); $i++)
{
   echo "<option value='".$output[$i][studentID]."'>".$output[$i][studentID]."</option>";
}

那就是在一个带有IN()语法的 sql 中查询所有教授。

更新:我刚刚注意到您使用 sql server 而不是 mysql,所以我稍微更改了 IN() 语法并更改了指向 sql server 帮助文档的链接。

于 2013-03-26T14:52:13.030 回答
0

听起来您的表格没有标准化。好的表格应该有一个sections 表、一个students 表和一个professor 表。每个表格中的信息应特定于表格的主题。

学生

student_id
student_last_name
student_first_name
student_address
etc

部分

section_id
section_name - multiple sections can tend to have the same name but differing content
section_description
section_year - sections can change from year to year

学院

faculty_id
faculty_name - this is not a key field, more than one person can have the same name.
faculty_address
faculty_type - adjunct, fulltime, etc.

然后,您将拥有关系表,因此您可以将教授与部分相关联,将学生与部分相关联。

Faculty_2_sections

f2s_id
faculty_id
section_id

student_2_sections

s2s_id
student_id
section_id

这使它变得非常简单,因为如果学生登录,那么您已经有了他们的学生 ID。如果是教授,你已经有了他们的faculty_id

如果您要吸引学生,您的 sql 可能如下所示:

$sql = "select * from students s,sections sc,faculty f,faculty_2_sections f2s,student_2_sections s2s where student_id='$student_id' and s2s.student_id=s.student_id and s2s.section_id=sc.section_id and f2s.faculty_id=f.faculty_id and f2s.section_id=s2s.section_id";

如果你正在为教师拉动,你会这样做:

$sql = "select * from students s,sections sc,faculty f,faculty_2_sections f2s,student_2_sections s2s where faculty_id='$faculty_id' and f2s.faculty_id=f.faculty_id and f2s.section_id=s2s.section_id and s2s.section_id=sc.section_id and s2s.student_id=s.student_id";

然后,您可以拉出部分列表以填充 section_ids 下拉列表,以仅显示特定部分的学生或教师。

于 2013-03-26T15:07:11.683 回答