Forum Moderators: coopster
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!
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.