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 6 of 6
  1. #1
    Regular Coder
    Join Date
    Apr 2006
    Posts
    311
    Thanks
    17
    Thanked 0 Times in 0 Posts

    calling a variable from a variable in a function

    Lousy Title I know, but couldn't think how better to phrase it.
    Please bare with me.

    I'm trying to call the correct dropdown options from $comDiv1 and $dropDiv_a1 into the following function
    "make_comboBoxDiv($cbxType,$num)" based on $cbxType variable.(See comments lines 45,51,63-65).

    If I enter EITHER the variable $comDiv1; or $dropDiv_a1; on line 63, the select option values from either, populate into BOTH the resulting fields, however if I enter the $options; variable on line 63, the result is that instead getting the appropriate OPTION VALUES from $comDiv1; and $dropDiv_a1; in comDiv1 and dropDiv_a1 respectively, I get instead simply the STRINGS "$comDiv" and "$dropDiv_a1" respectively returned in the source.

    Please, what am I doing wrong here?

    PHP Code:
    <script>
    // quick browser tests
    var ie5 = (document.all && document.getElementById) ? true : false;
    function show(sw,obj) {
    // show/hide divs
    if (sw && ie5) document.all[obj].style.display = 'block';
    if (!sw && ie5) document.all[obj].style.display = 'none';
    }
    function popTextField(fieldName,fieldValue){
        document.getElementById(fieldName).value=fieldValue;
        }
    </script>

    <style>
        <!--
            .myLayersClass { position: relative; top:0px; left:10px; display: none;style='z-index: 99' }
        -->
    </style>
    <body>
    <?php
    $pgCode
    ='a';//(alphaKey) 
    global $pgCode;
    function 
    make_comboBoxDiv($cbxType,$num){
        
    $comDiv1="
            <option>Select Option</option>
            <option value='Red'>Red</option>
            <option value='Green'>Green</option>
            <option value='Blue'>Blue</option>
        "
    ;
        
    $dropDiv_a1="
            <option>Select Option</option>
            <option value='Cyan'>Cyan</option>
            <option value='Magenta'>Magenta</option>
            <option value='Yellow'>Yellow</option>
        "
    ;
    if(
    $cbxType=='1'){
        
    $textName="comments_".$num;
        
    $selDiv="comDiv".$num;
        
    $selName="comSel_".$num;
        
    $options="$".$selDiv;//echos dropDiv_a1
        
    }
        else if(
    $cbxType=='2'){
            
    $textName="drop_".$pgCode.$num;
            
    $selDiv="dropDiv_".$pgCode.$num;
            
    $selName="dropSel_".$pgCode.$num;
            
    $options="$".$selDiv;//echos comDiv1
            
    }
            
    $div="<div>
                <input type='text' name='$textName' id='$textName' 
                    onFocus=\"show(true,'$selDiv');\" 
                    onblur=\"ff='$selDiv'; sTo=setTimeout('show(false,ff)',5);//allow switch focus to <select> below\">
                        <div id='$selDiv' class='myLayersClass'>
                            <select name='$selName' id='$selName'  
                                onFocus='clearTimeout(sTo);' 
                                onBlur='show(false,\"$selDiv\");' 
                                onchange=\"popTextField('$textName',this.value);show(false,'$selDiv');\" >
                                "
    ;
                                
    $div .= $comDiv1;//this works
                                //$div .= $dropDiv_a1;//this works
                                //$div .= $options;//this doesn't work
                                    
    $div .= "</select>
                        </div>
            </div>"
    ;
    echo
    "$div";
    }
    ?>
    <table style='table-layout: fixed;' >
        <tr height="25">
            <td ><?php make_comboBoxDiv(2,1);?></td>
            <td ></td>
            <td ></td>
            <td ></td>
        </tr>
        <tr >
            <td ><?php make_comboBoxDiv(1,1);?></td>
            <td ></td>
            <td ></td>
            <td ></td>
        </tr>
    </table>

    </body>

  • #2
    Super Moderator Inigoesdr's Avatar
    Join Date
    Mar 2007
    Location
    Florida, USA
    Posts
    3,638
    Thanks
    2
    Thanked 404 Times in 396 Posts
    Change this line:
    PHP Code:
    $options="$".$selDiv;
    // to
    $options=$$selDiv
    http://php.net/language.variables.variable

    And move this line: global $pgCode; inside of the function. Or better yet, pass it as a parameter to the function.

  • Users who have thanked Inigoesdr for this post:

    fuzzy1 (11-20-2007)

  • #3
    Regular Coder
    Join Date
    Apr 2006
    Posts
    311
    Thanks
    17
    Thanked 0 Times in 0 Posts
    Woohoo!
    Variable Variables
    Who Knew? Well, obviously YOU DID!
    Thank You Very Much!
    BTW, Also don't know why, but global $pgCode works as is.
    Be interested nonetheless in reading your thoughts on why it should'nt
    and why it would be better inside the function, and what you mean exeactly by "pass it as a parameter".
    Anyway, Thanks Again, that was driving me crazy!
    Last edited by fuzzy1; 11-20-2007 at 01:32 AM.

  • #4
    Super Moderator Inigoesdr's Avatar
    Join Date
    Mar 2007
    Location
    Florida, USA
    Posts
    3,638
    Thanks
    2
    Thanked 404 Times in 396 Posts
    Quote Originally Posted by fuzzy1 View Post
    BTW, Also don't know why, but global $pgCode works as is.
    Be interested nonetheless in reading your thoughts on why it should'nt
    and why it would be better inside the function, and what you mean exeactly by "pass it as a parameter".
    Anyway, Thanks Again, that was driving me crazy!
    global brings variables from the global scope into the function's local scope. And I meant you should define it in the function and pass it like this:
    PHP Code:
    function make_comboBoxDiv($cbxType,$num,$pgCode)
    // and when you call it:
    make_comboBoxDiv(2,1,$pgCode); 

  • #5
    Regular Coder
    Join Date
    Apr 2006
    Posts
    311
    Thanks
    17
    Thanked 0 Times in 0 Posts
    Okay, sure.
    $pgCode is "page code" (a-p) for each of 19 iframe forms pages which are brought together in a master iframe. For convenience sake the dropDiv numbering scheme starts at "1" for each forms page appended by the page code (a-p) to keep from breaking the javascript when all are assembled in the master iframe.

    I only want/need to define $pgCode once per page, so as to append each dropDiv therein, so in that context, I believe it is doing exactly what I want
    as is.
    Thanks for the clarification nonetheless

  • #6
    Senior Coder
    Join Date
    Jan 2007
    Posts
    1,648
    Thanks
    1
    Thanked 58 Times in 54 Posts
    Do not use global at all, unless for a specific purpose. It makes for poor code quality / portability / readability.


  •  

    Posting Permissions

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