递归循环创建一个家谱? (PHP / MySQL的/ HTML)

时间:2015-05-19 13:50:25

标签: php html mysql arrays loops

目前正在创建我自己的育种网站,但我正在努力检索,处理和显示给定狗的血统。我可以做到这一点,如果我非常难以编码,但不能通过检索父母(以及父母的父母等等)动态,直到父母再也找不到为止。

MYSQL是基本的:

tbl_dogs (表格)   - dog_id   - dog_name   - dog_sex   - mother_id   - father_id

这个想法是检索一只狗的父母,然后检索树中每只狗的父母,直到没有指定mother_id / father_id。

我完全迷失了,到目前为止,我一直在用硬编码的方式做到这一点:

<div class="pedigree-tree">
  <ul>
    <li><span><a href="<?php echo $mother["permalink"] ?>"><?php echo $mother["name"]; ?></a></span>
      <ul>
        <li><span><a href="<?php echo $mother_mother["permalink"] ?>"><?php echo $mother_mother["name"] ?></a></span>
          <ul>
            <li>
                <span>Grand-Grandmother</span>
                <ul>
                    <li><span><a href="<?php echo $mother_mother["permalink"] ?>"><?php echo $mother_mother["name"] ?></a></span></li>
                    <li><span><a href="<?php echo $mother_mother["permalink"] ?>"><?php echo $mother_mother["name"] ?></a></span></li>
                </ul>
            </li>
            <li><span>Grand-Grandfather</span></li>
          </ul>
        </li>
        <li><span><a href="<?php echo $mother_father["permalink"] ?>"><?php echo $mother_father["name"] ?></a></span>
          <ul>
            <li><span>Entry-1-2-1</span></li>
            <li><span>Entry-1-2-1</span></li>
          </ul>
        </li>
      </ul>
    </li>
    <li><span><a href="<?php echo $father["permalink"] ?>"><?php echo $father["name"]; ?></a></span>
      <ul>
        <li><span><a href="<?php echo $father_mother["permalink"] ?>"><?php echo $father_mother["name"] ?></a></span>
          <ul>
            <li><span>Entry-1-1-1</span></li>
            <li><span>Entry-1-1-1</span></li>
          </ul>
        </li>
        <li><span><a href="<?php echo $father_father["permalink"] ?>"><?php echo $father_father["name"] ?></a></span>
          <ul>
            <li><span>Entry-1-2-1</span></li>
            <li><span>Entry-1-2-1</span></li>
          </ul>
        </li>
      </ul>
    </li>
  </ul>
</div>

然后我会使用我的家谱树HTML5结构来显示这些数据。

save_screenshot(filename,:full => true)

但是这个解决方案虽然有效,但根本不适合我的需求,因为它根本不灵活。我想构建一个函数,它将经历我将要求的多代(如果它们存在)并将其放入一个数组中,然后我将通过嵌套的HTML列表输出。如上所述。

这个&#34;逻辑循环&#34;检索所有的父母是让我疯了,任何帮助将不胜感激..万分感谢!

3 个答案:

答案 0 :(得分:1)

这没什么大不了的。所以基本上你检查一只狗是否有一个母亲和/或一个父亲,并为每只狗再次执行该功能。

$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;
}

答案 1 :(得分:0)

基本理念是这样的:

function getDog ($id)
{
    // do your lookup

    if (isset($dog->father_id) && !empty($dog->father_id)) {
        $dog->father = getDog($dog->father_id);
    }

    if (isset($dog->mother_id) && !empty($dog->mother_id)) {
        $dog->mother = getDog($dog->mother_id);
    }

    return $dog;
}

它不漂亮,你需要添加逻辑来找到合适的祖先名字,但是你可以使用getDog(123)->father->father

找到一只狗的祖父

答案 2 :(得分:-1)

可能的解决方案的另一个起点:

{{1}}

由您来保护sql代码,您可以为父亲重复这个功能&#39;树或将母亲和父亲结合在同一个职能中。