Recursive loop to create a family tree? (PHP/MySQL/HTML) - Stack Overflow

PHOTO EMBED

Wed Dec 02 2020 05:05:37 GMT+0000 (Coordinated Universal Time)

Saved by @mvieira

$stmnt = $pdo->prepare("
    SELECT dog_id, dog_name, dog_sex, mother_id, father_id
    FROM tbl_dogs
    WHERE dog_id = ?
");

fetchDogRecursive($yourDogId);

function fetchDogRecursive($dogId)
{
    $stmnt->execute(array($dogId));

    $dogData = $stmnt->fetchAll(PDO::FETCH_ASSOC)[0];

    $dog = array(
        'id' => $dogData['dog_id'],
        'name' => $dogData['dog_name'],
        'mother' => null,
        'father' => null
    );

    if($dogData['mother_id'] !== null) {
        $dog['mother'] = fetchDogRecursive($dogData['mother_id']);
    }

    if($dogData['father_id'] !== null) {
        $dog['father'] = fetchDogRecursive($dogData['father_id']);
    }

    return $dog;
}
content_copyCOPY

https://stackoverflow.com/questions/30327638/recursive-loop-to-create-a-family-tree-php-mysql-html