0

I'm having some trouble with recursive functions in PHP. I'm creating a function that creates a link based in categories in database. The table is created as infinite level with "parent_id" to check what's the parent ID of that category.

I've also included a field "category_link" that creates the link. My problem resides into creating the link system with recursive function.

For example I have the category: Cars (ID 1) (link /cars) -- Volvo (ID 2) (link /volvo) -- Audi (ID 3) (link /audi)

So, if I build a function to create the link for ID2, the final link is supposed to be /cars/volvo. My function is only retrieving the last one, /volvo instead /cars/volvo

function GetSlug($mysqli)
{
    if (isset($_POST)) {
        $cat = validar($mysqli, $_POST['card_category']);
        if ($stmt = $mysqli->prepare("SELECT * FROM categories WHERE id = ? LIMIT 1")) {
            $stmt->bind_param("i", $cat);
            $stmt->execute() or trigger_error($stmt->error, E_USER_ERROR);
            ($stmt_result = $stmt->get_result()) or trigger_error($stmt->error, E_USER_ERROR);
            if ($stmt_result->num_rows > 0) {
                $category = array(
                    'categories' => array(),
                    'parent_cats' => array()
                );
                while ($row_data = $stmt_result->fetch_assoc()) {
                    $parent = $row_data['parent_id'];
                    //creates entry into categories array with current category id ie. $categories['categories'][1]
                    $category['categories'][$row_data['id']] = $row_data;
                    //creates entry into parent_cats array. parent_cats array contains a list of all categories with children
                    $category['parent_cats'][$row_data['parent_id']][] = $row_data['id'];
                }
                echo buildcat($parent, $category);
            } else {
                return false;
            }
        }
    }
}

And the second function:

function buildcat($parent, $category)
{
    $html = "";
    if (isset($category['parent_cats'][$parent])) {
        foreach ($category['parent_cats'][$parent] as $cat_id) {
            if (!isset($category['parent_cats'][$cat_id])) {
                $html .= $category['categories'][$cat_id]['category_link'] . "<br/>";
            }
            if (isset($category['parent_cats'][$cat_id])) {
                $html .= $category['categories'][$cat_id]['category_link'];
                $html .= buildcat($cat_id, $category);
            }
        }
    }
    return $html;
}

I guess my problem is because I'm getting only the info from DB for selected category ID, instead of getting all categories with parent_id that belongs to that category. I'm having headaches with this. Can you please help me? Thanks!

feeela
  • 29,399
  • 7
  • 59
  • 71
Tiago
  • 625
  • 5
  • 16
  • Yes, you will have to pull the information on all categories to use a recursive function in PHP to build a complete tree out of the flat data. – feeela Sep 19 '18 at 09:34
  • Can you point me in the right direction please? All this recursive things is driving me crazy. – Tiago Sep 19 '18 at 09:35
  • Can you `var_dump` the `$category` var in your `buildcat` and edit your question so we could have an example ? – Goufalite Sep 19 '18 at 09:37
  • For each level maintain the parent slug information in a variable (say for example $path) and keep appending current category slug ($path . $current_category_slug), update $path variable whenever you enter new child inside the recursive function. – Gaurav Mehra Sep 19 '18 at 09:38
  • Possible duplicate of [Build a tree from a flat array in PHP](https://stackoverflow.com/questions/8840319/build-a-tree-from-a-flat-array-in-php) – feeela Sep 19 '18 at 09:41
  • Pointing to the right direction: [Converting a Flat Array With Parent ID's to a Nested Tree](http://blog.tekerson.com/2009/03/03/converting-a-flat-array-with-parent-ids-to-a-nested-tree/) – feeela Sep 19 '18 at 09:42
  • My problem is that my function is supposed to start in somewhere as parent ID is 0 but it starts with the final category. I need to develop something that could go from child to root instead root to child as I have now. Oh god... – Tiago Sep 19 '18 at 09:59

0 Answers0