...

View Full Version : php arrays



howie2009
11-13-2011, 12:55 AM
Hi Guys,
Anybody know how to achieve this http://www.codingforums.com/showthread.php?t=243599 using php arrays?
Thanks :thumbsup:

Inigoesdr
11-13-2011, 04:07 AM
$phrase = array();
$phrase[1] = "about";
$phrase[2] = "contact";
$phrase[3] = "services";
$phrase[4] = "hmmm";
$phrase[5] = "francais";
$phrase[6] = "car";
$phrase[7] = "deutsch";
$phrase[8] = "dsad";

$phrase_length = count($phrase);
for ($i = 1; $i < $phrase_length; $i++)
{
echo '{exp:lg_ml:translate key="' + $phrase[$i] + '"}';
}

howie2009
11-13-2011, 01:45 PM
Thanks but when I ran the code the output was 7 zeros - "0000000"


<?php
$phrase = array();
$phrase[1] = "about";
$phrase[2] = "contact";
$phrase[3] = "services";
$phrase[4] = "hmmm";
$phrase[5] = "francais";
$phrase[6] = "car";
$phrase[7] = "deutsch";
$phrase[8] = "dsad";

$phrase_length = count($phrase);
for ($i = 1; $i < $phrase_length; $i++)
{
echo '{exp:lg_ml:translate key="' + $phrase[$i] + '"}';
}


?>

my intended output is:

{exp:lg_ml:translate key="about"}
{exp:lg_ml:translate key="contact"}
{exp:lg_ml:translate key="services"}
..and so on

howie2009
11-13-2011, 02:23 PM
ok my hack seems to work



<?php
$phrase=array();
$phrase[1] = "about";
$phrase[2] = "contact";
$phrase[3] = "services";
$phrase[4] = "hmmm";
$phrase[5] = "francais";
$phrase[6] = "car";
$phrase[7] = "deutsch";
$phrase[8] = "dsad";


$phrase_length = count($phrase);
for ($i = 1; $i <= $phrase_length; $i++)
{
echo "{exp:lg_ml:translate key=\"" .$phrase[$i] . "\"}<br />";

}
?>

Thanks for the pointer.

howie2009
11-13-2011, 04:02 PM
last gasp attempt.
The above code works in a standalone php file so thanks for that!
What I actually need is for the tag (eg {exp:lg_ml:translate key="about"}) not to be echoed on screen but to be generated in the code such that when the template renders it sees the tag generated eg {exp:lg_ml:translate key="about"} and applies the translation. I am using a cms so these tags have predefined values.
The tag {exp:lg_ml:translate key="about"} actually holds eg the German translation of the word about so it outputs just "uber" on screen.

So apart from echo or vprintf is there another word that just applies the array so its available in the code for the browser to render?

I effectively need
{exp:lg_ml:translate key="about"}
{exp:lg_ml:translate key="services"}
{exp:lg_ml:translate key="whatever"}
...
generated in the code via the array and then for the resultant code to be rendered.

Is there a way even to make the php array render before everything else. CAn precedence of some sort be applied. Sorry newbie to php

THANKS!

Inigoesdr
11-14-2011, 12:09 AM
Whoops, I didn't use the proper concatenation operator.
So apart from echo or vprintf is there another word that just applies the array so its available in the code for the browser to render?

I effectively need
{exp:lg_ml:translate key="about"}
{exp:lg_ml:translate key="services"}
{exp:lg_ml:translate key="whatever"}
...
generated in the code via the array and then for the resultant code to be rendered.
What template engine are you using? The PHP code is executed in the order it is loaded, so presumably you just need to insert it in to your template after it's loaded, but before it's parsed. You'll have to provide some more information about how you are loading and parsing the templates though, if you want people to be able to help you with more detail.

howie2009
11-14-2011, 03:51 AM
Ok here goes my novice attempt of explaining things.
I use the cms expressionengine 1.7.0 and {exp:lg_ml:translate key="about"} is a tag that relates to a translation module installed in the cms control panel - in this case the tag equates to the value "uber". When I render the array above nothing outputs (yet in a standalone php file it renders fine) so it appears to be a cms environment issue. The tag is effectively a global variable I expect(strangely other global variables i tested in the array do output their values correctly). Anyway this tag in question outputs blank with no value so perhaps the the tag is the last thing to execute in the template hence its blank in the array output? Idea is the array outputs eg {exp:lg_ml:translate key="about"} and the translation php snippet in the module comes along sees the tag {exp:lg_ml:translate key="about"} and changes it to "uber" (which is retrieved from an xml file) and output uber on screen.

eg if I used {site_url} in the array instead of generating the tag {exp:lg_ml:translate key="about"} I would get eg "www.mydomain.com about" outputted as www.mydomain.com is the value associated with {site_url}

i.e: echo "{site_url} .$phrase[$i]<br />";


...maybe something along the lines of:

for ($i = 1; $i <= $phrase_length; $i++)
{
$resPhrase = "{exp:lg_ml:translate key=\"" .$phrase[$i] . "\"}<br />";
echo "$resPhrase"

}

..may work if u could suggest the correct syntax please?

Apologies if I aint explaining it well.
THANKS

This the specific code i use in the cms


<?php
$phrase=array();
{exp:structure_entries} => see http://buildwithstructure.com/documentation/structure_entries/
$phrase[{count}] = "{title}";
{/exp:structure_entries}
$phrase_length = count($phrase);
for ($i = 1; $i <= $phrase_length; $i++)
{

echo "{exp:lg_ml:translate key=\"" .$phrase[$i] . "\"}<br />"; => see http://ee-garage.com/uploads/addons/ee1/docs/lg-multi-language.1.1.0.commercial.pdf
}
?>

which equates effectively to the array code above in the examples



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum