benjahnee Posted March 21, 2013 Share Posted March 21, 2013 hi guys i haver a problem with my code it works but also shows an error when i run it the line of code where there is an error is this <code> <?php echo ($_SESSION['switchcss'])? $_SESSION['switchcss']: '<link href="css/alternate.css" type="text/css" rel="stylesheet">';?> <code> its for a php based css style switcher. if u need to know all of the code i am using to diagnose the problem i will leave it below, thanks alot guys <code> <?php include ("inc/css.php"); ?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>Untitled Document</title> </head> <body> <div id="header"> </div> <?php if(isset($_GET['css'])){ switch ($_GET['css']) { case 'main': $stylesheet = '<link rel="stylesheet" type="text/css" href="css/main.css">'; $_SESSION['switchcss']=$stylesheet; break; case 'alternate2': $stylesheet = '<link rel="stylesheet" type="text/css" href="css/alternate2.css">'; $_SESSION['switchcss']=$stylesheet; break; case 'alternate3': $stylesheet = '<link rel="stylesheet" type="text/css" href="css/alternate3.css">'; $_SESSION['switchcss']=$stylesheet; break; // Our default stylesheet default: $stylesheet = '<link rel="stylesheet" type="text/css" href="css/alternate.css">'; $_SESSION['switchcss']=$stylesheet; } } ?> <?php echo ($_SESSION['switchcss'])? $_SESSION['switchcss']: '<link href="css/alternate.css" type="text/css" rel="stylesheet">';?> <a href="index1.php?css=main">[style 1]</a> |<a href="index1.php?css=alternate">[style 2]</a> | <a href="index1.php?css=alternate2">[style 3]</a> <a href="index1.php?css=alternate3">[style 4]</a> <code> Quote Link to comment https://forums.phpfreaks.com/topic/275985-problem-with-php-code/ Share on other sites More sharing options...
requinix Posted March 21, 2013 Share Posted March 21, 2013 And the error is... what? Undefined offset? Quote Link to comment https://forums.phpfreaks.com/topic/275985-problem-with-php-code/#findComment-1420183 Share on other sites More sharing options...
benjahnee Posted March 21, 2013 Author Share Posted March 21, 2013 Notice: Undefined variable: _SESSION in F:\EasyPHP-12.1\www\scripting\inc\header.php on line 42 i dont understand why i get this?? Quote Link to comment https://forums.phpfreaks.com/topic/275985-problem-with-php-code/#findComment-1420187 Share on other sites More sharing options...
PaulRyan Posted March 21, 2013 Share Posted March 21, 2013 Try something like this: <?PHP session_start(); //### Array of style sheets $stylesheets = array('main' => 'main.css', 'alternate2' => 'alternate2.css', 'alternate3' => 'alternate3.css' ); //### Set the default stylesheet $stylesheet = 'alternate.css'; //### If $_GET['css'] is available, changing CSS if(isset($_GET['css'])) { if(isset($_GET['css']) && array_key_exists($_GET['css'], $stylesheets)) { $stylesheet = $stylesheets[$_GET['css']]; } $_SESSION['css'] = $stylesheet; //### No $_GET['css'], so use the session value } else if(!empty($_SESSION['css'])) { $stylesheet = $_SESSION['css']; } $stylesheetHTML = '<link href="css/'. $stylesheet .'" type="text/css" rel="stylesheet">'; ?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR...ransitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>Untitled Document</title> <?PHP echo $stylesheetHTML;?> </head> <body> <div id="header"> </div> <a href="index1.php?css=main">[style 1]</a> | <a href="index1.php?css=alternate">[style 2]</a> | <a href="index1.php?css=alternate2">[style 3]</a> | <a href="index1.php?css=alternate3">[style 4]</a> <br><br> Current Stylesheet: <?PHP echo $stylesheet;?> </body> </html> Quote Link to comment https://forums.phpfreaks.com/topic/275985-problem-with-php-code/#findComment-1420191 Share on other sites More sharing options...
requinix Posted March 21, 2013 Share Posted March 21, 2013 You can't use $_SESSION until you've called session_start(). Quote Link to comment https://forums.phpfreaks.com/topic/275985-problem-with-php-code/#findComment-1420192 Share on other sites More sharing options...
benjahnee Posted March 22, 2013 Author Share Posted March 22, 2013 thanks for the advice guys very helpful all is now sorted Quote Link to comment https://forums.phpfreaks.com/topic/275985-problem-with-php-code/#findComment-1420280 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.