Textpattern CMS support forum
You are not logged in. Register | Login | Help
- Topics: Active | Unanswered
#1 2007-06-01 23:48:53
- hdcool
- New Member
- Registered: 2006-02-01
- Posts: 7
foreach-tag in an external file is behaving
Hi all,
I’m having a slight problem here, for which I can’t really find the cause :\
I have an existing site which has this somewhere in a file:
settings.inc.php
[code]
$daysOfWeek = array( “Mon” => “Maandag”,
“Tue” => “Dinsdag”,
“Wed” => “Woensdag”,
“Thu” => “Donderdag”,
“Fri” => “Vrijdag”,
“Sat” => “Zaterdag”,
“Sun” => “Zondag”
);
[/code]
Which is basically a translation to Dutch :-)
Then, I use this function to create a nice formatted date, eg: Saterday 02/06/2007.
common.inc.php
[code]
function parseDates()
{
global $daysOfWeek;
global $currentDay;
foreach($daysOfWeek as $key => $value) { if ( $currentDay == $key ) $currentDay = eregi_replace( $currentDay, $value, $currentDay ); }
return $currentDay.” “.date(“d/m/Y”); } [/code]This works perfectly, until I tried to call this function from within a <txp:php> tag. I stripped the code down, and without the foreach it works. It’s the foreach it’s complaining about:
[quote=“My TXP website”]
tag_error <txp:php> -> Warning: Invalid argument supplied for foreach() on line 18
[/quote]
Anyone an idea of what’s going on? Any help is appreciated!
Offline
Re: foreach-tag in an external file is behaving
use global $daysOfWeek in both places, not just in the function, otherwise you’re declaring $daysOfWeek as a local variable. When using <txp:php> setting the variable outside a function does not make it a global variable, you have to specify that explicitly. Same for plugins, btw.
Last edited by ruud (2007-06-02 00:52:25)
Offline
#3 2007-06-02 08:26:50
- Mary
- Sock Enthusiast
- Registered: 2004-06-27
- Posts: 6,236
Re: foreach-tag in an external file is behaving
Offline
#4 2007-06-02 10:58:04
- hdcool
- New Member
- Registered: 2006-02-01
- Posts: 7
Re: foreach-tag in an external file is behaving
Hmmm… Mary, I did read the FAQ, and I experimented with making the variables global and not, except those.. rats.. :p
It just didn’t cross my mind that is true for every possible variable in the scope. I thought it was only for those that are used in <txp:php> tags.
What I had now is that I declared the variables in the global scope, and then there where I needed them, I declared that they had to be global. Now that you mention this, it’s getting more obvious… .
So indeed, when I put global in front of all “globally” declared variables it’s working.
Thanks all… again :-)
Offline