loop in switch

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
Domsore
Forum Commoner
Posts: 46
Joined: Wed Jan 26, 2011 7:07 pm

loop in switch

Post by Domsore »

Okay so I am trying to make variable case names.

So I have this:

Code: Select all


       $handle = opendir(TPATH_BASE.DS.'content');

	for($i=0; false !== ($file = readdir($handle)); $i++){

	    if ($file !== '' && $file !== 'default.php'){

		    $case[$i] = $file;
		
	    }
	}

	$count = count($case);

	switch ($frame){

	for($x=0; $x<=$count; $x++){
	    
	    $cas[$x] = strstr($case[$x], '.', TRUE);

	    case $cas[$x]:
		include_once (TPATH_BASE.DS.'content'.$case[$x]);
		break;
	}
	
	default:
	    include_once (TPATH_BASE.DS.'content'.$case[x].'default');
	}
the error I get is:

Parse error: syntax error, unexpected T_FOR, expecting T_CASE or T_DEFAULT or '}' in ------------------------\template.php on line 99

So I am assuming this means I cant have a for statement in a switch so is there any other way around this? I've been trying too many things but having no luck

Cheers,

Dom
User avatar
VladSun
DevNet Master
Posts: 4313
Joined: Wed Jun 27, 2007 9:44 am
Location: Sofia, Bulgaria

Re: loop in switch

Post by VladSun »

Forget about the switch :)
Use the array itself as a lookup table.
There are 10 types of people in this world, those who understand binary and those who don't
Domsore
Forum Commoner
Posts: 46
Joined: Wed Jan 26, 2011 7:07 pm

Re: loop in switch

Post by Domsore »

Okay I googled 'Array lookup table PHP' as I have no idea what that is... and I must admit. I still have no idea haha... any suggested reading material?

Cheers
User avatar
VladSun
DevNet Master
Posts: 4313
Joined: Wed Jun 27, 2007 9:44 am
Location: Sofia, Bulgaria

Re: loop in switch

Post by VladSun »

OK, you already have:

Code: Select all

       $handle = opendir(TPATH_BASE.DS.'content');

        for($i=0; false !== ($file = readdir($handle)); $i++){

            if ($file !== '' && $file !== 'default.php'){

                    $case[$i] = $file;
                
            }
        }
so in $case you have all files that are "allowed" to be included.

So, you should check the value of $frame by using the in_array() function. If it's found then include it, if not include the default file.

Another way to do the same is to use file_exist() function, without iterrating through the directory.
There are 10 types of people in this world, those who understand binary and those who don't
Domsore
Forum Commoner
Posts: 46
Joined: Wed Jan 26, 2011 7:07 pm

Re: loop in switch

Post by Domsore »

Oh, I think I just got confused, I understand what you mean.

Thanks a million :D
Post Reply