having a switch within a switch using the URL as control

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
MrPotatoes
Forum Regular
Posts: 617
Joined: Wed May 24, 2006 6:42 am

having a switch within a switch using the URL as control

Post by MrPotatoes »

well, that's my problem. i'm trying to use the URL to do the main switch statement and then i have another doing another within that main switch. here is what i'm doing:

this is the URL
http://localhost/framework/index.php?pe ... gin=forums

op=index -> this defines the main
adminPlugin=forums -> this is the switch that is within the switch

this is the main switch case:

Code: Select all

switch ('op')
{				
	case 'index':
	// do stuff
	break;

	case 'process':
	// do stuff
	break;

	default:
	// oh noes.  you suck
}
this is what i want to do:

Code: Select all

switch ('op')
{				
	case 'index':
	break;

	case 'adminPlugin':  // remember this was explained up top :thumbup:
	
		switch($adminPlugin)
		{
			case 'forums'
			echo 'omg it worked!';
			break;
		}
	
	break;

	default:
	// oh noes.  you suck
}
so basically i'm stumped. i can do that second switch outside but what's the point? i don't think there is a good point to that. what do you think?
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post by John Cartwright »

Code: Select all

switch ('op')
{
vs.

Code: Select all

switch($_GET['op'])
{
Although you still may want to check for the variables existance before using it.
bike5
Forum Newbie
Posts: 6
Joined: Mon Jul 03, 2006 7:31 pm

Post by bike5 »

also don't forget the other ":"

Code: Select all

switch($adminPlugin)
                {
                        case 'forums'
                        echo 'omg it worked!';

Code: Select all

switch($adminPlugin)
                {
                        case 'forums':
                        echo 'omg it worked!';
User avatar
MrPotatoes
Forum Regular
Posts: 617
Joined: Wed May 24, 2006 6:42 am

Post by MrPotatoes »

haha, ok. it's pusedo code. i already am doing that. i did that so you can read it better ;)
User avatar
MrPotatoes
Forum Regular
Posts: 617
Joined: Wed May 24, 2006 6:42 am

Post by MrPotatoes »

you can close this thread. i decided i know what i'm going to do. i'm not a fan but it works so i guess i'll quit bitchin. it's not as cool as i would like but whatever works works and i'm happy with that

less parsing as well so i guess i should stop complaining
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post by John Cartwright »

MrPotatoes wrote:haha, ok. it's pusedo code. i already am doing that. i did that so you can read it better ;)
Please let us know it is pseudo code then.
MrPotatoes wrote:you can close this thread. i decided i know what i'm going to do. i'm not a fan but it works so i guess i'll quit bitchin. it's not as cool as i would like but whatever works works and i'm happy with that

less parsing as well so i guess i should stop complaining
Well what did you decide to do? I don't think you were "bitching".. were here to help :wink:
User avatar
MrPotatoes
Forum Regular
Posts: 617
Joined: Wed May 24, 2006 6:42 am

Post by MrPotatoes »

first i have to say that you people post too fast lol

i'm already on the second page and it's not even been a full day!

secondly this is what i did mroe or less:

Code: Select all

switch ('op')
{				
	case 'index':
	$this->renderAdmin('template.tpl');
	break;

	case 'adminPlugin':
	// ?op=index&adminPlugin=a_Plugin_To_Admin
	$dookie = $_GET['administrate'];
	require_once 'plugins/_' . $dookie . '/admin/_' . $dookie . '.admin.php';
	break;

	default:
	echo 'throw error here';
}

now, not thinking ahead of time that means that i've got to do the same to the other one. so i'm going to have to define one more URL param for this controller to work with and i'm good. i guess i've had to develop subcontrollers sooner than i thought. i knew i would have to but oh well. what can you do eh?
User avatar
Jenk
DevNet Master
Posts: 3587
Joined: Mon Sep 19, 2005 6:24 am
Location: London

Post by Jenk »

I settled for multi-tier controllers.

main controller (index.php):

Code: Select all

<?

include_once 'includes/configs/config.php';

$page = (!empty(${'_' . $METHOD}['page']) ? ${'_' . $METHOD}['page'] : '');

if (!in_array($page, $CONFIG['WHITELIST']['PAGES'])) die ('invalid page action');

switch ($page) {
    case 'auth':
        $controller = 'auth.inc.php';
        break;
    case 'reg':
        $controller = 'register.inc.php';
        break;
    //etc..

    default:
        $controller = 'welcome.inc.php';
}

include_once 'includes/controllers/' . $controller;

?>
auth.inc.php:

Code: Select all

<?php

$action = (!empty(${'_' . $METHOD}['action']) ? ${'_' . $METHOD}['action'] : '');

if (!in_array($method, $CONFIG['WHITELIST']['ACTION'])) die('Invalid action specified.');

switch ($action) {
    case 'login':
        $user = new User (${'_' . $METHOD}['user'], ${'_' . $METHOD}['pass']);
        break;
 //etc..
}

?>
Left out some stuff for the sake of making an example before anyone points it out.
User avatar
MrPotatoes
Forum Regular
Posts: 617
Joined: Wed May 24, 2006 6:42 am

Post by MrPotatoes »

that's more-or less the way that i have it setup
Post Reply