SitePoint Sponsor

User Tag List

Results 1 to 10 of 10
  1. #1
    SitePoint Addict
    Join Date
    Aug 2006
    Location
    Washington DC
    Posts
    215
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Stupid CSS Switcher with PHP question

    Hey all!

    I was following this http://www.sitepoint.com/article/css...style-switcher tutorial and all was going well until I uploaded all of my results to my server. As you can see here I've linked the php file and put the right PHP code in the HTML file.... I think I've done everything else correctly as well, except when I go to click on the change the stylesheet option, it just brings me to a blank page.... What have I done wrong?

    the code in the external PHP file is
    Code:
    <?php
    $theStyle    = $HTTP_GET_VARS["style"]; /* querystring */
    setcookie("style", $theStyle, time()+36000, "/", "");
    header("Location: $HTTP_REFERER");
    exit;
    ?>
    Also, why is some of the PHP code displaying in the parsed HTML page when its in the Header section and enclosed in PHP tags?

    *GAH* I just checked IE, and everything just died.... Can anyone explain to me why IE decided to die out on me?

    Thanks everyone,
    Eric Huang
    Erichuangis: Erichuangis.com
    Small Blogs Network: Small Blogs Network

  2. #2
    hi galen's Avatar
    Join Date
    Jan 2006
    Location
    New Haven, CT
    Posts
    1,228
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    you might want to check your coding right before the \n

    also i don't understand why the tutorial says the switcher shoudl bring you do a different page. seems like a better way would be to link to ?style=whatever and have the styel changer in an include file. That way the current page reloads.

  3. #3
    SitePoint Addict
    Join Date
    Aug 2006
    Location
    Washington DC
    Posts
    215
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by galen View Post
    you might want to check your coding right before the \n

    also i don't understand why the tutorial says the switcher shoudl bring you do a different page. seems like a better way would be to link to ?style=whatever and have the styel changer in an include file. That way the current page reloads.
    I copied that code directly from the tutorial... not sure what I'm doing wrong?
    Erichuangis: Erichuangis.com
    Small Blogs Network: Small Blogs Network

  4. #4
    ♪♪ ♪ ♪ ♪ ♪♪ ♪ ♪♪ Markdidj's Avatar
    Join Date
    Sep 2002
    Location
    Bournemouth, South UK
    Posts
    1,551
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    I wouldn't advise using hyperlinks for style switchers. Personally I prefer to use image inputs, but there are various inputs and form elements to use instead which would do better than links for your search engine rankings.

    I also prefer to write my style sheets all from one PHP file. Actually I usually use 2 for each page, 1 for the common css and another for page-specific css if it needs it. I use php to output css depending on the cookie or querystring. When using cookies you have to be careful of the caching. For example, common-css.php will cache if the no-cache headers are missing. So when the cookie has changed and common-css.php is called again, it will be imported from the cached page without changing. The better alternative is to use common-css.php?color=red&amp;size=14 which will cache, but if you change either the color or the size a new css sheet will be delivered from the server. Each variation will then be cached seperately.

    If you don't need it to cache, or only have one sheet for your whole site, then you can use common.css in the <link>, but do a modrewrite in the .htaccess to the common-css.php file. If the no-cache headers are set, then cookies can be used within the php file outputting the css.

    Once you have your various outputs you can change the styling quite simply with a javascript style switcher which will do it without changing the page.

    Hope it helps
    Last edited by Markdidj; Oct 27, 2007 at 20:20.
    LiveScript: Putting the "Live" Back into JavaScript
    if live output_as_javascript else output_as_html end if

  5. #5
    ♪♪ ♪ ♪ ♪ ♪♪ ♪ ♪♪ Markdidj's Avatar
    Join Date
    Sep 2002
    Location
    Bournemouth, South UK
    Posts
    1,551
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    OP, Are you sure your server is linux based? It looks like it may not be, which is why it is outputting the coding. I did this on my site. Set up apache on my computer, learnt how to program abit of server stuff, uploaded it after creating the whole site then realising I was on a windows server!! Still, learnt both now

    Looked again, your file has a html extension. Are you trying to use php in html files or have you done a mod rewrite? For php scripts to run they have to have the correct suffix, usually .php, unless your using mod-rewrite to redirect a html file to a php one.......

    ??????

    Well, checked again, index.php doesn't exist but the page did tell me your on an apache server. So your using the right language
    LiveScript: Putting the "Live" Back into JavaScript
    if live output_as_javascript else output_as_html end if

  6. #6
    SitePoint Addict
    Join Date
    Aug 2006
    Location
    Washington DC
    Posts
    215
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Markdidj View Post
    OP, Are you sure your server is linux based? It looks like it may not be, which is why it is outputting the coding. I did this on my site. Set up apache on my computer, learnt how to program abit of server stuff, uploaded it after creating the whole site then realising I was on a windows server!! Still, learnt both now

    Looked again, your file has a html extension. Are you trying to use php in html files or have you done a mod rewrite? For php scripts to run they have to have the correct suffix, usually .php, unless your using mod-rewrite to redirect a html file to a php one.......

    ??????

    Well, checked again, index.php doesn't exist but the page did tell me your on an apache server. So your using the right language
    I just realized that I forget to name the file .php... haha... Silly me. Let me try that.

    I also first tried with a JS version, but the tutorials for that was from 2001... haha, so I was like huh

    *EDIT* even after rename, it still doesn't work.. Also, if you look at it in IE 7, its all messed up? Wtf?
    Erichuangis: Erichuangis.com
    Small Blogs Network: Small Blogs Network

  7. #7
    ♪♪ ♪ ♪ ♪ ♪♪ ♪ ♪♪ Markdidj's Avatar
    Join Date
    Sep 2002
    Location
    Bournemouth, South UK
    Posts
    1,551
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    try this
    Code:
    if $_COOKIES["cookiename"]!="" $mycookie=$_COOKIES["cookiename"]; else $mycookie="default";
    echo "<link rel=\"stylesheet\" type=\"text/css\" href=\"styles/".$mycookie.".css\">";
    LiveScript: Putting the "Live" Back into JavaScript
    if live output_as_javascript else output_as_html end if

  8. #8
    SitePoint Addict
    Join Date
    Aug 2006
    Location
    Washington DC
    Posts
    215
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Markdidj View Post
    try this
    Code:
    if $_COOKIES["cookiename"]!="" $mycookie=$_COOKIES["cookiename"]; else $mycookie="default";
    echo "<link rel=\"stylesheet\" type=\"text/css\" href=\"styles/".$mycookie.".css\">";
    that just gives me this:
    Parse error: syntax error, unexpected T_VARIABLE, expecting '(' in /home/2069/domains/thinkpoint.org/html/thinkpoint/index.php on

    I think I'm going to give a JS version a try because this is just not working...
    Erichuangis: Erichuangis.com
    Small Blogs Network: Small Blogs Network

  9. #9
    Theoretical Physics Student bronze trophy Jake Arkinstall's Avatar
    Join Date
    May 2006
    Location
    Lancaster University, UK
    Posts
    7,062
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    Change it to this:
    PHP Code:
    if($_COOKIES["cookiename"]!=""){
    $mycookie=$_COOKIES["cookiename"];
    }else{
    $mycookie="default";
    }
    echo 
    "<link rel=\"stylesheet\" type=\"text/css\" href=\"styles/".$mycookie.".css\">"
    Jake Arkinstall
    "Sometimes you don't need to reinvent the wheel;
    Sometimes its enough to make that wheel more rounded"-Molona

  10. #10
    SitePoint Addict
    Join Date
    Aug 2006
    Location
    Washington DC
    Posts
    215
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by arkinstall View Post
    Change it to this:
    PHP Code:
    if($_COOKIES["cookiename"]!=""){
    $mycookie=$_COOKIES["cookiename"];
    }else{
    $mycookie="default";
    }
    echo 
    "<link rel=\"stylesheet\" type=\"text/css\" href=\"styles/".$mycookie.".css\">"
    Nevermind, I solved it... It turns out 2 lines of JS can solve that entire issue.. :P I guess PHP wasn't needed. This http://www.centerkey.com/style/switcher/ was extremely helpful.
    Erichuangis: Erichuangis.com
    Small Blogs Network: Small Blogs Network


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •