Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 5 of 5
  1. #1
    Regular Coder
    Join Date
    Aug 2012
    Posts
    142
    Thanks
    39
    Thanked 3 Times in 3 Posts

    Disappearing variable in recursive tree code

    I'm setting up a menu structure using a recursive menu tree that works just fine. I want to be able to restrict the items in the menu by userID. I have some code that delivers the current users userID to a variable $currentUserID.

    The problem that I'm having is that there seems to be no way to get this variable into the code to be read. Here's the broken code:
    PHP Code:
    <?php
        $currentUserID 
    $row_currentUserRS['id'];
        
    $parentid 0// assuming that 0 is the main category
        
    get_sub_cats($parentid);
        function 
    get_sub_cats($parentid) {
        
    $sql "SELECT * FROM netContent WHERE parentID = $parentid "
        
    $run mysql_query($sql);
            echo 
    '<ul>';
                while (
    $rec mysql_fetch_assoc($run)) {
                    if (
    $rec['userID'] == $currentUserID) {
                    echo 
    '<li /><a href ="',$rec['pageType'], $rec['id'],'">'$rec['linkName'], '</a>'
                    
    get_sub_cats($rec['id']);
                    }
                }
            echo 
    '</ul>';
        }
    ?>
    If I change the if statement to
    Code:
     if ($rec['userID'] == 35) {
    for example, I get what I want. I also get what I want if I remove the if statement and add
    Code:
     AND userID = '35'
    to the query, but whenever I try and use the $currentUserID variable I get nothing. No list. No error.

    Any suggestions would be appreciated. I've been fooling with this for far too long.
    Last edited by rgEffects; 02-13-2013 at 04:48 AM.

  • #2
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,987
    Thanks
    4
    Thanked 2,660 Times in 2,629 Posts
    http://php.ca/manual/en/language.variables.scope.php
    $currentUserID is not in scope of function get_sub_cats. Pass it into the function by adding a parameter for it.
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 

  • Users who have thanked Fou-Lu for this post:

    rgEffects (02-12-2013)

  • #3
    Regular Coder
    Join Date
    Aug 2012
    Posts
    142
    Thanks
    39
    Thanked 3 Times in 3 Posts
    I read through the manual you referred to and searched the web but I can't seem to figure out how to add a parameter in the right way. Any suggestions?

  • #4
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,987
    Thanks
    4
    Thanked 2,660 Times in 2,629 Posts
    PHP Code:
    function get_sub_cats($parentid$currentUserID)

       ...
    }
    get_sub_cats($parentID$currentUserID); 
    Same goes for the recursive call.
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 

  • Users who have thanked Fou-Lu for this post:

    rgEffects (02-13-2013)

  • #5
    Regular Coder
    Join Date
    Aug 2012
    Posts
    142
    Thanks
    39
    Thanked 3 Times in 3 Posts
    Thanks for the help. I had tried something similar but missed that I needed to include all three instances. Here is the working code:
    PHP Code:
    <?php
        $currentUserID 
    $row_currentUserRS['id'];
        
    $parentid 0// assuming that 0 is the main category
        
    get_sub_cats($parentid$currentUserID);
        function 
    get_sub_cats($parentid$currentUserID) {
        
    $sql "SELECT * FROM netContent WHERE parentID = $parentid "
        
    $run mysql_query($sql);
        
        echo 
    '<ul>';
            while (
    $rec mysql_fetch_assoc($run)) {
                if (
    $rec['userID'] == $currentUserID){
                echo 
    '<li /><a href ="',$rec['pageType'], $rec['id'],'">'$rec['linkName'], '</a>'
                
    get_sub_cats($rec['id'], $currentUserID);
                }
            }
        echo 
    '</ul>';
        
        }
    ?>


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •