我怎么能把它变成一个递归函数。这样它会获得所选 parent_id 的所有类别的列表?
public function getProductCategoryPath($product_id) {
$query = $this->db->query("SELECT category_id FROM " . DB_PREFIX . "product_to_category WHERE product_id = '" . (int)$product_id . "' LIMIT 1");
if($query->row['category_id']){
// Parent category
$path = $query->row['category_id'];
// Category parent
$query = $this->db->query("SELECT parent_id FROM " . DB_PREFIX . "category WHERE category_id = '" . $query->row['category_id'] . "' AND parent_id != 0");
if($query->num_rows){
$path = $query->row['parent_id'] . "_" . $path;
// Sub-category parent
$query = $this->db->query("SELECT parent_id FROM " . DB_PREFIX . "category WHERE category_id = '" . $query->row['parent_id'] . "' AND parent_id != 0");
if($query->num_rows){
$path = $query->row['parent_id'] . "_" . $path;
}
}
return $path;
}
return false;
}