So this is a personal question I think :)
I am working with MessageIndex.template.php (1.1 RC1)
What I am trying to do is checking if a topic is containing "my" own posts. So I used;
if ( $topic['class'] == 'my_normal_post' || $topic['class'] == 'my_veryhot_topic' || etc....)
So this way, I added all "my" post classes to the list. (Damn I know there is a shorter way for this but I dont know it :P )
Everything is normal till here. It returns the right results to me. But when I create a function for it for not writing that long story everytime I need it, it wont work.
I created the function like:
if ( .... as above) echo ' yes my topic '; else echo ' not my topic ';
When I call the function from somewhere, it always returns, 'yes my topic'
Maybe you think there maybe some typos. No, because I just copied and pasted the code, used instead of calling the function, and it works ok..
<?php
function MyTopicClass(){
// Globals here
if(($topic['class'] == 'my_normal_post') || ($topic['class'] == 'my_veryhot_topic')){
echo 'Yes my topic';
} else {
echo 'not my topic';
}
}
something like that?
That code is giving smf rss feed an error.
Doug
Yes Leipe_Po,
What I mean and what I say "not working" is that.
jaxdoug.com,
How did you test?
For other opinions; Dont worry about undefined index $topic Because I use it with foreach ($context['topics'] as $topic)
hmmz youve registerd the global to use $topic?
something like:
<?php
function MyTopicClass(){
global $topic;
if(($topic['class'] == 'my_normal_post') || ($topic['class'] == 'my_veryhot_topic')){
echo 'Yes my topic';
} else {
echo 'not my topic';
}
}
?>
now i dont know if $topic is a legal global, i'm just making this code up as we go,
as to doug:
what was the error message?
witch versions of smf are you two using?
EDIT: forgot to put a ; after the global
1.05 Its ok now, may have just been a read error.
Doug
@Leipe_Po,
If you read my post above, you will see that its not a problem to use $topic as its defined with foreach. (As if it was problem, forum error log surely warns me about undefined index: topic)
Moreover, there isnt a global like $topic.
whats the function? can you post some code?
function my_posts()
{
foreach ($context['topics'] as $topic)
{
if ($topic['class'] == 'my_normal_post' || $topic['class'] == 'my_veryhot_topic' || ..... other my topic classes)
echo ' yes my topic '; else echo ' not my topic ';
}
}
This is how I call it:
echo ' <img src="', $settings['images_url'] ,'/topic/', $topic['class'] ,'.gif" alt="', my_posts() ,'" /> ';
<?php
function my_posts()
{
foreach ($context['topics'] as $post)
{
if ($post['class'] == 'my_normal_post' || $post['class'] == 'my_veryhot_topic' || ..... other my topic classes)
echo ' yes my topic '; else echo ' not my topic ';
}
}
?>
since you used: $context['topics'] as $post ,you must use $post and not $topic
and i quess you can say the same for the url, but this isnt tested
<?php
echo ' <img src="', $settings['images_url'] ,'/topic/', $post['class'] ,'.gif" alt="', my_posts() ,'" /> ';
?>
Leipe_Po,
Guy, I know, you are trying to find a typo. But I am not that stupid! ;)
Because I told above, IT WORKS FINE OUT OF THE FUNCTION.
Now I corrected that thingy in the message above.
then whats the problem, the topic title says clearly: Function not working
Leipe_Po,
This is also what I am trying to find out anyway. I wouldnt have asked if I had known :)
Variables, be they global or local, has to be declared (global) or passed (local) before they can be accessed.
for example, try to start your function with globals, assuming they are really globals: (I think context is, but post...)
<?php
function my_posts()
{
global $context, $post;
// blah blah blah...
}
?>
Omg, I think noone is reading my posts :D
I already added all the globals to my function. Still no luck.
The $topic variable that is in the messageIndex template is a local variable. It is generated in a foreach loop. When calling a function you quit the variable scoop, so $topic no longer contains the value it had. To work around this call the function like this:
my_posts($topic['class'])
where my_posts function looks like
<?php
function my_posts($topic_class)
{
global $context, $post;
// blah blah blah...
if ($topic_class == 'my_normal_post'...
}
?>
Compuart,
Thanks a lot.
And is it $topic_class ? or $topic['class'] in function?
It's just a parameter I've chosen, it could have just as well been:
<?php
function my_posts($is_this_really_my_post_lets_find_out_shall_we)
{
global $context, $post;
// blah blah blah...
if ($is_this_really_my_post_lets_find_out_shall_we == 'my_normal_post'...
}
?>