dsfsda121545 2012-08-27 19:18
浏览 26
已采纳

MSSQL和PHP echos重复行以获取相同的信息

I am sorry about the title as I am not sure whether my problem is in the PHP or the MSSQL query. I am pretty sure it is php.

I am repairing a PHP page that accesses two different MSSQL tables. the page worked fine until we had a system-wide change that created new department ID's and hence some new department names.

I have this almost completely fixed now except one minor thing. When you click List all departments, it Lists all the departments and each link has the correct individuals under them.

The problem is it lists a link for EACH individual in that department name for example if there are 10 people in the Business department we have ten links to Business and all have the same information under them.

I want to make it so there is only 1 link for each department. the two tables are directory (columns involved are Displayname and Department) and departments (columns involved are name and id)

Can someone please tell me where I need to change this so it only prints one link to each department? Is the problem with my SQL query or the PHP?

here is the code

function listDepts() {

$query = "SELECT directory.Lastname, directory.Firstname, directory.email, 
directory.phone, directory.Office, directory.Department, departments.id, 
 departments.name FROM directory FULL JOIN departments ON 
directory.Department=departments.id  ORDER BY name";

$result = mssql_query($query);

echo "<h3>Please select a department:</h3>
";
echo "<ul>
";

for ($i=0; $i<mssql_num_rows($result); ) {
    $info = mssql_fetch_assoc($result);
    echo "<li><a href=\"dept.php?dept=$info[id]\">$info[name]</a></li>
";
}

echo "</ul>

";

}

Other query

function displayResults($query) {

$result = mssql_query($query);

if (mssql_num_rows($result) > 0) {

    for ($i=0; $i<mssql_num_rows($result); $i++) {
        $info = mssql_fetch_assoc($result);

        if ($info[dept_name] != $last_dept) {
            if ($i > 0) {
                echo "</table>

";
            }

            echo "<h3><a href=\"$info[dept_url]\">$info[dept_name]</a></h3>

";
            echo "<table id=\"directory_table\">
";
            echo "<tr>
";
            echo "<th>Name</th>
";
            echo "<th>E-mail</th>
";
            echo "<th>Phone</th>
";
            echo "<th>Office</th>
";
            echo "<th>Title</th>
";
            echo "</tr>
";
        }


        if (!$info[dept_name] && $i==0) {
            echo "<table id=\"directory_table\">
";
            echo "<tr>
";
            echo "<th>Name</th>
";
            echo "<th>E-mail</th>
";
            echo "<th>Phone</th>
";
            echo "<th>Office</th>
";
            echo "<th>Title</th>
";
            echo "</tr>
";
        }

        if ($i % 2 == 0) {
            echo "<tr class=\"even\">
";
        } else {
            echo "<tr class=\"odd\">
";
        }

        echo "<td>";
        echo ($info[Firstname]) ? "$info[Firstname]" . " " . "$info[Lastname]" : "&nbsp;";
        echo "</td>
";

        echo "<td>";
        echo ($info[email]) ? "<a href=\"mailto:$info[email]\">$info[email]</a>" : "&nbsp;";
        echo "</td>
";

        echo "<td>";
        echo ($info[phone]) ? "$info[phone]" : "&nbsp;";
        echo "</td>
";

        echo "<td>";
        echo ($info[office]) ? "$info[office]" : "&nbsp;";
        echo "</td>
";

        echo "<td>";
        echo ($info[title]) ? "$info[title]" : "&nbsp;";
        echo "</td>
";

        $last_dept = $info[dept_name];
    }

    echo "</table>

";

} else {
    echo "<p>No results found.</p>

";
}

}
  • 写回答

2条回答 默认 最新

  • dongye9182 2012-08-27 19:21
    关注

    If you're only using these two fields:

    echo "<li><a href=\"dept.php?dept=$info[id]\">$info[name]</a></li>
    ";
    

    Why not just make your select query:

    SELECT DISTINCT 
        name 
    FROM 
        departments 
    ORDER BY 
        name
    

    You're not using anything from the directory table, so you don't need to take anything from it.

    If you've got multiple departments, try:

    echo "<li><a href=\"dept.php?dept=$info[name]\">$info[name]</a></li>
    ";
    

    And then updating your department code to search for employees in the department with that name.

    Department code:

    $theSQL = "SELECT * FROM directory WHERE department IN (SELECT id FROM departments WHERE name='$department') ORDER BY Lastname");
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 Vue3 大型图片数据拖动排序
  • ¥15 划分vlan后不通了
  • ¥15 GDI处理通道视频时总是带有白色锯齿
  • ¥20 用雷电模拟器安装百达屋apk一直闪退
  • ¥15 算能科技20240506咨询(拒绝大模型回答)
  • ¥15 自适应 AR 模型 参数估计Matlab程序
  • ¥100 角动量包络面如何用MATLAB绘制
  • ¥15 merge函数占用内存过大
  • ¥15 使用EMD去噪处理RML2016数据集时候的原理
  • ¥15 神经网络预测均方误差很小 但是图像上看着差别太大