Go Back   CodingForums.com > :: Server side development > PHP

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 02-12-2013, 09:12 PM   PM User | #1
rgEffects
New Coder

 
Join Date: Aug 2012
Posts: 76
Thanks: 22
Thanked 0 Times in 0 Posts
rgEffects is an unknown quantity at this point
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..
rgEffects is offline   Reply With Quote
Old 02-12-2013, 11:24 PM   PM User | #2
Fou-Lu
God Emperor


 
Fou-Lu's Avatar
 
Join Date: Sep 2002
Location: Saskatoon, Saskatchewan
Posts: 15,741
Thanks: 4
Thanked 2,465 Times in 2,434 Posts
Fou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to all
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'); 
Fou-Lu is offline   Reply With Quote
Users who have thanked Fou-Lu for this post:
rgEffects (02-12-2013)
Old 02-12-2013, 11:54 PM   PM User | #3
rgEffects
New Coder

 
Join Date: Aug 2012
Posts: 76
Thanks: 22
Thanked 0 Times in 0 Posts
rgEffects is an unknown quantity at this point
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?
rgEffects is offline   Reply With Quote
Old 02-13-2013, 12:39 AM   PM User | #4
Fou-Lu
God Emperor


 
Fou-Lu's Avatar
 
Join Date: Sep 2002
Location: Saskatoon, Saskatchewan
Posts: 15,741
Thanks: 4
Thanked 2,465 Times in 2,434 Posts
Fou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to all
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'); 
Fou-Lu is offline   Reply With Quote
Users who have thanked Fou-Lu for this post:
rgEffects (02-13-2013)
Old 02-13-2013, 04:46 AM   PM User | #5
rgEffects
New Coder

 
Join Date: Aug 2012
Posts: 76
Thanks: 22
Thanked 0 Times in 0 Posts
rgEffects is an unknown quantity at this point
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>';
    
    }
?>
rgEffects is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 01:33 AM.


Advertisement
Log in to turn off these ads.