How To Group Data By Category And Display In HTML Table Using PHP

How To Group Data By Category And Display In HTML Table Using PHP

How To Group Data By Category And Display In HTML Table Using PHP

This post will show you how to create   a program that will  generate  an individual table  for each company with employees associated with it.

Files involve

1.This sample contains 1 Files  (1)displayByCompany.php

 

Prior to that you need to complete  the items listed Below

  1. Mamp or xampp with phpMyAdmin install into your Computer , check out here
  2.  A Sample Database created Check out Here
  3.  New User Created . Check out Here
  4. A New table created , check out Here
  5. Insert Data into the Table Here

 

 

displayByCompany.php

 


<?php
/*

*	File:		displayByCompany.php

*/


// Connect DB

$hostName  ="localhost";
$userName ="jane";
$userPassword ="jane";
$database ="janedb";
$comArray = array(); 
$orderClause ='';
$tempcompany ='';
$comArray = array();


$dbConnectionStatus  = new mysqli($hostName, $userName, $userPassword,$database);

//Connection Error
if ($dbConnectionStatus->connect_error){     

        
     die("Connection failed: " . $dbConnectionStatus->connect_error);

}
// Connected to Database JaneDB
// Object oriented  -> pointing 
if($dbConnectionStatus->query("SELECT DATABASE()")){
	
	$dbSuccess =true;
	//
	$result = $dbConnectionStatus->query("SELECT DATABASE()");
	$row = $result->fetch_row();
    printf("Default database is %s.\n", $row[0]);
    $result->close();

    
	
	
}



// DB Connect Successful

if ($dbSuccess) {

 //  -------------------- Style declarations---------------------------------------------------------

			
			$textFont = 'style = " font-family: arial, helvetica, sans-serif; "';

			
			$indent50 = 'style = " margin-left: 50; "';
			$indent100 = 'style = " margin-left: 100; "';
 //  ----------------------------------------------------------------------------------------------------
 
 
echo '<h1>All Personnel Segregated By Company</h1>';

//-------------------------- Select Company Querries-------------------------------------------------------




$selectCompany = "SELECT testtable.id ,testtable.firstname,testtable.lastname,testtable.email,testtable.companyname FROM testtable ORDER BY testtable.companyname,testtable.id ";
$selectCompany_Query = mysqli_query($dbConnectionStatus,$selectCompany );
$companyArray =array();
	
// Loop Through Company Records

 while($rowsCompany=mysqli_fetch_assoc($selectCompany_Query)){
	 
	             // Get the Company Name/ id
	           
			 $companyName =$rowsCompany['companyname'];
			 
			 
			 // Check whether the Company Table is Created f No Create the Table
			  if(! in_array($companyName,$comArray)){
							 array_push($comArray,$companyName);
							 
						             echo '<h2 '.$indent50.'>'.$companyName .'</h2>';
									        echo '<div '.$indent100.'>';
											     echo "<table border='1'>";
												 
												   echo "<tr>";
												   
															echo "<td>id</td>";
															echo "<td>First Name</td>";
															echo "<td>Last Name</td>";
															echo "<td>Email </td>";
															echo "<td>Company</td>";
															
													 echo "</tr>"; 
															
															 //-----------------Add Row into the Selected Company --------------------------------
															 
															  $selectPerson = "SELECT * FROM testtable WHERE companyname = '$companyName' ";
															  $selectPerson_Query = mysqli_query($dbConnectionStatus,$selectPerson);
															  $arrayPerson = array();
															  while($personrows=mysqli_fetch_assoc($selectPerson_Query )){
																  
				                                                                               $arrayPerson[] = $personrows; 

															  }
															  
															
											  
															  foreach($arrayPerson as $data){
																               
		
																				echo'<tr>';
																			   // Search through the array print out value if see the Key  eg: 'id', 'firstname ' etc.
																				echo'<td>'.$data['id'].'</td>';
																				echo'<td>'.$data['firstname'].'</td>';
																				echo'<td>'.$data['lastname'].'</td>';
																				echo'<td>'.$data['email'].'</td>';
																				echo'<td>'.$data['companyname'].'</td>';
																			 
																			 
																			    echo'</tr>';
																				
																				
																				
																				
															
																			}
															  
															
															
															
															
															
															
															
								   
												            //-------------------------------------------------------------------------------------
												   
												  echo "</table>";
		                                      echo '</div>';
							 
							 
							 
							
							 
							 
						 }
			 
					  
			
			
				
 }			
				
	

                

    echo '</div>';
		


 




}


?>


 

Leave a Reply

Your email address will not be published. Required fields are marked *

18 − 7 =