Jump to content

Recommended Posts

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">
<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>

 

Link to comment
https://forums.phpfreaks.com/topic/275985-problem-with-php-code/
Share on other sites

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>
This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.