Toube
asked on
php subfunctions problem
Hi,
I'm having difficulties showing functions withing a function using php.
I'm including a forum_functions.php on the main index.php file.
The forum_functions.php file has got some main functions like this:
Now these 3 functions then include some subfunctions that are also shown to users depending on users view level in Mysql db. viewlevels are 1-5.. 1 being normal user and 5 being admin user.
Problem here is that I cannot get all of the function so show if user has a userlevel 5 (should see all the messages).
Here's how I call the functions from the index.php file:
What am I doing wrong?
Regards,
Toby
I'm having difficulties showing functions withing a function using php.
I'm including a forum_functions.php on the main index.php file.
The forum_functions.php file has got some main functions like this:
function showactive($pageid) //pageid = 1 show active headers
{
function showtopics_all($ulevel, $refresh) //shown to users with userlevel = 1
{
echo "this is shown for all users";
}
function showtopics_family($ulevel) //shown to users with userlevel = 3, 4 and 5
{
echo "this is shown for users with level 3 and higher";
}
function showtopics_friends($ulevel) //shown to users with userlevel = 2,3,4 and 5
{
echo "this is shown for users with level 2 and higher";
}
} // end main function
Now these 3 functions then include some subfunctions that are also shown to users depending on users view level in Mysql db. viewlevels are 1-5.. 1 being normal user and 5 being admin user.
Problem here is that I cannot get all of the function so show if user has a userlevel 5 (should see all the messages).
Here's how I call the functions from the index.php file:
if($pageid == 1 && $topicID == "")
{
showactive($pageid, $ulevel); // main function for active topics
}
else if($pageid == 2 && $topicID == "")
{
showold($pageid, $ulevel); // main function for old topics
}
else if($pageid == 3 && $topicID == "")
{
shownew($pageid, $ulevel); // main function for new messages
}
else if($pageid == 4)
{
newtopic($pageid, $ulevel); // main function for creating new topic
}
else if($pageid == 5 && $topicID == "")
{
showsearch($pageid, $ulevel); // main function for searching
}
else if($pageid == 6 && $topicID == "")
{
showsearch($pageid, $ulevel);
}
else if(($pageid > 6 && $topicID == "") || $pageid > "a")
{
$pageid = 1;
showactive($pageid, $ulevel); // if $pageid is empty the show the active topics
}
else if(($pageid != "" || $pageid != null) && $topicID != "")
{
viewposts($topicID, $pageid, $ulevel); // show messages
}
else
{
$pageid = 1;
showactive($pageid, $ulevel, $userid); // else show the active topics
}
//below should show the subfunctions inside the main functions
if ($topicID != "" || $pageid == 3 || $pageid == 4 || $pageid == 5 || $pageid == 6)
{
echo "";
}
else
{
if($ulevel >= 1)
{
showtopics_all($ulevel, $refresh, $userid);
}
if($ulevel >= 3)
{
showtopics_family($ulevel, $userid);
}
if($ulevel >= 2)
{
showtopics_friends($ulevel, $userid);
}
if($userid == $fpuser)
{
showgrouptopics($ulevel, $userid);
}
}
What am I doing wrong?
Regards,
Toby
Nesting functions is a bad idea in my opinion, wouldn't it be better to rewrite it in OOP ?
ASKER
Hi,
you are probably right but for now i would like to get this working. Any ideas?
Regards,
Toby
you are probably right but for now i would like to get this working. Any ideas?
Regards,
Toby
Also, there is no reason to try to make them subfunctions. PHP does not recognize them as that. All functions are global in PHP. http://www.php.net/manual/en/functions.user-defined.php
All functions and classes in PHP have the global scope - they can be called outside a function even if they were defined inside and vice versa.
In your function definition for 'showactive', you have one parameter and in your calls, you have two. You should be getting an error for that alone.
ASKER
Hi,
added the missing parameters to the subfunction calls. I have gotten this working on my old intranet layout but now it not working anymore.. this is a real bugger..
added the missing parameters to the subfunction calls. I have gotten this working on my old intranet layout but now it not working anymore.. this is a real bugger..
ASKER
Shouldn't I be able to show all the functions like this:
showtopics_all($ulevel, $refresh);
showtopics_family($ulevel, $userid);
showtopics_friends($ulevel , $userid);
showgrouptopics($ulevel, $userid);
This only show the first showtopics_all info. For some reason only one subfunction can be shown at the same time why is this?
showtopics_all($ulevel, $refresh);
showtopics_family($ulevel,
showtopics_friends($ulevel
showgrouptopics($ulevel, $userid);
This only show the first showtopics_all info. For some reason only one subfunction can be shown at the same time why is this?
With the 'if' structure you have, there is no reason to think that more than one of the functions will run. There is no 'fall-thru' for 'if' statements. The 'if' has to be true for each one in order to run. Something with "$ulevel >= 3" should make each one be true if it also means that "$userid == $fpuser".
ASKER
Hmm.. I'm not really getting this.. I have a working site with this kind of function call using the same if statement and it brings all the results that I want.
If I'm not going to get this working so how can I use php to do this..
example:
level 1 = should only see one message
level 2 = should see level 1 and level 2 messages
level 3 = should see level 1, level 2 and level 3 messages
level 4 = should see level 1, level 2, level 3 and level 4 messages
level 5 = should see level 1, level 2, level 3, level 4 and level 5 messages
Regards,
Toby
If I'm not going to get this working so how can I use php to do this..
example:
level 1 = should only see one message
level 2 = should see level 1 and level 2 messages
level 3 = should see level 1, level 2 and level 3 messages
level 4 = should see level 1, level 2, level 3 and level 4 messages
level 5 = should see level 1, level 2, level 3, level 4 and level 5 messages
Regards,
Toby
ASKER CERTIFIED SOLUTION
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
ASKER
Got it working thanks!
greetings Toube, , Although you seem to think that your code works because of the placement of the "Sub-functions" in the showactive( ) function, this is not the case, (as others have said already), , , it works because you use IF tests later in the code as -
else if($pageid == 2 && $topicID == "")
{
showold($pageid, $ulevel); // main function for old topics
// this has NO relation to being a Sub-function or Not a Sub-function, it just calls a general function named showold( )
}
and later you have -
if($ulevel >= 3)
{
showtopics_family($ulevel, $userid);
// this has NO relation to being a Sub-function or Not a Sub-function, it just calls a general function named showtopics_family( )
}
But you seem to not even use the function parameters = ($pageid, $ulevel) or ($ulevel, $userid) because they are mixed up and inconsistent in your coding.
I would think that you should Not use your current function methods, and try to have some consistency in the way you use and have parameters in your code for a function, you might consider something like -
but you should look to use something that is more a PHP coding, since sub-functions do not mean anything in php
else if($pageid == 2 && $topicID == "")
{
showold($pageid, $ulevel); // main function for old topics
// this has NO relation to being a Sub-function or Not a Sub-function, it just calls a general function named showold( )
}
and later you have -
if($ulevel >= 3)
{
showtopics_family($ulevel,
// this has NO relation to being a Sub-function or Not a Sub-function, it just calls a general function named showtopics_family( )
}
But you seem to not even use the function parameters = ($pageid, $ulevel) or ($ulevel, $userid) because they are mixed up and inconsistent in your coding.
I would think that you should Not use your current function methods, and try to have some consistency in the way you use and have parameters in your code for a function, you might consider something like -
function showactive($ulevel) {
switch ($ulevel) {
case 5: echo "this is Level 5 messaage - ";
case 4: echo "this is Level 4 messaage - ";
case 3: echo "this is Level 3 messaage - ";
case 2: echo "this is Level 2 messaage - ";
case 1: echo "this is Level 1 messaage - ";
break;
default: echo "this is ERROR MESSAGE ";
}
}
// you might call this like
showactive($ulevel) ;
// or like below
else if($pageid == 2 && $topicID == "")
{
showactive(2) ;
}
but you should look to use something that is more a PHP coding, since sub-functions do not mean anything in php