Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8822371
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T06:05:34+00:00 2026-06-14T06:05:34+00:00

Possible Duplicate: function returning only once, why? my Database structure looks like id|parent| 1

  • 0

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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T06:05:35+00:00Added an answer on June 14, 2026 at 6:05 am

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Possible Duplicate: function parameter evaluation order Assuming that I have a function with 4
Possible Duplicate: Bind function to multiple events of different elements at once I need
Possible Duplicate: returning multiple values from a function For example if you want a
Possible Duplicate: Returning unique_ptr from functions 20.7.1.2 [unique.ptr.single] defines copy constructor like this :
Possible Duplicate: Should a function have only one return statement? Hello, gcc 4.4.4 c89
Possible Duplicate: Function ereg() is deprecated I understand that in the new version of
Possible Duplicate: Javascript function to add X months to a date I'd like to
Possible Duplicate: Write a function that returns the longest palindrome in a given string
Possible Duplicate: Should a function have only one return statement? My teacher took points
Possible Duplicate: Why can you return from a non-void function without returning a value

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.