Function Help!

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
William
Forum Contributor
Posts: 332
Joined: Sat Oct 25, 2003 4:03 am
Location: New York City

Function Help!

Post by William »

I made a function called: page(), It's not working. Here is the function script:

Code: Select all

<?php

function page($url, $title, $type)
{
	if($type == "memberonly") {
		if(session_is_registered("loggedin") && $page != "logout") {

			case $title:
			include("$url");
			break;

		} else {

			case $title:
			include("pages/error.php");
			break;

		}

	} elseif($type == "any") {
	
	case $title:
	include("$url");
	break;

	} elseif($type == "guest") {
		if(!session_is_registered("loggedin") {
			case $title:
			include("$url");
			break;
		} else {
			case $title:
			include("pages/error.php");
			break;
		}

	}
}
?>
And here is the use of the function:

Code: Select all

<?php

include("functions/page.php");

switch ($page) {

page("pages/attackresult.php", "attackresult", "memberonly");

page("pages/register.php", "register", "guest");

page("pages/aboutus.php", "aboutus", "any");

page("pages/profile.php", "profile", "memberonly");

page("pages/research.php", "research", "memberonly");

page("pages/topexp.php", "topexp", "any");

page("pages/topgold.php", "topgold", "any");

page("pages/top10002.php", "top10002", "any");

page("pages/mcc.php", "mcc", "memberonly");

page("pages/inbox.php", "inbox", "memberonly");

page("pages/sendmessage.php", "sendmessage", "memberonly");

page("pages/clan.php", "clan", "memberonly");

page("pages/shop1.php", "shop1", "memberonly");

page("pages/shop2.php", "shop2", "memberonly");

page("pages/shop3.php", "shop3", "memberonly");

page("pages/shop4.php", "shop4", "memberonly");

page("pages/minegold.php", "minegold", "memberonly");

page("pages/help.php", "help", "any");

page("pages/top1000.php", "top1000", "any");

page("pages/news.php", "news", "any");

page("pages/login.php", "login", "guest");

page("pages/gold.php", "gold", "memberonly");

page("pages/rank.php", "rank", "any");

page("pages/attack.php", "attack", "memberonly");

page("pages/logout.php", "logout", "memberonly");

page("pages/shop.php", "shop", "memberonly");

page("pages/info.php", "info", "any");

page("pages/heroinfo1.php", "heroinfo1", "any");

page("pages/lightinfo.php", "lightinfo", "any");

page("pages/darkinfo.php", "darkinfo", "any");

page("pages/heroinfo2.php", "heroinfo2", "any");

page("pages/heroinfo3.php", "heroinfo3", "any");

page("pages/heroinfo4.php", "heroinfo4", "any");

page("pages/raidersinfo.php", "raidersinfo", "any");

page("pages/incognitosinfo.php", "incognitosinfo", "any");

page("pages/barbariansinfo.php", "barbariansinfo", "any");

page("pages/earthlingsinfo.php", "earthlingsinfo", "any");

default:
	include("pages/home.php");
}
?>
it keeps saying: Parse error: parse error, expecting `T_CASE' or `T_DEFAULT' or `'}'' in /home/cheekuon/public_html/inc/pages.php on line 16

Any ideas?
magicrobotmonkey
Forum Regular
Posts: 888
Joined: Sun Mar 21, 2004 1:09 pm
Location: Cambridge, MA

Post by magicrobotmonkey »

Umm I don't think that will do what you think it wil. I can see what you're trying to do, but its not quite right. I think you would have to return the

case $title:
include("$url");
break;

as a string then eval it
User avatar
William
Forum Contributor
Posts: 332
Joined: Sat Oct 25, 2003 4:03 am
Location: New York City

Post by William »

huh?
User avatar
patrikG
DevNet Master
Posts: 4235
Joined: Thu Aug 15, 2002 5:53 am
Location: Sussex, UK

Post by patrikG »

[php_man]switch[/php_man]
User avatar
William
Forum Contributor
Posts: 332
Joined: Sat Oct 25, 2003 4:03 am
Location: New York City

Post by William »

I know switch. It worked fine till I add my new function.
User avatar
Weirdan
Moderator
Posts: 5978
Joined: Mon Nov 03, 2003 6:13 pm
Location: Odessa, Ukraine

Post by Weirdan »

You need to have your swith inside the function. (or your cases outside of it)
User avatar
launchcode
Forum Contributor
Posts: 401
Joined: Tue May 11, 2004 7:32 pm
Location: UK
Contact:

Post by launchcode »

Apart from the fact your case statements are totally invalid because they're not inside a switch block, as Weirdan has pointed out, why are you even bothering with them? I can't see any reason for them - why not just pass in the file name of the page to include? That is in the first script - in the second script you're missing your case statements totally!
User avatar
William
Forum Contributor
Posts: 332
Joined: Sat Oct 25, 2003 4:03 am
Location: New York City

Post by William »

Ok, Case is ourside. The function should be prining the case thing. The function checks if the user is loggin. if it is set memberonly. Not to check if it is any. And checks if your not loggin if its guest. Now do you understand?
jason
Site Admin
Posts: 1767
Joined: Thu Apr 18, 2002 3:14 pm
Location: Montreal, CA
Contact:

Post by jason »

The Hidden Viper wrote:Ok, Case is ourside. The function should be prining the case thing. The function checks if the user is loggin. if it is set memberonly. Not to check if it is any. And checks if your not loggin if its guest. Now do you understand?
No. That's wrong.

Your switch is wrong. It doesn't have any case. You can't have your case sitting inside a function. It won't work.

Your case statements need to be inside the switch, not inside functions.

Read: http://www.php.net/switch

Then continue/
User avatar
William
Forum Contributor
Posts: 332
Joined: Sat Oct 25, 2003 4:03 am
Location: New York City

Post by William »

o, lol, So I got to add function inside the case. Dang going to make it longer. :-|, k thx
Post Reply