good greeting
I have a PHP query that brings data, name, and department into a table, how can I split into tables by departments, which means each department is in a table
$employee_qry=$conn->query("SELECT * FROM `employee` ") or die(mysqli_error());
while($row=$employee_qry->fetch_array()){
<tr>
<td><?php echo $row['idnumber']?></td>
<td><?php echo $row['EmpleName']?></td>
<td><?php echo $row['Nationality']?></td>
2
Answers
Given a basic table schema and dummy data as follows:
The task then of displaying the employees in separate tables depending upon the department in which they work is straightforward if you create an array to hold department names. Using
in_array
as a test you can determine if the current record within the while loop requires to be placed in a new table if yousort
the table, in the sql query, by department.The above then yields (inline styles added for clarity) – view the snippet for rendered html
First you can fetch each unique department via GROUP BY or DISTINCT. And store it in Array.
After that run foreach() loop and run query in foreach() loop and create table.