I am currently working on a project that requires me to take an uploaded spreadsheet file (XLSX, XLS, etc), place it into an array, then based off of the parent child relationship, build an HTML table structured to display the MLM referral setup.
For example:
First Name | Last Name | Parent | Child
John Johnson 0 1
Mary Sue 1 2
Harold Fineberg 1 3
Gerald George 2 4
(Sorry about the lame structuring to explain this)
I want to show whoever is a child of someone, their information is placed in a table relative to the parent.
I have attempted using PhpSpreadsheet but to no avail. I am not using a database as this is going to be a simple upload your spreadsheet and have it display in a table with the respective relationships grouped together.
Any assistance would be much appreciated as this is a first time post for me, as well as a first time MLM tree/excel project!
<?php
require 'vendor/autoload.php';
use PhpOffice\PhpSpreadsheet\Spreadsheet;
use PhpOffice\PhpSpreadsheet\Writer\Xlsx;
if(isset($_POST['upload'])) {
$filename = $_FILES['sheet']['tmp_name'];
//$data = csv_to_array($filename);
$spreadsheet = PhpOffice\PhpSpreadsheet\IOFactory::load($filename);
$worksheet = $spreadsheet->getActiveSheet();
$rows = [];
foreach ($worksheet->getRowIterator() AS $row) {
$cellIterator = $row->getCellIterator();
$cellIterator->setIterateOnlyExistingCells(FALSE); // This loops through all cells,
$cells = [];
foreach ($cellIterator as $cell) {
$cells[] = $cell->getValue();
}
$rows[] = $cells;
}
// echo '<pre>';
// print_r($rows);
// echo '</pre>';
// $output = fopen('test.xlsx', 'w');
// foreach ($rows as $file) {
// $result = [];
// array_walk_recursive($file, function($item) use (&$result) {
// $result[] = $item;
// });
// }
// $writer = \PhpOffice\PhpSpreadsheet\IOFactory::createWriter($result, "Xlsx");
// $writer->save("05featuredemo.xlsx");
function buildTree(array $elements, $parentId = 0) {
$branch = array();
foreach ($elements as $element) {
if ($element[0] == $parentId) {
$children = buildTree($elements, $element[0]);
if ($children) {
$element[1] = $children;
}
$branch[] = $element;
}
}
return $branch;
}
$tree = buildTree($rows);
}
?>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<title>Page Title</title>
<meta name="viewport" content="width=device-width, initial-scale=1">
</head>
<body>
<div class="form-container">
<form enctype="multipart/form-data" method="POST">
<div class="form-box">
<input type="file" name="sheet" id="sheet" accept=".xls,.xlsx">
</div>
<div align="center">
<input type="submit" name="upload" value="Upload & Convert">
</div>
</form>
</div>
</body>
</html>
This is what I've been working with. I have found out how to place it into an array nicely, but I cant seem to convert it into a linear treeview :
Parent
Child
Child
Child
Child
Child
Child
The commented code is things I have tried but haven't deleted due to possible usefulness. If I don't need to use PhpSpreadhseet that'd be great, but I can compromise.