Forum Moderators: coopster

Message Too Old, No Replies

Need a neat thing with DEFINE

like a variables variable

         

mcibor

10:08 am on Aug 27, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I use a smarty template system, where the language is stored in defined vals and I just pass them to the appropriate variable:

one file:
define("ERROR", "You can't enter here");

second file: $smarty -> assign(array("Error" => ERROR));

Everything runs smoothly.
However now, I have 2x4 texts, that I want to display randomly
TM1 - first text for a man
TM2 - second text for a man...
TM4 - fourth text for a man
TF1 - first text for a girl... etc

How can I pass the values of define? If the defined were variables I would just write:

${"T".$gender.$i} ===> $TM1 - $TF4

Is there a way to do this with defined as well?

Best regards
Michal Cibor

PS. I just don't want to have 8 ifs/cases
PS2. Sorry for the spelling mistake in the title. It should be "neat" there!

madmac

3:48 pm on Aug 27, 2005 (gmt 0)

10+ Year Member



Do your constant defines:

define('TM1', '...');
....
define('TF4', '...');

Use constant() to get the right one:

$var = constant('T' . $gender . $i);

ergophobe

4:12 pm on Aug 27, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Shouldn't you be generating the random value PHP side and then just passing that to Smarty? So you woudl use madmac's code and then

define ('TEXT_RAND', $var);

then

$smarty -> assign(array("TextRand" => TEXT_RAND));

If you need to name a constant on the fly and then define it, that's fairly easy with eval()

$gender = rand(0,1);
$index = rand(1,4);
$gender_ar = array('M', 'F');
eval("define('T" . $gender_ar[$gender] . $index ."', '" . $text . "');");

You can do that in a loop also and define them all that way

$text_array('M' => array('first male text', 'second male text'), 'F'=>array('first female text'));

foreach ($text_array as $gender_key => $gender_array)
{
$i=1;
foreach ($gender_array as $text)
{
eval("define('T" . $gender_key . $i . "', '" . $text . "');");
$i++;
}
}

Not sure I understand excatly though, because I don't know Smarty.

mcibor

4:45 pm on Aug 27, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Thanks guys!

I was making a "brothel" for a game, and had some texts (not random) that I wanted to show randomly (so people won't get bored so fast).

Therefore I'll use MadMac's way!
Thanks Ergophobe!

See you round! However I'm leaving for a week to Warsaw, so see you on next weekend!

Michal Cibor