0

Hello,

I want help for recursive in php which like structure is attachment file..

[1] Sire (Parent)

 > Sub1
     ->nChild..
 > Sub2

[2] Dam (Parent)

 > Sub1
     ->nChild..
 > Sub2
     ->nChild..

Do you have some example of code so its better for me.
Thank You.

Attachments aCdb6.png 21.77 KB
3
Contributors
6
Replies
27
Views
2 Years
Discussion Span
Last Post by Taywin
0

Huh? What does your recursive function do? What are inputs and what output you want? Or you simply want a search for something? The word recursive is very broad.

0

@Taywin:Given In Details.

[1]I have One dropdown box there have number of Goat Names.
[2]When select it then it geneted his Sir and Dam Names.
[3] If Sir and Dame have his N-childs then its also Display Names.

Names records come from database simple.

1

OK, so the input is a name used in query to get from database. You must always define base cases for a recursive function, or it will run forever! A very simple idea for the function (Depth-First-Search) is as follows...

function a_recursive_function(name)
  data = get_data_from_database(name)
  if data not exist  // base case
    done!
  end if
  display data(SireName)
  a_recursive_function(data(SireName)
  display data(DamName)
  a_recursive_function(data(DamName)
end function

Edited by Taywin

Votes + Comments
@Thank You.Now its work
0

We always try to prevent ourselves from sinking into recursive function in PHP. However, if that is what you need, please consider the simple function I wrote to demonstrate how recursion works.

Recursion in PHP is a function or method that calls itself within itself.

Huh!???? I know my definition is a little blurry. For a moment allow me to provide a generic example without logic.

function genericFunction()
{

    return genericFunction();

 }

We can make our simple non-logical function above as template.

Let say we have a paren array as shown below,

$parent = array(
                    'a'=>(array('a_one' => 'a_one value','a_two'=>'a_two value', 'a_three' => 'a_three value')),
                    'b'=>(array('b_one' => 'b_one value','b_two'=>'b_two value', 'b_three' => 'b_three value'))
                    );

and we want a function that will give us the $parent['a'] and $parent['b'] recursively.

Array ( [a_one] => a_one value [a_two] => a_two value [a_three] => a_three value ) 
Array ( [b_one] => b_one value [b_two] => b_two value [b_three] => b_three value )

we can simply write a function that can do or provide us an output like the above.

the function::

function find_child_in_parent($child,$parent = array())
{

    if(is_array($parent)){

        foreach($parent as $p_key => $p_value){

            if($p_key == $child){

                return ( $p_value );
            } // endif

         } //endforeach

         if(is_array($p_value)){

            //recursion here
            $recursion_res = find_child_in_parent($child,$p_value);

            return($recursion_res ? $recursion_res : FALSE);

            }

            return FALSE;





    }

    }

we call the function like this

 $parent = array(
                    'a'=>(array('a_one' => 'a_one value','a_two'=>'a_two value', 'a_three' => 'a_three value')),
                    'b'=>(array('b_one' => 'b_one value','b_two'=>'b_two value', 'b_three' => 'b_three value'))
                    );

     print_r(find_child_in_parent('a',$parent));
     echo '<br/>';

     print_r(find_child_in_parent('b',$parent));   

the result will be the same as I expected it shown in the earlier paragraph.

Good luck to you. please feel free to experiment. Don't be afraid in making mistakes. :)...

Edited by lorenzoDAlipio: corrected codes

0

@Taywin: as you guide me here i have worked but its going infinite.

function generate_tree($sire)
{
 global $wpdb;

    $sql = $wpdb->prepare("SELECT * FROM mm_goats WHERE goat_name = %s", $sire);
    $rowsire = $wpdb->get_row($sql, ARRAY_A);

    if(empty($rowsire))
    {
    echo "done";
    }

    echo $rowsire['sire'];            //this porsion come infinite as output
    generate_tree($rowsire['sire']);

    echo $rowsire['dam'];              //this porsion not come in output
    generate_tree($rowsire['dam']);

}
0

You need to return (exit the function) right after line 10 inside the if statement. ;)

Edited by Taywin

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.