Skip to content
PHP Logo

What are some common challenges when generating an organigram using PHP and database data?

One common challenge when generating an organigram using PHP and database data is properly structuring the data in a way that represents the hierarchical relationships between different entities. One way to solve this is by using a recursive function to traverse through the data and build the organigram accordingly.

php
function buildOrganigram($parent_id, $data) {
    $html = '<ul>';
    
    foreach ($data as $row) {
        if ($row['parent_id'] == $parent_id) {
            $html .= '<li>' . $row['name'];
            $html .= buildOrganigram($row['id'], $data);
            $html .= '</li>';
        }
    }
    
    $html .= '</ul>';
    
    return $html;
}

// Assuming $data is an array of rows fetched from the database
echo buildOrganigram(0, $data);