douweng7233 2015-05-19 13:50
浏览 68
已采纳

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

Currently creating my own breeding website, but I am struggling with retrieving, processing and showing the pedigree of a given dog. I can do it if I pretty much hard-code it but not dynamically by retrieving parents (and parents of parents, etc...) until parents aren't found anymore.

MYSQL is basic:

tbl_dogs (table) - dog_id - dog_name - dog_sex - mother_id - father_id

The idea is to retrieve parents of one dogs, then parents of each dog in the tree, until mother_id/father_id aren't specified.

I am completely lost, so far I have been doing it the hard-coded way:

    $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"]);

And I then would just use my family tree HTML5 structure to display this data.

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

But this solution, although working, is not at all suited to my needs since it is not flexible at all. I want to build a function that will go through as many generations as I will ask for (if they exist) and put that into an Array that I will then output through nested HTML lists as done above.

This "logical loop" to retrieve all parents is driving me insane, any help would be appreciated.. Thanks a million!

  • 写回答

3条回答 默认 最新

  • dqst96444 2015-05-19 14:05
    关注

    It's not that big deal. So basically you check if a dog has a mother and/or a father and execute the function again for each dog.

    $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;
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 做个有关计算的小程序
  • ¥15 MPI读取tif文件无法正常给各进程分配路径
  • ¥15 如何用MATLAB实现以下三个公式(有相互嵌套)
  • ¥30 关于#算法#的问题:运用EViews第九版本进行一系列计量经济学的时间数列数据回归分析预测问题 求各位帮我解答一下
  • ¥15 setInterval 页面闪烁,怎么解决
  • ¥15 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历
  • ¥15 TLE9879QXA40 电机驱动
  • ¥20 对于工程问题的非线性数学模型进行线性化