Page 1 of 1

Switch Statements[solved,read solution!]

Posted: Sun Nov 06, 2005 12:16 pm
by Charles256
Okay..Here's the deal.. I want to dynamically generate a switch statement;) Reason why is because I'm trying to think of a way to completely automate my webpage creation and all;)
here's what I got...

Code: Select all

$sql="SELECT * FROM case WHERE switch='content'";
		$query=mysql_query($sql);
		switch ($content)
		{			
			while ($test=mysql_fetch_object($query))
			{
				case "".$test->cases."":
					include("".$test->includes."");
					break;
			}
			
			default:
				include ("main.php");
				break;
		}
and in my database...

Code: Select all

switch  	 cases  	 includes
content 	ct 	ct.php
content 	c 	c.php
content 	phpt 	phpt.php
content 	php 	php.php
content 	me 	me.php
content 	site 	site.php
content 	resume 	resume.php
content 	resume2 	resume2.php
needless to say once I run the script I get an error.. to be exact..

Code: Select all

Parse error: parse error, unexpected T_WHILE, expecting T_CASE or T_DEFAULT or '}' in /home/esitemai/public_html/index.php on line 65
any one care to speculate?

Posted: Sun Nov 06, 2005 1:14 pm
by Charles256
and here is what I came up with...
the main page...

Code: Select all

if (!isset($_REQUEST['content']))
		{
			$content="BOO";
		}
		else
		{
			$content=$_REQUEST['content'];
		}
		$sql="SELECT * FROM cases WHERE switches='content'";
		$query=mysql_query($sql);
		while ($test=mysql_fetch_object($query))
		{
			ifswitch("$content","$test->cases","$test->includes");
		}
and my lovely little function...

Code: Select all

function ifswitch($content,$cases,$includes)
{
	if($content==$cases)
	{
		include("$includes");
	}
}
granted it's a bunch of if statements but it allows for me to theoretically completely automate page creation..we'll see..

Posted: Sun Nov 06, 2005 1:16 pm
by foobar

Posted: Sun Nov 06, 2005 1:22 pm
by Charles256
i wanted to avoid the eval statement :-/ and I did thank ya very much;)

Posted: Sun Nov 06, 2005 5:23 pm
by feyd
there's no reason for eval() or the odd if function you came up with that I can see..

Code: Select all

if (!isset($_REQUEST['content']))
{
  $content="BOO";
}
else
{
  $content=$_REQUEST['content'];
}
$sql="SELECT * FROM cases WHERE switches='content'";
$query=mysql_query($sql);
$acceptancePool = array();
while ($test=mysql_fetch_assoc($query))
{
  $acceptancePool[$test['includes']] = $test['cases'];
}

if(($key = array_search($content,$acceptancePool)) === false) {
  $key = 'BOO.php';
}

if(is_readable($key)) {
  include($key);
}