Happy new year!:)
I'm fairly new to PHP and i'm trying to write a function to get the childs of the parent categories, and re use that function again to get the childs of childs categories.
However, it is not working as I was hoping, and I can't really figure out why. I hope you have a minute to help me out here.
Thank you! Roy
$Parents =
Array
(
[0] => Array
(
[category_id] => 3
[parent_id] => 1
[name] => PC Onderdelen
[is_active] => 1
[position] => 2
[level] => 1
)
[1] => Array
(
[category_id] => 11
[parent_id] => 1
[name] => test
[is_active] => 1
[position] => 3
[level] => 1
)
[2] => Array
(
[category_id] => 14
[parent_id] => 1
[name] => Rootcat3
[is_active] => 1
[position] => 4
[level] => 1
)
)
The function
public function getchilds($parents)
{
$subcategories = array();
foreach ($parents as $parent)
{
$parentid = $parent['category_id'];
$sql = "SELECT * FROM categories WHERE parent_id = $parentid";
$db = new DB ('novacpos');
$result = $db->query($sql);
while ($rows = mysqli_fetch_assoc($result))
{
$subcategories[] = $rows;
}
$parent["children"] = $subcategories;
unset ($subcategories);
$parents1[] = $parent;
}
return $parents1;
}
The result
Array
(
[0] => Array
(
[category_id] => 3
[parent_id] => 1
[name] => PC Onderdelen
[is_active] => 1
[position] => 2
[level] => 1
[children] => Array
(
[0] => Array
(
[category_id] => 4
[parent_id] => 3
[name] => Moederborden
[is_active] => 1
[position] => 2
[level] => 2
)
[1] => Array
(
[category_id] => 6
[parent_id] => 3
[name] => Behuizingen
[is_active] => 1
[position] => 1
[level] => 2
)
[2] => Array
(
[category_id] => 8
[parent_id] => 3
[name] => Laptops
[is_active] => 1
[position] => 3
[level] => 2
)
[3] => Array
(
[category_id] => 9
[parent_id] => 3
[name] => Muizen
[is_active] => 1
[position] => 4
[level] => 2
)
)
)
[1] => Array
(
[category_id] => 11
[parent_id] => 1
[name] => test
[is_active] => 1
[position] => 3
[level] => 1
[children] => Array
(
[0] => Array
(
[category_id] => 13
[parent_id] => 11
[name] => Test2
[is_active] => 1
[position] => 1
[level] => 2
)
)
)
[2] => Array
(
[category_id] => 14
[parent_id] => 1
[name] => Rootcat3
[is_active] => 1
[position] => 4
[level] => 1
[children] => Array
(
[0] => Array
(
[category_id] => 15
[parent_id] => 14
[name] => Extracat1
[is_active] => 1
[position] => 1
[level] => 2
)
)
)
)
Result of print_r ($parent["children"]);
Array
(
[0] => Array
(
[category_id] => 4
[parent_id] => 3
[name] => Moederborden
[is_active] => 1
[position] => 2
[level] => 2
)
[1] => Array
(
[category_id] => 6
[parent_id] => 3
[name] => Behuizingen
[is_active] => 1
[position] => 1
[level] => 2
)
[2] => Array
(
[category_id] => 8
[parent_id] => 3
[name] => Laptops
[is_active] => 1
[position] => 3
[level] => 2
)
[3] => Array
(
[category_id] => 9
[parent_id] => 3
[name] => Muizen
[is_active] => 1
[position] => 4
[level] => 2
)
)
Array
(
[0] => Array
(
[category_id] => 13
[parent_id] => 11
[name] => Test2
[is_active] => 1
[position] => 1
[level] => 2
)
)
Array
(
[0] => Array
(
[category_id] => 15
[parent_id] => 14
[name] => Extracat1
[is_active] => 1
[position] => 1
[level] => 2
)
)
So I thought: (but not working)
public function getchilds($parents)
{
$subcategories = array();
foreach ($parents as $parent)
{
$parentid = $parent['category_id'];
$sql = "SELECT * FROM categories WHERE parent_id = $parentid";
$db = new DB ('novacpos');
$result = $db->query($sql);
while ($rows = mysqli_fetch_assoc($result))
{
$subcategories[] = $rows;
}
$parent["children"] = $subcategories;
unset ($subcategories);
getchilds($parent["children"]); <-----------
$parents1[] = $parent;
}
return $parents1;
}
class myModel{
public function get_categories(){
$sql = "SELECT * FROM categories";
$db = new DB ('novacpos'); // You should use a singleton pattern for this
$result = $db->query($sql);
$categories = array();
while ($rows = mysqli_fetch_assoc($result))
{
$categories[] = $rows;
}
return $categories;
}
public function organize_categories($categories) {
$resultParents = array();
$resultChildren = array();
foreach ($categories as $row) {
if(empty($row['parent']))
$resultParents[] = $row;
else
$resultChildren[] = $row;
}
foreach ($resultParents as &$parent){
$parent['children'] = $this->_organize_category($resultChildren, $parent['category_id']);
}
unset($parent);
return $resultParents;
}
private function _organize_category($resultChildren, $parent) {
$children = array();
foreach ($resultChildren as $child) {
if ($child['parent_id'] == $parent){
$child['children'] = $this->_organize_category($resultChildren, $child['category_id']);
$children[] = $child;
}
}
return $children;
}
}
Finally :
$categories = $myModel->get_categories();
$organized_categories = $myModel->organize_categories($categories);
var_dump($organized_categories);
It's implemented in procedural style, but it works, just rewrite it as you need
$db = new mysqli('localhost', 'root', 'mysql', 'test');
$parents = [];
$result = $db->query('SELECT * FROM categories WHERE category_id IN (0,1)');
while ($row = $result->fetch_assoc())
{
$parents[] = $row;
}
function getchilds($parents, $db)
{
foreach ($parents as &$parent)
{
$result = $db->query("SELECT * FROM categories WHERE parent_id = {$parent['category_id']}");
if ($result && $result->num_rows > 0)
{
while ($row = $result->fetch_assoc())
{
$parent['children'][] = $row;
}
$parent['children'] = getchilds($parent['children'], $db);
}
}
return $parents;
}
echo '<pre>';
print_r(getchilds($parents, $db));
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.