...

View Full Version : calling a variable from a variable in a function



fuzzy1
11-19-2007, 11:23 PM
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?


<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>

Inigoesdr
11-20-2007, 01:11 AM
Change this line:
$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.

fuzzy1
11-20-2007, 01:19 AM
Woohoo!
Variable Variables (http://us.php.net/language.variables.variable)
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!

Inigoesdr
11-20-2007, 01:31 AM
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 (http://php.net/language.variables.scope) 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:


function make_comboBoxDiv($cbxType,$num,$pgCode)
// and when you call it:
make_comboBoxDiv(2,1,$pgCode);

fuzzy1
11-20-2007, 02:26 AM
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

aedrin
11-20-2007, 03:27 PM
Do not use global at all, unless for a specific purpose. It makes for poor code quality / portability / readability.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum