3

Possible Duplicate:
function returning only once, why?

my Database structure looks like

id|parent|
1 |   0  |
2 |   0  |
3 |   0  |
4 |   1  |
5 |   4  |
6 |   5  |

I am in need of a function that gets parent(i.e parent=0) for a id as parameter For eg .. get_parent(6)==returns 1 I did some research and found this question

How can I recursively obtain the "parent ID" of rows in this MySQL table?

I tried making this function

    function get_parent_id($cid,$found=array())
    {
     array_push($found,$cid);
     $sql="SELECT * FROM tbl_destinations WHERE id=$cid";
     $result = mysql_query($sql) or die ($sql);
     if(mysql_num_rows($result))
     {

        while($row = mysql_fetch_assoc($result))
        {
        $found[] = get_parent_id($row['parent'], $found);
        }
     }
return $found;
       }

I make a call by

$fnd=get_parent_id();
$array_reverse($fnd);
$parent_root=$fnd['0'];

But my method is wrong. Where did I go wrong?

Community
  • 1
  • 1
Bikal Basnet
  • 1,639
  • 1
  • 21
  • 31
  • 2
    You really shouldn't *double* post. You should ensure you receive your answer for the original question. – Jason McCreary Nov 12 '12 at 19:09
  • even though . I would be very glad if any one could provide me the answer besides telling me the rules and regulation. Sorry for being rude..I need answer of this question desperately – Bikal Basnet Nov 12 '12 at 19:11
  • please try to understand http://de.slideshare.net/billkarwin/models-for-hierarchical-data – Varon Nov 12 '12 at 19:25
  • you don't want it's direct parent ... you want to navigate all the way up the tree until it is a root node, correct? just do a recursive loop getting each parent until the parent = 0. – MikeMurko Nov 12 '12 at 19:39

1 Answers1

3

Are you trying to get the parent ID within the SQL query, or using PHP? If you're looking at using PHP for it, you could either do $arr[6]['parent'] assuming you got the information from the database into an array. Or, you could have a function:

<?php
//Let's assume you have your data from the database as such
$arr = array(
    array('id' => 1, 'parent' => 0),
    array('id' => 2, 'parent' => 0),
    array('id' => 3, 'parent' => 0),
    array('id' => 4, 'parent' => 1),
    array('id' => 5, 'parent' => 4),
    array('id' => 6, 'parent' => 5));

function get_key($arr, $id)
{
    foreach ($arr as $key => $val) {
        if ($val['id'] === $id) {
            return $key;
        }
    }
    return null;
}

function get_parent($arr, $id)
{
    $key = get_key($arr, $id);
    if ($arr[$key]['parent'] == 0)
    {
        return $id;
    }
    else 
    {
        return get_parent($arr, $arr[$key]['parent']);
    }
}

echo get_parent($arr, 6);
?>

Note that the code is untested, and just a sample.

jmgardn2
  • 981
  • 2
  • 8
  • 21
  • he wants the "root" parent. so not the direct parent. you need to recurse until you get $arr[$id]['parent'] = 0, and then return $id; – MikeMurko Nov 12 '12 at 19:39
  • ya I want the root parent. and @mikeMurko I am trying to make that. If I do that I will put my answers here. I am almost very close to getting answer. but am getting confused by tree hierarchy – Bikal Basnet Nov 12 '12 at 19:47
  • Oh ok, that makes sense then, I've updated my answer to demonstrate what you're looking for. I've tested and verified it works as expected. – jmgardn2 Nov 12 '12 at 19:57
  • Not a problem, glad I could help. Let me know if you need any explanation of the code as I didn't comment it. – jmgardn2 Nov 12 '12 at 20:09