CraigSherwood Posted December 6, 2007 Share Posted December 6, 2007 Seemingly it is possible have an included php file 'pretend' to be a css file. Thereby you can use a variable within a css file to set, for example font-size: <?php $newsize ?>% So, I was wondering, anyone know how to do this: 1. Create a session variable to store the user's choice, i.e <?php $newsize ?>, probably from a mouse click 2. In the css file, run a function to determine if the session exists. If it does, determine whether to increment or decrement the value, then assign that value to the style definitions. If it doesn't, then assign a default value. I guess the code in the css file would be something like <?php session_start(); if (empty($_SESSION['newsize'])) { $_SESSION['newsize'] = 80; } else { $_SESSION['newsize']=$_SESSION['newsize']+5; } ?> For a beginning, that's ok. But, how do I submit a variable with an onClick. I'm guessing via a form submit, but not too sure. I know that most forums tell me it can't be done. But, given that a css file can run php functions, there must be a way. Many thanks, Craig Quote Link to comment Share on other sites More sharing options...
pocobueno1388 Posted December 6, 2007 Share Posted December 6, 2007 Yes, you can incorporate PHP into CSS. The file must be named with a .php extension though. font-size: <?php echo $newsize; ?>% Quote Link to comment Share on other sites More sharing options...
CraigSherwood Posted December 6, 2007 Author Share Posted December 6, 2007 Thank you. I realise that the CSS file must be named with a php extension. My question, badly put though it may have been, was how to pass PHP variables to the CSS file. Quote Link to comment Share on other sites More sharing options...
nafetski Posted December 6, 2007 Share Posted December 6, 2007 <?php session_start(); if (empty($_SESSION['newsize'])) { ?> put any CSS that you want here <?php } else { ?> put any CSS that you want here <?php } ?> Is this what you're looking for? Quote Link to comment Share on other sites More sharing options...
CraigSherwood Posted December 6, 2007 Author Share Posted December 6, 2007 That's close, but not quite. I need to store the value in a session variable so that its available on all pages. I can already do that. Somehow I need to be able to change the value of the session variable. I.e the user clicks the font size buttons, to increment or decrement the session value. That in turn is read by the css file and the font size is changed accordingly. Maybe I've got it the wrong way around. The User clicks the buttons, a variable is sent to the css file, which in turn stores the new value in a session variable? Quote Link to comment Share on other sites More sharing options...
revraz Posted December 6, 2007 Share Posted December 6, 2007 And then what happens when they log out? Are you saving this file for each user? Would be easier to just make some templates and let them choose one to use. Quote Link to comment Share on other sites More sharing options...
nafetski Posted December 6, 2007 Share Posted December 6, 2007 I think I know what you're shooting for here. Rather than changing the session variable, I'd probably just make a new one. $_SESSION['size1'] $_SESSION['size2'] $_SESSION['size3'] Then maybe run something like this in your CSS file? <?php switch ($size) { case $_SESSION['size1']; echo "CSS code for size1"; break; case $_SESSION['size2']; echo "CSS code for size2"; break; case $_SESSION['size3']; echo "CSS code for size3"; break; } ?> Haven't tested it - but it might be a place to start! Cookies are a great idea too. Quote Link to comment Share on other sites More sharing options...
nafetski Posted December 6, 2007 Share Posted December 6, 2007 Oh as far as setting the session variable (I wouldn't know how to do it with an onclick)...I had to do something similar for a page with english/spanish translations. To set the session variable I made a spanish.php and english.php page that was loaded after the link was clicked. <?php session_start(); $_SESSION['size1'] = $value; Header ("Location:" . $_SESSION['currentpage'] ); exit; ?> The currentpage session variable was set in the header of the page that had the content. $_SERVER['PHP_SELF'] = $_SESSION['currentpage']; Like I said, probably a horribly inefficient way to accomplish your goal...but horribly inefficient is how I roll (for now =P) -Nafetski Quote Link to comment Share on other sites More sharing options...
CraigSherwood Posted December 8, 2007 Author Share Posted December 8, 2007 Many thanks to one and all for sundry clues. I think I've cracked it; tested in Mozilla, Firefox, Opera, IE6, Netscape. Seems to work everywhere. Hurrah. The following will allow links to set a percentage to use in a php file (pretending to be a css file) thereby changing the font-size for the user. Uses sessions because I didn't want to rely on cookies, avoids javascript because that too can be turned off. Maybe I should use cookies if they're enabled? Page 1 This has the following set-up <?php include("css/phpfunctions.php"); ?> <!-- Put IE into quirks mode --> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" > <head> <meta http-equiv=Content-Type content="text/html; charset=UTF-8" /> <link href="css/cdcacss.php" rel="stylesheet" type="text/css" /> <title>Page 1 : <?php echo 'Session : '.$_SESSION['sesscss']; ?></title> </head> <body> <p><a href="page2.php">to page 2</a></p> <p><a href="<?php echo $_SERVER['PHP_SELF']; ?>?nmbr=5">change up</a></p> <p><a href="<?php echo $_SERVER['PHP_SELF']; ?>?nmbr=-5">change down</a></p> <p>session : <?php echo $_SESSION['sesscss']; ?></p> <p>nmbr : <?php echo $_REQUEST['nmbr']; ?></p> </body> </html> Note that the css stylesheet is actually a php file - css/cdcacss.php" rel="stylesheet". The change up/down links send the page with variable and value, nmbr, back to itself. This is intercepted by the file - phpfunctions.php phpfunctions.php This file, apart from other fabulous things, has this in it:- <?php session_start(); if (!isset($_SESSION['sesscss'])) { $_SESSION['sesscss'] = 80; session_register('sesscss'); } else { $nmbr=$_REQUEST['nmbr']; $_SESSION['sesscss'] = $_SESSION['sesscss'] + $nmbr; unset($nmbr); } ?> Looks to see if the session has started, assigns a default value if not, or adds $nmbr to the current value. Page 1 therefore loads with the session started and assigned a value or increments/decrements the existing value. This value is then picked up by the css file. cdcacss.php This, for the sake of simplicity, merely has this in it (currently) <?php session_start(); $size=$_SESSION['sesscss'];{ ?> <?php header('Content-type: text/css'); ?> HTML { font-size: <?php echo $size; ?>%; font-family: Verdana, Arial, Helvetica, sans-serif; } <? } ?> IE and Opera worked without the line for header(....), but Mozilla flavours required it to function. Also the session_start() needed to be declared again to access the session value. Page 2 Just to verify that everything is functioning, this merely has in it <?php include("css/phpfunctions.php"); ?> at the top of the page, and once again the link to the css file <link href="css/cdcacss.php" rel="stylesheet" type="text/css"> . Everything seems ok. Problem If I send the page back to itself, nmbr, has a value. This is demonstrated by the line <p>nmbr : <?php echo $_REQUEST['nmbr']; ?></p> . However, it shouldn't, because the functions.php file has in it unset($nmbr); . Obviously it isn't actually deleting the value. Anyone know why? Or even better how to genuinely delete it? Reason being that if I refresh the page while nmbr has a value, the font size is being incremented/decremented. Which is ok, but not really. Many thanks, Craig Quote Link to comment Share on other sites More sharing options...
DyslexicDog Posted December 8, 2007 Share Posted December 8, 2007 To have php parse your css file and have the browser accept it as a css file you need to name your file with a php extension and add this code to the top of the page. header("Content-type: text/plain"); or if you are running apache you can add an entry to your .htaccess file to have it parse the css file as a php document. Quote Link to comment Share on other sites More sharing options...
CraigSherwood Posted December 8, 2007 Author Share Posted December 8, 2007 I guess you didn't notice this bit in the file named cdcacss.php? <?php header('Content-type: text/css'); ?> It all works exactly as the doctor ordered, with the exception of the unset part. Any ideas about that? Quote Link to comment 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.