Function not working..

Started by Elmacik, December 14, 2005, 06:40:27 PM

Previous topic - Next topic

Elmacik

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..
Home of Elmacik

Leipe Po


<?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?
There is only one thing more importend to me then coding:
My Girlfriend

Microsoft - "You've got questions.  We've got dancing paperclips."

Doug

That code is giving smf rss feed an error.

Doug
YAP Portal

Elmacik

#3
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)
Home of Elmacik

Leipe Po

#4
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
There is only one thing more importend to me then coding:
My Girlfriend

Microsoft - "You've got questions.  We've got dancing paperclips."

Doug

1.05 Its ok now, may have just been a read error.


Doug
YAP Portal

Elmacik

@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.
Home of Elmacik

Leipe Po

whats the function? can you post some code?
There is only one thing more importend to me then coding:
My Girlfriend

Microsoft - "You've got questions.  We've got dancing paperclips."

Elmacik

#8

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() ,'" /> ';
Home of Elmacik

Leipe Po


<?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() ,'" /> ';
?>

There is only one thing more importend to me then coding:
My Girlfriend

Microsoft - "You've got questions.  We've got dancing paperclips."

Elmacik

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.
Home of Elmacik

Leipe Po

then whats the problem, the topic title says clearly: Function not working
There is only one thing more importend to me then coding:
My Girlfriend

Microsoft - "You've got questions.  We've got dancing paperclips."

Elmacik

Leipe_Po,
This is also what I am trying to find out anyway. I wouldnt have asked if I had known :)
Home of Elmacik

Sheepy

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...
}
?>

Elmacik

Omg, I think noone is reading my posts :D
I already added all the globals to my function. Still no luck.
Home of Elmacik

Compuart

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'...
}
?>
Hendrik Jan Visser
Former Lead Developer & Co-founder www.simplemachines.org
Personal Signature:
Realitynet.nl -> ExpeditieRobinson.net / PekingExpress.org / WieIsDeMol.Com

Elmacik

Compuart,
Thanks a lot.
And is it $topic_class ? or $topic['class'] in function?
Home of Elmacik

Compuart

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'...
}
?>

Hendrik Jan Visser
Former Lead Developer & Co-founder www.simplemachines.org
Personal Signature:
Realitynet.nl -> ExpeditieRobinson.net / PekingExpress.org / WieIsDeMol.Com

Advertisement: