Author |
Message |
bavarian
Hangin' Around

Joined: Nov 11, 2004
Posts: 25
|
Posted:
Mon Oct 15, 2007 5:05 am |
|
trying to get a script to work where a language constant (usually seen in phpNuke as ._XXXXX. is put into HTML code through a variable
something like this ...
Code:$byttext = strtoupper($altlang);
$byt = "._$byttext.";
echo "<a href=\"index.php?newlang=$tl\">
<img src=\"images/language/flag-$tl.png\" title=\"$byt\"></a>";
|
the variable $byt is on the page then shown as "._XXXXXX." without the quotes and where XXXXXX is the name of the constant everything is about. to get the value of the constant, I know I should put quotation-marks somewhere .... BUT WHERE? been trying for hours to get the quotation-marks in right place .... without success. any hints are very appreciated, please help ! THANK YOU VERY MUCH IN ADVANCE ! |
|
|
|
 |
montego
Site Admin

Joined: Aug 29, 2004
Posts: 9457
Location: Arizona
|
Posted:
Mon Oct 15, 2007 5:43 am |
|
Is this really nuke? If so, you simply add a new define in the appropriate lang-yourlanguage.php file within the languages directory and then use that constant.
So, for example, maybe you have added the following:
define('_XXXXXX', 'This was my label');
The above would have gone in your language file.
Then, to use that define, you would do something like this:
Code:
echo "<a href=\"index.php?newlang=$tl\">
<img src=\"images/language/flag-$tl.png\" title=\"" . _XXXXXX . "\"></a>";
|
This should give you what you need. |
_________________ Only registered users can see links on this board! Get registered or login!
Only registered users can see links on this board! Get registered or login! |
|
|
 |
bavarian

|
Posted:
Mon Oct 15, 2007 6:06 am |
|
yes, that's how it usually works ....
and dependent on the content of the variable (xxx1, xxx2, xxx3 and so on) i need to call the defined constants from the different langauge.php files ...
define("_XXX1","xxx1);
define("_XXX2","xxx2);
define("_XXX3","xxx3);
.
.
.
I got things to work to a certain degree. but the last "touch" is missing ... the quotation-marks
instead of having the code like this
Code: title=\"" . _XXXXXX . "\
|
i have it like this
this will return the content of the variable as a text-string. that's ok, but in order to have this recognized as the language constant i need to have quotation-marks around the string. and that's were the problem arises and the code does not work any longer ... whereever i try to put the quotationmarks in my attempts to resolve this |
|
|
|
 |
montego

|
Posted:
Mon Oct 15, 2007 6:24 am |
|
Ok, you must have your reasons for wanting to do it that way. I have defined new constants using dynamically generated constant names, but I have not done the reverse.
Constants are handled a bit differently and so I am not sure you can do this. My initial thought was you may need to wrap it in an eval(), but I have not tried.
You may have better luck if you use a language array rather than constants. Arrays are faster than constant definitions anyways. |
|
|
|
 |
Gremmie
Former Moderator in Good Standing

Joined: Apr 06, 2006
Posts: 2415
Location: Iowa, USA
|
Posted:
Mon Oct 15, 2007 8:00 am |
|
bavarian, if you are seeing XXXX instead of the constant's value, then it is likely that the constant wasn't defined in the scope you tried to use it. You may not be including the right language constant file. You might want to turn on warnings so you can see if that is happening.
You may also just try this:
$myVar = _XXXXX;
echo $myVar;
If you see _XXXXX instead of the value for _XXXXX it isn't in scope for some reason. |
_________________ Only registered users can see links on this board! Get registered or login! - An Event Calendar for PHP-Nuke
Only registered users can see links on this board! Get registered or login! - A Google Maps Nuke Module |
|
|
 |
Gremmie

|
Posted:
Mon Oct 15, 2007 9:25 am |
|
Let me clarify what I said, above. I am assuming you have your constants in another file. Is this correct? Then make sure you have included or required that file in your main script where you are trying to use your constants.
If the file containing the define for the constant XYZ has not been included, then for something like this:
$foo = XYZ;
PHP will issue a notice that XYZ is not defined (which you may not see because of your warning levels), but $foo will get the value of 'XYZ'. That might explain the behavior you are seeing.
Now if you had the file that had the define for XYZ included, PHP will assign the value of XYZ to $foo. |
|
|
|
 |
bavarian

|
Posted:
Mon Oct 15, 2007 10:46 am |
|
Gremmie: yes, the constants are residing in the phpnuke content management system original /language/folder in the root directory. I have already been checking if the constant (just for example here called _XXXX1 with a value of xxxxx1) does return the correct value, if i call it by other means: and yes, it does return the correct value (here for example reason just called xxxx1)
the problem is: in the title-tag that I mentioned above, on the page it just returns exactly as typed in next row
._XXXX1.
which is the name of the constant. that's how far I have manage to get the code to work.
if i remove the variable $XXXX1 and replace it in the title tag with the constant (title=\""._XXXX1"\" )it does then on the page provide everything correctly, the value xxxx1 . even that proofes that the constant from the correct lang.php-file is picked up correctly....
so ... I would need to find a way to put the variable $XXXX1 (that contains the string ._XXXX1. ) into quotationmarks ... that's what I have not succeeded with ! that's where I need help ... so the complete string contained in the constant $XXXX1 would be (including the quotationmarks)
"._XXXX1." (until now, the code just returns only ._XXXX1. without the quotes)
if you guys check in your php-nuke systems and search for files containing code like title=\"$ you will see that there are plenty of files with instances of that. in my case there is a varible that contains the string that is a name of a constant ._XXXX1. if somebody could hint me on how to put that into quotationmarsk the correct way everything could be solved i guess.
title=\""$XXXX1"\" will ruin the code, does give an error ...
also trying to combine the quotationmarks with the string that gives the constant name like
$XXXX1 = ""._XXXX1.""; will ruin the code, provides an error ... |
|
|
|
 |
fkelly
Former Moderator in Good Standing

Joined: Aug 30, 2005
Posts: 3312
Location: near Albany NY
|
Posted:
Mon Oct 15, 2007 12:18 pm |
|
<?php
define (_xxxx, "\"XXXX\"");
$xxxx = _xxxx;
echo $xxxx;
?> |
|
|
|
 |
Gremmie

|
Posted:
Mon Oct 15, 2007 1:28 pm |
|
I'm probably not understanding you, but again, that tells me in the place where the variable is assigned the constant, the constant isn't defined, and the variable is getting the constant name instead of the constant value.
It would help if we could see all of the code. Maybe you could zip it up and post a link to it. |
|
|
|
 |
montego

|
Posted:
Tue Oct 16, 2007 5:34 am |
|
Like I said, I don't think it can be done. Constants are handled differently than variables, which is why I suggested using an array for your labels rather than defines.
The key here is that you are trying to dynamically reference a constant. I.e., the constant NAME is dynamically generated for the one that you wish to use at any particular point in time.
I just cannot figure out why you would need for the constant names to be dynamically generated. Never seen that done anywhere in nuke or any other PHP program... ever. |
|
|
|
 |
Gremmie

|
Posted:
Tue Oct 16, 2007 7:01 am |
|
LOL...I'm still totally confused about this and would love to see all of the code in question.
In the meantime, much to my surprise, you can obtain the value of a constant if you don't know its name:
http://www.php.net/manual/en/function.constant.php
Quote: |
constant() is useful if you need to retrieve the value of a constant, but do not know its name. I.e. it is stored in a variable or returned by a function.
|
|
|
|
|
 |
montego

|
Posted:
Tue Oct 16, 2007 7:07 am |
|
Gremmie, awesome find! That might be exactly what the Dr. ordered!
 |
|
|
|
 |
fkelly

|
Posted:
Tue Oct 16, 2007 7:34 am |
|
Quote: | so ... I would need to find a way to put the variable $XXXX1 (that contains the string ._XXXX1. ) into quotationmarks ... that's what I have not succeeded with ! that's where I need help ... so the complete string contained in the constant $XXXX1 would be (including the quotationmarks)
"._XXXX1." (until now, the code just returns only ._XXXX1. without the quotes)
|
I guess it's the sea air that's keeping me from understanding. The code I quoted does what you are asking for, no? Ummm, looking at it again or is that you want the ._ in there too and the closing . too? |
|
|
|
 |
fkelly

|
Posted:
Tue Oct 16, 2007 7:51 am |
|
In which case:
Code:<?php
define (_xxxx, "\"._XXXX.\"");
$xxxx = _xxxx;
echo $xxxx;
?>
|
Does the trick. |
|
|
|
 |
bavarian

|
Posted:
Tue Oct 16, 2007 11:34 am |
|
busy days at the moment. will write more on this issue at the end of the week. thanks for the input so far ! |
|
|
|
 |
montego

|
Posted:
Wed Oct 17, 2007 5:18 am |
|
fkelly, I believe it is the constant name itself that he wishes to be "dynamic". In other words, some code will figure out which constant to use but the name of that constant is in a variable. Then, he wants to actually reference the constant from that variable to get its value to use elsewhere in his code.
At least that is how I am interpreting the conversation thus far. |
|
|
|
 |
fkelly

|
Posted:
Wed Oct 17, 2007 8:14 am |
|
Well then the answer is found on the user contributed notes section of the page Gremmie sited. Here's my simplified example based on that:
Code:<?php
// z determines which constant to use; could be retrieved from a database or based on user input
$z = '_yyyy';
define (_xxxx, "\"._XXXX.\"");
define (_yyyy, "\"._YYYY.\"");
$xxxx = _xxxx;
echo constant($z);
?>
|
|
|
|
|
 |
montego

|
Posted:
Wed Oct 17, 2007 6:54 pm |
|
Very cool... didn't know the function even existed. Thanks Gremmie for the "find"! |
|
|
|
 |
|