Page 1 of 1

Multiple Switches... with the same criteria

Posted: Sat Apr 10, 2010 6:01 am
by hypedupdawg
I have recently been trying to write a section of code that will allow a video to be shown either at it's default size, or at a user value. To do this I have been using two switch statements like this:

Code: Select all

switch ($vidinfo[2])
		{
		case "small":
			$vidwidth = 400;
			break;
		case "medium":
			$vidwidth = 600;
			break;
		case "large":
			$vidwidth = 800;
			break;
		default:
			break;
		}
	switch ($size)
		{
		case "small":
			$vidwidth = 400;
			break;
		case "medium":
			$vidwidth = 600;
			break;
		case "large":
			$vidwidth = 800;
			break;
		default:
			break;
		}
What I want to know is - is there any way to always give these statement the same criteria? I tried this:

Code: Select all

switch ($vidinfo[2])
		{
		include($_SERVER['DOCUMENT_ROOT'] . "/videos/switch.txt");
		}
	switch ($size)
		{
		include($_SERVER['DOCUMENT_ROOT'] . "/videos/switch.txt");
		}
But it throws the error "unexpected T_INCLUDE". Is there any way to do this - maybe using variable variables?
Thanks a lot :)

Re: Multiple Switches... with the same criteria

Posted: Sat Apr 10, 2010 1:49 pm
by requinix
How do you determine which one to use? What if $vidinfo[2]=small and $size=medium?

Re: Multiple Switches... with the same criteria

Posted: Sat Apr 10, 2010 2:12 pm
by minorDemocritus
Almost every time I get the Unexpected T_something error, it's because I've forgotten a semicolon above the line that pops the error... check for that.

Re: Multiple Switches... with the same criteria

Posted: Sat Apr 10, 2010 2:41 pm
by wurdup
why cant you use a function that's what theyre for. I'm not an expert on PHP security but I dont think it's recommended using include for a txt file.

Code: Select all

$vidwidth=400;

check($size);
check($othervar[2]);

function check($var)
                {
                switch($var)
                case "small":
                        $vidwidth = 400;
                        break;
                case "medium":
                        $vidwidth = 600;
                        break;
                case "large":
                        $vidwidth = 800;
                        break;
                default:
                        break;
                }


        

Re: Multiple Switches... with the same criteria

Posted: Sat Apr 10, 2010 3:41 pm
by hypedupdawg
thanks for everyone's great response... thanks for reminding me of the (ever present) function option - it works great now with roughly the same syntax as wurdup suggested.

In answer to tasairis, I used an if statement to use the $size variable only if it was present:

Code: Select all

if ($size != "")
		{
		$vidwidth = choosesize($size);
		}
	else
		{
		$vidwidth = choosesize($vidinfo[2]);
		}
I'll look in the other forums for the security facts :D thanks to all of you

Re: Multiple Switches... with the same criteria

Posted: Sat Apr 10, 2010 4:11 pm
by requinix
The issue is that you're misusing switch. It has a very specific form and you have to stick to it.

But yeah, stick it in a function.
And also, putting PHP code in a .txt file, especially one that's accessible over the web, is a very bad idea. Stick to .php.