これを再帰関数にするにはどうすればよいですか。選択した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;
}