简体   繁体   中英

recursion php/mysql function issue

Hi I have a database table that is self referencing, basically it contains all the species and the taxonomic classification levels. so for example we have a turtle record below

tsn_id   name  rank_id  parent_id
123       turtle       220       349

the parent of this turtle record is the turtle_family record(which is in the same table) and this goes on until the parent_id is 0. 0 meaning the animal kingdom (animalia parent_id 0)

tsn_id   name         rank_id  parent_id
349      turtle_family 210     465

I'm wanting to climb up the levels for each species record like turtle. here is my attempt

<?php


function get_each_species_record(){

  $db = dbConnect();

  $query= "select * from species_records where rank_id = 220"; //this rank_id is species

  $result = $db -> Execute($query);

    while($row=$result->FetchRow())
    {
        $tsn_id= $row['tsn']; 
        $complete_name = $row['complete_name'];
        $parent_tsn = $row['parent_tsn'];
        $rank = $row['rank_id'];

        //* My objective *

        //for each species
        //take its parent tsn
        //where tsn = parent tsn
        //take that parents tsn
        //where tsn = parent tsn
        //till parent tsn = 0

       climbtree($tsn_id);

    }
}

//recursive function for each species

 function climbtree($tsn_id){

          if($parent_tsn == 0){
            return 1; //quit and go onto next speices record?

          }
          else{
            $queryone= 'select * from species_records where tsn = $parent_tsn';
            $resultone = $db -> Execute($queryone);
            return climbtree($tsn_id);
          }

  }


?>
function get_species($specie_id, $db = null) {
    if ($specie_id === '0') {
        return array();
    }
    if ($db === null) {
       $db = dbConnect();
    }
    $query = "SELECT * FROM species_records WHERE rank_id = $specie_id";
    $result = $db->Execute($query);
    $row = $result->FetchRow();
    return array_merge($row, array('parent' => get_species($row['parent_tsn'], $db);
}

This would do the recursion well, haven't tested if the array_merge will show up the results how you'd like it to, but this would essentially give you an an associative array that each animal has his parent until it reaches animalia which it's parent is an empty array.

I also handled the database connection inside so you wouldn't recreate the connection each level.

This is somewhat a redundant recursion, I would suggest that you'd have a cache table for each animal that is inserted to the system that creates it's parents so whenever you want to check for an animals predecessors you could just do one SELECT instead of a recursion.

ie

TABLE species_records_familiy_tree
tsn_id | parents
123    | 0,2,36,77,463,349

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM