PDA

View Full Version : passing arguements to function


Oreamnos
02-10-2005, 02:00 PM
I have a simple function in my includes/functions.php file. it requires 2 arguements (userid, forumid).

I am trying to make a template conditional that uses this function

where do the userid and forumid get passed to this function?

any help with this would be great because i am super noob to templates

thanks
eric

Andreas
02-10-2005, 02:19 PM
Without a few exceptions (like can_moderate() or in_array()) you can't use functions in Templates!

Oreamnos
02-10-2005, 02:23 PM
thanks

I realize you are quite limited in what you can do in the template conditionals.

But how do the arguements get passed to say, the following function, found in /inludes/functions.php?// ###################### Start queryphrase #######################
function fetch_phrase($phrasename, $phrasetypeid, $strreplace = '', $doquotes = true, $alllanguages = false, $languageid = -1)
{

Andreas
02-10-2005, 03:22 PM
$phrase = fetch_phrase('someerror', PHRASETYPEID_ERROR);

for Example, if this is what you wanted to know ...

Marco van Herwaarden
02-10-2005, 06:47 PM
Just to make things a bit clear. You are now talking about a php-script, not about a template.

Oreamnos
02-10-2005, 11:21 PM
Thanks but I am still lost. Let me be more specific.

I have this function (which I didn't write) which is in my inludes/function.php. what it is supposed to do is check if the user has started a thread in forum number 10.


// ###################### Check if user posted Introductory thread #######################
function posted_in_forum($userid,$forumid = 10) {
global $DB_site;
$posted = $DB_site->query_first("SELECT threadid FROM ".TABLE_PREFIX."thread WHERE postuserid='$userid' AND forumid='$forumid' LIMIT 0,1");
if ($posted['threadid'])
return true;
else
return false;
}


First, is the function correct and if so, what should my template conditional look like? right now it looks like this <if condition="$posted_in_forum"> but i have a strong feeling that's wrong :rolleyes:

Can someone help me with this? Thanks!
Eric

Andreas
02-11-2005, 12:17 AM
1) The function does what it is supposed to do
2) You can't use this function in Template conditionals

Oreamnos
02-11-2005, 12:19 AM
I realize i cant use it in a template conditional. But can i use the result of this function in a template conditional?

Andreas
02-11-2005, 12:22 AM
Yes.
Calll it in a PHP file like


$hasposted = posted_in_forum($userid, $forumid)


and then use


<if condition="$hasposted">
stuff here
</if>


(There is a way to make custom functions work in Template conditionals, but as this can be dangerous I won't recommend it)

Oreamnos
02-11-2005, 12:39 AM
i think we are getting closer.

I put this in my functions.php file:
// ###################### Check if user posted Introductory thread #######################
function posted_in_forum($userid,$forumid) {
global $DB_site, $bbuserinfo;
$posted = $DB_site->query_first("SELECT threadid FROM ".TABLE_PREFIX."thread WHERE postuserid='$userid' AND forumid='$forumid' LIMIT 0,1");
if ($posted['threadid'])
return true;
else
return false;
}


$hasposted = posted_in_forum($bbuserinfo['userid'], 10);

and this is my template conditonal:
<if condition="$hasposted">
<tr>
<td class="vbmenu_option" align="left">Not Posted</td>
</tr>
<else />
<tr>
<td class="vbmenu_option" align="left">Posted</td>
</tr>
</if>

But still no luck... is it ok to have this $hasposted = posted_in_forum($bbuserinfo['userid'], 10); in my functions.php file? or should it go somewhere else?

Andreas
02-11-2005, 12:47 AM
Should work - if $hasposted is within the scope.
If which Template did you try the above code?

Furhtermore, the way you did it, posted_in_forum() will be called on every page - which seems like overkill to me.

Oreamnos
02-11-2005, 12:50 AM
I'm not sure if $hasposted is within the scope. How can I check that?

I am calling the code in the header template so it needs to be called everytime.

I am basically checking if a user has introdued themselves and if they haven't, show a reminder to them.