Page 1 of 1

loop in switch

Posted: Wed Feb 02, 2011 6:43 am
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

Re: loop in switch

Posted: Wed Feb 02, 2011 8:32 am
by VladSun
Forget about the switch :)
Use the array itself as a lookup table.

Re: loop in switch

Posted: Wed Feb 02, 2011 8:54 am
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

Re: loop in switch

Posted: Wed Feb 02, 2011 9:37 am
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.

Re: loop in switch

Posted: Wed Feb 02, 2011 9:48 am
by Domsore
Oh, I think I just got confused, I understand what you mean.

Thanks a million :D