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 11-19-2007, 11:23 PM   PM User | #1
fuzzy1
Regular Coder

 
Join Date: Apr 2006
Posts: 311
Thanks: 17
Thanked 0 Times in 0 Posts
fuzzy1 is an unknown quantity at this point
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>
fuzzy1 is offline   Reply With Quote
Old 11-20-2007, 01:11 AM   PM User | #2
Inigoesdr
Super Moderator


 
Inigoesdr's Avatar
 
Join Date: Mar 2007
Location: Florida, USA
Posts: 3,602
Thanks: 2
Thanked 398 Times in 391 Posts
Inigoesdr is a jewel in the roughInigoesdr is a jewel in the roughInigoesdr is a jewel in the rough
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.
Inigoesdr is offline   Reply With Quote
Users who have thanked Inigoesdr for this post:
fuzzy1 (11-20-2007)
Old 11-20-2007, 01:19 AM   PM User | #3
fuzzy1
Regular Coder

 
Join Date: Apr 2006
Posts: 311
Thanks: 17
Thanked 0 Times in 0 Posts
fuzzy1 is an unknown quantity at this point
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..
fuzzy1 is offline   Reply With Quote
Old 11-20-2007, 01:31 AM   PM User | #4
Inigoesdr
Super Moderator


 
Inigoesdr's Avatar
 
Join Date: Mar 2007
Location: Florida, USA
Posts: 3,602
Thanks: 2
Thanked 398 Times in 391 Posts
Inigoesdr is a jewel in the roughInigoesdr is a jewel in the roughInigoesdr is a jewel in the rough
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); 
Inigoesdr is offline   Reply With Quote
Old 11-20-2007, 02:26 AM   PM User | #5
fuzzy1
Regular Coder

 
Join Date: Apr 2006
Posts: 311
Thanks: 17
Thanked 0 Times in 0 Posts
fuzzy1 is an unknown quantity at this point
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
fuzzy1 is offline   Reply With Quote
Old 11-20-2007, 03:27 PM   PM User | #6
aedrin
Senior Coder

 
Join Date: Jan 2007
Posts: 1,648
Thanks: 1
Thanked 58 Times in 54 Posts
aedrin will become famous soon enough
Do not use global at all, unless for a specific purpose. It makes for poor code quality / portability / readability.
aedrin 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 11:07 PM.


Advertisement
Log in to turn off these ads.