// This function prints the data out in the command line in a grid format pleasing to humans. // This is where the spaceLength function comes into play. function printArrayInCommandLine($contentsArray) { $textLines = ''; $unitsTitleSpace = spaceLength(8, 'Units'); $fullnameTitleSpace = spaceLength(35, 'Full Name'); foreach ($contentsArray as $key => $value) { $firstSpace = spaceLength(8, $value['units']); $secondSpace = spaceLength(35, $value['full name']); $thirdSpace = spaceLength(3, $value['employee number']); $textLines .= "{$value['units']}{$firstSpace}| {$value['full name']}{$secondSpace}| {$value['employee number']}{$thirdSpace}" . PHP_EOL; } return "Units{$unitsTitleSpace}| Full Name{$fullnameTitleSpace}| Employee Number" . PHP_EOL . $textLines; } // Each function is called and saved as $contentsArray to cut down on variables. // Each time the updated version of the array is saved over the previous array of the same name. $contentsArray = readTheFile('data/reports.txt'); $contentsArray = separateArray($contentsArray); $contentsArray = reorderArray($contentsArray); // This portion prints out human pleasing sentences of each variable. $numOfEmployees = countEmployees($contentsArray); echo "There are {$numOfEmployees} employees." . PHP_EOL; $sumOfAllUnits = addAllUnits($contentsArray); echo "The total number of units sold was {$sumOfAllUnits}." . PHP_EOL; $averageUnitsSold = averageOfUnitsSold($sumOfAllUnits, $numOfEmployees); echo "The average number of units sold by each employee was {$averageUnitsSold}." . PHP_EOL; // This portion sorts the array by the employee array units value. $contentsArray = sortByUnits($contentsArray); // And this portion prints out the resulting array into a human pleasing format in the command line. echo printArrayInCommandLine($contentsArray);
$stringValueWithSpaces = $stringValue; for ($i = 0; $i < $numberOfSpaces; $i++) { $stringValueWithSpaces .= " "; } return $stringValueWithSpaces; } echo "====================================================================" . PHP_EOL; //grabs text file and renames as a variable $unitsSorted = parseNames('parsecsv.txt'); //count employees function countEmployees($array) { return count($array); } //gets the number of employees to count from line 51- $unitsSorted echos it out to HRO- human readable output $employeeCount = countEmployees($unitsSorted); echo "There are {$employeeCount} total employees working." . PHP_EOL; //adds units sold from all employees together function soldUnits($array) { $sumOfUnitsSold = 0; foreach ($array as $key => $value) { $sumOfUnitsSold = $value['Units_Sold'] + $sumOfUnitsSold; } return $sumOfUnitsSold; } $unitsSold = soldUnits($unitsSorted); echo "The company sold {$unitsSold} total products." . PHP_EOL; //gets the avg. of untis sold/employees function avgUnitsSold($IntUnitsSold, $IntEmployeeCount) {
?> <tr> <td><?php print $company['name']; ?> </td> <td><?php print $company['address']; ?> </td> <td> <?php print $company['city']; ?> </td> <?php $count = countEmployees($company['employees']); ?> <td><?php print $count; ?> </td> <?php $TotalEmployees += $count; ?> <td> <?php foreach ($company['employees'] as $employee) { print capName($employee . " "); } ?> <?php