I’m trying to retrieve the category path including parent categories too
I’m trying to use the following code to retrieve the current category and parent categories further up the tree to display categories in a breadcrumb format such as:
TV / LED / 4K / 50 inch
<?php
function retrieve_category_path($id){
// DB connection
require("../../config/db/connection.php");
// Company ID
$company_id = 1
$stmt = $conn->prepare("SELECT id, name, parent, position FROM category WHERE company_id = ? AND id = ?");
$stmt->bind_param("ii", $company_id, $id);
$stmt->execute();
$result = $stmt->get_result();
$path = array();
while($row=$result->fetch_assoc()){
$parent =$row['parent'];
$name = $row['name'];
$position = $row['position'];
$position_seperator = '::';
$seperator = '/';
if($parent > 0){
$path[] = array($name, $position_seperator, $position, $seperator); // Category 1::2/Category 2::3/Category 3::1/Category 4::0/
$path = array_merge(retrieve_category_path($row['parent']), $path);
} else {
$path = array($name, $position_seperator, $position, $seperator); // Category 1::2/Category 2::3/Category 3::1/Category 4::0/
}
}
return $path;
}
?>
However the returned data for categories that have more than one category are displaying as an array rather than a string
Using
$get_data = retrieve_category_path($data); //$data is the category ID i use to send to the function
$category_path = implode($get_data);
Results in:
TV/ArrayArrayArray
Rather than
TV / LED / 4K / 50 inch
Please could someone point out where i’m going wrong, i’m guessing its in the $path section of the function