Collections functions
General functions
Render 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 480 to 506

Definition

 
function get_root_node_by_leaf(int $refint $level)
    {
    if(
>= $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 = ?";
            
$placeholders = ['i'$ref];
            }

        
$level--;
        }

    return (int) 
ps_value($query$placeholders0);
    }

This article was last updated 24th May 2022 20:05 Europe/London time based on the source file dated 6th April 2022 16:35 Europe/London time.