Collections functions
Encryption functions
General functions
Theme permission functions
Resource functions

get_root_node_by_leaf()

Description

Find node ID of the root parent when searching by one
of the leaves ID
Example:
1
2
2.1
2.2
2.2.1
2.2.2
2.2.3
2.3
3
Searching by "2.2.1" ID will give us the ID of node "2"

Parameters

ColumnTypeDefaultDescription
$ref integer Node ID of tree leaf
$level integer Node depth level (as returned by get_tree_node_level())

Return

integer|boolean

Location

include/node_functions.php lines 403 to 438

Definition

 
function get_root_node_by_leaf($ref$level)
    {
    
$ref   escape_check($ref);
    
$level escape_check($level);

    if(!
is_numeric($level) && >= $level)
        {
        return 
false;
        }

    
$query "SELECT n0.ref AS `value` FROM node AS n{$level}";

    
$from_level $level;
    
$level--;

    while(
<= $level)
        {
        
$query .= " LEFT JOIN node AS n{$level} ON n" . ($level 1) . ".parent = n{$level}.ref";

        if(
=== $level)
            {
            
$query .= " WHERE n{$from_level}.ref = '{$ref}'";
            }

        
$level--;
        }
        
    
$root_node sql_value($query'');

    if(
'' == $root_node)
        {
        
$root_node 0;
        }

    return (int) 
$root_node;
    }

This article was last updated 12th July 2020 01:35 Europe/London time based on the source file dated 6th July 2020 18:09 Europe/London time.