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
    Apr 2003
    Location
    Montreal, QC
    Posts
    340
    Thanks
    3
    Thanked 2 Times in 2 Posts

    Need a variable before it's set

    Hi,

    I've made this function to create my menu and it works great except for one thing - I need the $opener variable to be set before it is, and I can't really switch it around.

    I think it is possible to do what I want by replacing the echo statements by a string variable which appends to itself, but I haven't been able to make that work properly - mostly because I'm not that great working with functions and variable scope.

    So, here's the code:

    PHP Code:
    function make_menu$level,$CNX,$page ){
        
    $theString NULL;
        
    $opener NULL;
        
    $toPrint NULL;
        
    $after NULL;
        
    $subjectQuery "SELECT page.PAGE_TITLE, page.PAGE_ID, page.PAGE_FILE_NAME, page.NEXT_PAGE, page.PARENT_PAGE FROM page WHERE PARENT_PAGE = ".$level." ORDER BY PAGE_TITLE";
        
    $subjectResult mysql_query($subjectQuery,$CNX) or die(mysql_error());
        while(
    $subjectRow mysql_fetch_assoc($subjectResult)){
            
    //check if there are children
            
    $checkQuery "SELECT page.PAGE_TITLE, page.PAGE_ID, page.PAGE_FILE_NAME, page.NEXT_PAGE, page.PARENT_PAGE FROM page WHERE PARENT_PAGE = ".$subjectRow["PAGE_ID"]." ORDER BY PAGE_TITLE";
            
    $checkResult mysql_query($checkQuery,$CNX) or die(mysql_error());
            if( 
    mysql_num_rows($checkResult) > ){
                
    //make_opener($level,$CNX,$page,&$opener);
                
    echo '<li class="';
                echo 
    $opener;                // this is where I need to have the variable already set
                
    echo '"><a href="#" class="icon" onclick="return door(this);"></a>'.'<a href="'.$subjectRow["PAGE_FILE_NAME"].'" class="parent">'.$subjectRow["PAGE_TITLE"]."</a>\n";
                echo 
    '<ul class="item">'."\n";
                
    make_menu($subjectRow["PAGE_ID"], $CNX$page, &$opener);            
            } else {
                                            
    //  $opener shoule be set in this if...else statement
                
    if ($subjectRow["PAGE_ID"] == $page) {
                echo 
    '<li><a href="'.$subjectRow["PAGE_FILE_NAME"].'" class="parent">'.$subjectRow["PAGE_TITLE"]."</a>";
                } else {
                echo 
    '<li><a href="'.$subjectRow["PAGE_FILE_NAME"].'">'.$subjectRow["PAGE_TITLE"]."</a>";
                }
                
            }

         echo 
    "</li>"."\n";

            
    //reset child checking query and result to nothing
            
    mysql_free_result($checkResult);
            
    $checkQuery NULL
        } 
    // end the while loop
        

        
    echo "</ul>\n";
        
    //echo $theString;
        
    $theString NULL;
        return ;
    mysql_free_result($subjectResult);
    // end make_menu function

    function menu_maker($level,$CNX,$page) {

        echo 
    '<ul id="menuTree">'."\n";
    if (
    $page == 53) {
        echo 
    '<li class="announceheading">Encyclopedia:</li>'."\n";
    } elseif (
    $page == 10) {
        echo 
    '<li class="announceheading">Studio to Stage:</li>'."\n";
    }

    $theString=NULL;
    make_menu($level,$CNX,$page);



    // end menu_maker function 
    Search for Laughter or Just Search?
    GiggleSearch.org
    Blog: www.johnbeales.com
    All About Ballet: www.the-ballet.com

  • #2
    Regular Coder dniwebdesign's Avatar
    Join Date
    Dec 2003
    Location
    Carrot River, Saskatchewan
    Posts
    846
    Thanks
    15
    Thanked 10 Times in 10 Posts
    So what you're saying is that you need the $opener variable set before the menu is created. If so what do you need it set to?
    Dawson Irvine
    CEO - DNI Web Design
    http://www.dniwebdesign.com

  • #3
    Senior Coder
    Join Date
    Aug 2003
    Location
    One step ahead of you.
    Posts
    2,815
    Thanks
    0
    Thanked 3 Times in 3 Posts
    How about adding
    PHP Code:
    isset($opener) ? $opener 'Default value'
    where it is needed?

  • #4
    Regular Coder
    Join Date
    Apr 2003
    Location
    Montreal, QC
    Posts
    340
    Thanks
    3
    Thanked 2 Times in 2 Posts
    $opener is needed on the line that says "echo $opener;" the default value would be "closed"

    However, $opener would be set to "open" if the if condition "if ($subjectRow["PAGE_ID"] == $page) { " 6 lines later evaluates to true.

    Really, what I need to do is evaluate that if condition before I echo $opener, however this would print my menu elements in the wrong order.

    What $opener does in this expanding menu is determine if the page the menu is on is in a certain part of the menu tree, and if it is then the menu tree is opened to show the page title, (which is also bolded).
    Search for Laughter or Just Search?
    GiggleSearch.org
    Blog: www.johnbeales.com
    All About Ballet: www.the-ballet.com

  • #5
    Senior Coder
    Join Date
    Aug 2003
    Location
    One step ahead of you.
    Posts
    2,815
    Thanks
    0
    Thanked 3 Times in 3 Posts
    Well how do you imagine this? The thing you want to do is can be compared to wanting to get on a bus before it arrives.
    Moreover the part where trhe opener is neede will never execute if the if query is to be evaluaterd becouse they are in different if/else blocks.
    You should copy over the if and set it there.
    It can look like this: (if the if makes sense in that block)
    PHP Code:
    $opener = ($subjectRow["PAGE_ID"] == $page) ? 'open' 'closed'
    or the if/else version:
    PHP Code:
    if($subjectRow["PAGE_ID"] == $page)
    {
       
    $opener 'open';
    }
    else
    {
       
    $opener 'closed';



  •  

    Posting Permissions

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