目前正在创建自己的繁殖网站,但我正在努力检索,处理和展示给定犬只的血统书。如果我几乎对它进行硬编码,但不能动态地做到这一点,则可以通过检索父母(和父母的父母等)直到不再找到父母来实现。

MYSQL是基本的:

tbl_dogs(表)
 -dog_id
 -dog_name
 -dog_sex
 -mother_id
 -父亲编号

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

我完全迷路了,到目前为止,我一直在使用硬编码方式:

    $mother["id"] = get_post_meta(get_the_ID(), "_mother_id", true);
    $mother["name"] = get_animal_name($mother["id"]);
    $mother["permalink"] = get_the_permalink($mother["id"]);

        $mother_mother["id"] = get_mother_id($mother["id"]);
        $mother_mother["name"] = get_animal_name($mother_mother["id"]);
        $mother_mother["permalink"] = get_the_permalink($mother_mother["id"]);

        $mother_father["id"] = get_father_id($mother["id"]);
        $mother_father["name"] = get_animal_name($mother_father["id"]);
        $mother_father["permalink"] = get_the_permalink($mother_father["id"]);

    $father["id"] = get_post_meta(get_the_ID(), "_father_id", true);
    $father["name"] = get_animal_name($father["id"]);
    $father["permalink"] = get_the_permalink($father["id"]);

        $father_mother["id"] = get_mother_id($father["id"]);
        $father_mother["name"] = get_animal_name($father_mother["id"]);
        $father_mother["permalink"] = get_the_permalink($father_mother["id"]);

        $father_father["id"] = get_father_id($father["id"]);
        $father_father["name"] = get_animal_name($father_father["id"]);
        $father_father["permalink"] = get_the_permalink($father_father["id"]);


然后,我只需要使用家谱HTML5结构来显示此数据。

<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>


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

检索所有父母的这种“逻辑循环”使我发疯,任何帮助将不胜感激。。谢谢百万!

最佳答案

没什么大不了的。因此,基本上,您要检查狗是否有母亲和/或父亲,然后为每只狗再次执行该功能。

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

08-08 06:00