title: Efficient Method To Display Group Title Once — PHP/MySQL/jQuery
body: <p>There are three tables in MySQL: Employees, Branches, Departments. I need information to appear in the following way:</p>
  
  <blockquote>
    <p><strong>ATLANTA Branch</strong><br> <i>Delivery Department</i><br> Phillip J. Fry<br> Phone: 123456</p>
    
    <p><i>Engineering Department</i><br> Turanga Leela<br> Phone: 123457<br>
    <br> Bender Rodriguez<br> Phone: 123458<br></p>
  </blockquote>
  
  <p><br>
  The simple PHP code currently:<br>
  1) Takes rows from three tables (simple SELECT query with JOIN)<br>
  2) Puts them in row (mysql_fetch_assoc)<br>
  3) Displays using the PHP While loop<br>
  <br>The result is then like this:<br></p>
  
  <blockquote>
    <p><strong>ATLANTA Branch</strong><br> <i>Delivery Department</i><br> Phillip J. Fry<br> Phone: 123456</p>
    
    <p><strong>ATLANTA Branch</strong><br> <i>Engineering Department</i><br> Turanga Leela<br> Phone: 123457<br> <br>
    <strong>ATLANTA Branch</strong><br> <i>Engineering Department</i><br> Bender Rodriguez<br> Phone: 123458<br></p>
  </blockquote>
  
  <p>What technique (JS, jQuery, Ajax) or method can you recommend so I can pull row information using only one query and not duplicate the Branch name and Department name?
  <br>
  <br>
  <strong>UPDATE:</strong> If I put the branch name outside the loop (using While loop), there would be multiple loops: 1) To get a branch, 2) To get a department, 3) To get all employees in that department. Loop.
  <br>
  <br>
  <strong>UPDATE:</strong> Sharing the code:
  <br></p>
  
  <pre><code>&lt;?php
  // Create connection
  $connection = mysql_connect('localhost','root', '') or die('Connection error.');
  mysql_query("SET NAMES 'utf8'", $connection);
  mysql_select_db("eReference");
  
  // Check Employees
  $query = "SELECT Employees.fName, Employees.lName, Department.deptName, Branch.branchName, ".
  "FROM Employees ".
  "LEFT JOIN Department ".
  "ON Employees.department = Department.id ".
  "LEFT JOIN Branch ".
  "ON Employees.branch = Branch.id ;";
  
  $result = mysql_query($query, $connection) or die(mysql_error());
  
  while ($row = mysql_fetch_assoc($result)) {
  
  ?&gt;
  
  &lt;h2&gt;&lt;?php echo $row['branchName']; ?&gt;&lt;/h2&gt;
  &lt;?php if ($row['deptName']) echo "&lt;h3&gt;" . $row['deptName'] . "&lt;/h3&gt;"; ?&gt;
  &lt;h4&gt;&lt;?php echo $row['fName'] . " " . $row['lName']; ?&gt;&lt;/h4&gt;&lt;/p&gt;
  &lt;?php
  }
  ?&gt;
  </code></pre>