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

  • SEARCH
  • Home
  • 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 8520545
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T06:33:56+00:00 2026-06-11T06:33:56+00:00

I have a table (eg the name is page), where each row is a

  • 0

I have a table (eg the name is “page”), where each row is a child of another row, like below (see the HTML section):

http://jsfiddle.net/Fuqrb/

I want to find the topmost parent’s id, by using a child’s id. For example, we get a URL variable as the id of the bottommost child:

http://www.website.com/page.php?p=6

Then we store the URL variable into a variable:

$p = $_GET['p'];

To get the topmost parent’s id, I have to use query function several times. For example:

if($result = $mysqli->query("SELECT * FROM page WHERE id='$p' LIMIT 1"))
{
    while($row = $result->fetch_array())
    {
        $parent = $row["parent"];
    }
}

if($result = $mysqli->query("SELECT * FROM page WHERE id='$parent' LIMIT 1"))
{
    while($row = $result->fetch_array())
    {
        $parent1 = $row["parent"];
    }
}

if($result = $mysqli->query("SELECT * FROM page WHERE id='$parent1' LIMIT 1"))
{
    while($row = $result->fetch_array())
    {
        $parent2 = $row["parent"];
    }
}

if($result = $mysqli->query("SELECT * FROM page WHERE id='$parent2' LIMIT 1"))
{
    while($row = $result->fetch_array())
    {
        $id = $row["id"];
    }
}

This way is ineffective, because if I got another URL variable as the id of another child of the higher/ lower level, this will not work.

How to get the topmost parent’s id dynamically, regardless of the child’s id?

  • 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-11T06:33:57+00:00Added an answer on June 11, 2026 at 6:33 am
    DELIMITER $$
    
    CREATE FUNCTION `fnGetRootParent`(
        page INT
    ) RETURNS int(11)
        READS SQL DATA
        DETERMINISTIC
    BEGIN
        DECLARE _parent INT;
        DECLARE _result INT;
    
        SET _parent = page;
    
        my_loop: LOOP
            SELECT 
                 p.id
                ,p.parent
            INTO 
                 _result
                ,_parent
            FROM page p
            WHERE id = _parent;
    
            IF (_parent = 0) OR (_parent = _result) THEN
                LEAVE my_loop;
            END IF;
        END LOOP my_loop;
    
        RETURN _result;
    END
    

    SELECT fnGetRootParent(213);

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

Sidebar

Related Questions

I have a <code>rich:extendedDataTable</code>. Each row has a a4j:commandLink as below. Table Page: <a4j:commandLink
On my index page I have check boxes in each row of my table.
I have a table named users and each row (user) has an image. The
I have a organization name table with the following structure given below: CREATE TABLE
I have a large table (2,000,000 rows) and I'd like to print each record
In my index.html page, I have an empty table defined as following: <body> ...
I have an html form (each form has different page, according to date), with
I have a table of of more then 100 rows. each row consist of
I have a problem with my self-made search page. There is 2 tables: **name
I have a page with several tables on it with the same class name.

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.