What are some potential pitfalls when trying to create a navigation structure from MySQL data in PHP?

One potential pitfall when creating a navigation structure from MySQL data in PHP is not properly handling the hierarchical nature of the data. To solve this issue, you can use a recursive function to loop through the data and build the navigation structure accordingly.

function buildNavigation($parent_id, $data) {
    $html = '<ul>';
    
    foreach ($data as $item) {
        if ($item['parent_id'] == $parent_id) {
            $html .= '<li>' . $item['name'];
            
            // Check if item has children
            $children = array_filter($data, function($child) use ($item) {
                return $child['parent_id'] == $item['id'];
            });
            
            if (!empty($children)) {
                $html .= buildNavigation($item['id'], $data);
            }
            
            $html .= '</li>';
        }
    }
    
    $html .= '</ul>';
    
    return $html;
}

// Assuming $data is an array of rows fetched from MySQL
echo buildNavigation(0, $data);