Pedigree/Family tree chart from database

寵の児 提交于 2019-12-04 20:44:44

I have already resolved problem few month ago... I'm posting answer, so this might be helpful for other fellow developers... p.s. The project was written in Yii 1.1

Firstly, in my HorseController assigned private array type variable where I am going to keep Pedigree:

private $horses = array();

Then, I wrote function that will get all nodes (parents) from database:

public function getParents($id)
{
    global $horses;
    $horses[] = Horses::model()->findByPk($id)->horse_id;
    if($this->loadModel($id)->horse_sire != null && $this->loadModel($id)->horse_dam != null)
    {
        $this->getParents($this->loadModel($id)->horse_sire);
        $this->getParents($this->loadModel($id)->horse_dam);
    }
    return $horses;
}

Then, I modified ActionView function, where all nodes from database will be passed to View

public function actionView($id)
{
    $this->horses = $this->getParents($id);
    $this->render('view',array(
        'model'=>$this->loadModel($id),
        'parents'=>$this->horses,
    ));
}

And finally a bit of UGLY CODE that will show pedigree (like this Pedigree Query) :)

<table>
            <?php
                $reverse_multiplier = (count($parents)+1)/2;
                $last_node_count = 0;
                for($i = 0; $i < count($parents); $i++)
                {
                    if($i == 0 && $last_node_count ==1)
                        echo "<tr>";

                    echo "<td rowspan='$reverse_multiplier'>";

                    echo "<a href=".Yii::app()->baseUrl."/index.php/horses/".Horses::model()->model()->findByPk($parents[$i])->horse_id." >";
                    echo Horses::model()->model()->findByPk($parents[$i])->horse_name;
                    echo "</a>";
                    echo "<br/>";
                    echo Horses::model()->model()->findByPk($parents[$i])->horse_yob;

                    echo "</td>";
                    if($reverse_multiplier == 1 || $reverse_multiplier == 0.5)
                        echo "</tr>";

                    if($reverse_multiplier == 0.5 && $last_node_count <= (count($parents)+1)/4)
                        $reverse_multiplier = (count($parents)+1)/8;
                    else
                        $reverse_multiplier = $reverse_multiplier/2;

                    if($last_node_count == (count($parents)+1)/4)
                    {
                        $reverse_multiplier = (count($parents)+1)/4;
                        $last_node_count=0;
                    }
                    if($reverse_multiplier == 0.5 || $reverse_multiplier == 1)
                        $last_node_count++;
                }
            ?>
        </table>

And that's it :) Hope it was helpful...

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!