Coding standards
Security in ResourceSpace
Developer reference
Database
Action functions
Admin functions
Ajax functions
Annotation functions
API functions
Collections functions
Comment functions
Config functions
CSV export functions
Dash functions
Debug functions
Encryption functions
Facial recognition functions
File functions
General functions
Language functions
Log functions
Login functions
Message functions
Migration functions
Node functions
PDF functions
Plugin functions
Render functions
Reporting functions
Request functions
Research functions
Slideshow functions
Theme permission functions
User functions
Video functions
Database functions
Metadata functions
Resource functions
Search functions
Map functions
Job functions
Tab functions
Test 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 495 to 521

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 1st November 2023 15:05 Europe/London time based on the source file dated 17th October 2023 09:45 Europe/London time.