[nycphp-talk] translating behaviors from JavaScript to PHP
Keith Richardson
keith.richardson at thompsonhealth.com
Wed Dec 3 07:54:54 EST 2003
In my opinion, to accomplish that I would probabally use sessions, or a get
variable.
in the top, you would have something like this:
<?php
$current_stylesheet = "default.css";
if (!empty($_GET['stylesheet']))
{
switch($_GET['stylesheet'])
{
case "red":
$current_stylesheet = "red.css";
break;
case "blue":
$current_stylesheet = "blue.css";
break;
}
}
?>
Then where you display the style sheet, you would do something like this in
the html code:
<link rel="stylesheet"
type="text/css" href="<?php print($current_stylesheet); ?>" />
So when you have a list of stylesheets to use, it just reloads the page, say
index.php, as index.php?stylesheet=red - and it will load the red style
sheet. If they did not enter a style sheet, or it is not one of your
options, then it would load the default style sheet.
the thing with this one, is that you have to pass the get variable to each
page that you want to have customized style sheets, which can be a lot of
reworking, because you have to add something like this to each link:
<A href="links.php<?php if ($current_stylesheet != "default.css") {
print("?stylesheet=".$_GET['stylesheet']); } ?>">Links</a>
This prints out the ?stylesheet=red (as an example) after the link, if red
is selected. if there is no option, it wont print anything after the link.
The other way to do it is with sessions, which for me is easier.
on the top of the page, do something like
<?php
session_name("mysite.com_css");
session_start();
if (empty($_SESSION['stylesheet']))
$_SESSION['stylesheet'] = "default.css";
if (!empty($_GET['stylesheet']))
{
switch($_GET['stylesheet'])
{
case "red":
$_SESSION['stylesheet'] = "red.css";
break;
case "blue":
$_SESSION['stylesheet'] = "blue.css";
break;
}
}
?>
Then where you display the style sheet, you would do something like this in
the html code:
<link rel="stylesheet"
type="text/css" href="<?php print($_SESSION['stylesheet']); ?>" />
This way, you can change the stylesheet with the GET request, say
page.php?stylesheet=red - and it will change the stylesheet in the users
session. Since the sessions are saved for the browser session, it will keep
the session variables for each page that the user loads, and thus keeping
their selected style with each page, without having to add any more get
requests.
If it were me, I would add the php code that checks the session
variables/get variables in an include file, say change_css.php - and include
it on the top of every file, or in your header file. This way its easier to
add another option for a different CSS.
-----Original Message-----
From: talk-bounces at lists.nyphp.org
[mailto:talk-bounces at lists.nyphp.org]On Behalf Of -sry
Sent: Wednesday, December 03, 2003 7:43 AM
To: NYPHP Talk
Subject: Re: [nycphp-talk] translating behaviors from JavaScript to PHP
On Tuesday, December 02, 2003 12:36 PM
"Hans Zaunere" <hans not junk at nyphp.com>
> -sry wrote:
> > [ repost - sent to the wrong list ]
> > [snip]
> >>vis my personal web site. Since I like to keep code as
> >>modularized as possible, I separated out my JavaScript
> >>(for mouseover behavior and other sillyness) into a .js file.
> >>As a mechanism for putting my hands on some PHP as I
> >>learn it, I figured I'd take the existing page design and
> >>behavior and "translate" it into a PHP implementation.
>
> Keep in mind that PHP runs strictly on the server - the
> browser is never aware of any PHP code. Granted, you
> can use PHP to generate the client-side code the browser
> then reads and parses, but translating between Javascript
> and PHP - in a linear sense - is not possible.
I guess my subject line wording is a bit misleading. I don't
mean a LITERAL translation. I meant to say I am trying
to translate my thinking into how to design the same
results/behavior with a different approach, a PHP approach.
There is always more than one way to skin any cat and I
am not used to thinking in PHP terms--as is apparent from
my newbie post where I was embedding HTML in PHP
rather than PHP into HTML which seems obvious now
that some of you have pointed it out to me :-) Thanks for
the tips, guys.
I'm asking for more "tips" like this, how to approach, in
PHP, doing things like overwriting DIVs to dynamically
load content or how to replace stylesheets based on
user prefs using PHP rather than JS...such as the JS
code described at:
http://www.alistapart.com/articles/alternate/
Maybe some of these behaviors are simply not appropriate
for a PHP implementation - I dunno - that's why I'm asking
for your opinions :)
Maybe it'd better to ask, how would I go about "retooling"
existing functionality from JS to PHP? Assuming the
functionality is *not* specifically better-suited for a client-side
scripting. Better way to ask? :)
-sry
_______________________________________________
talk mailing list
talk at lists.nyphp.org
http://lists.nyphp.org/mailman/listinfo/talk
More information about the talk
mailing list