Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8446081
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T09:49:59+00:00 2026-06-10T09:49:59+00:00

I am sorry about the title as I am not sure whether my problem

  • 0

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>\n";
echo "<ul>\n";

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>\n";
}

echo "</ul>\n\n";

}

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>\n\n";
            }

            echo "<h3><a href=\"$info[dept_url]\">$info[dept_name]</a></h3>\n\n";
            echo "<table id=\"directory_table\">\n";
            echo "<tr>\n";
            echo "<th>Name</th>\n";
            echo "<th>E-mail</th>\n";
            echo "<th>Phone</th>\n";
            echo "<th>Office</th>\n";
            echo "<th>Title</th>\n";
            echo "</tr>\n";
        }


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

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

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

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

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

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

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

        $last_dept = $info[dept_name];
    }

    echo "</table>\n\n";

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

}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-10T09:50:01+00:00Added an answer on June 10, 2026 at 9:50 am

    If you’re only using these two fields:

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

    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>\n";
    

    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");
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

First of all, sorry about that title. I'm not the best at writing those
sorry about the title, I'm not sure how to even describe this, which makes
Sorry about the title, not sure how to describe without example. I trying to
I'm sorry about the title I'm just not sure how to describe this one.
Sorry about the title, I'm not exactly sure how to explain this with a
Sorry if the title is not very clear. I was not sure about the
Sorry about terrible title, not sure of a better one. Here's a gross simplification
Sorry for the not very descriptive question title. I'm not very sure how to
SQL Server 2008: Sorry for the possibly non-informative title but I'm not sure quite
First of all, sorry about the title -- I couldn't figure out one that

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.