Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 8 of 8
  1. #1
    New to the CF scene
    Join Date
    Apr 2004
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    StyleSheet Selector

    Hi, I was wondering if anyone knew of a script that would go in a dropdown list and choose a stylesheet. Basically, I want to give members the option to choose what stylesheet they use to browse the site, in a dropdown list, that will then stay that way until they change it or close the browser.

    Does anyone know of one/can make one?

    Thanks

  • #2
    raf
    raf is offline
    Master Coder
    Join Date
    Jul 2002
    Posts
    6,589
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Welcome here !

    Very easy. You create a dropdown (lets call it stylesheetdropdown) with the stylesheetname as vaues. You post the selected value from the dropdown to a PHP page, where you then store it inside a sessionvariable. Like
    PHP Code:
    session_start();
    $_SESSION['stylesheet'] = $_POST['stylesheetdropdown']; 
    then on top of each page, you build your html header (preferably using an include) like this (inside the header tags:
    <style type="text/css" media="all">@import "<?php echo $_SESSION['stylesheet']; ?>";</style>
    Posting guidelines I use to see if I will spend time to answer your question : http://www.catb.org/~esr/faqs/smart-questions.html

  • #3
    New to the CF scene
    Join Date
    Apr 2004
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts
    I'm like, a PHP newbie, so please bear with me.

    What else do I need to add to the code? Can you give me an example?

    As it stands, I get this:
    Code:
    Notice: Undefined index: stylesheetdropdown in c:\program files\easyphp1-7\www\test.php on line 9
    PHP Code:
    $_SESSION['stylesheet'] = $_POST['stylesheetdropdown']; 
    Thanks for your help

  • #4
    Senior Coder Nightfire's Avatar
    Join Date
    Jun 2002
    Posts
    4,265
    Thanks
    6
    Thanked 48 Times in 48 Posts
    PHP Code:
    <?php 
    session_start
    ();
    if(isset(
    $_POST['submit'])){
        
    $_SESSION['cssselector'] = $_POST['cssselector'];
        
        switch(
    $_SESSION['cssselector']){
            case 
    'blue':
                
    $css "blue.css";
                break;
            case 
    'red':
                
    $css "red.css";
                break;
        }
    }else{
        
    $css "default.css";
    }
    ?>
    <html>
    <head>
    <title>Style switcher</title>
    <link rel="stylesheet" href="styles/<?php echo $css?>">
    </head>
    <body>
    <form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post" >
        <select name="cssselector">
            <option value="blue">Blue</option>
            <option value="red">Red</option>
            etc
        </select>
        <input type="submit" name="submit" value="Change style">
    </form>
    </body>
    </html>

  • #5
    raf
    raf is offline
    Master Coder
    Join Date
    Jul 2002
    Posts
    6,589
    Thanks
    0
    Thanked 0 Times in 0 Posts
    You don't need to add any PHP code. Just these two lines.

    Is your dropdown with the stylesheet-options in called 'stylesheetdropdown' ? Did you post the form to this page ? Maybe post the relevant piece of the form here (the form-tag and the dropdowncode.
    Posting guidelines I use to see if I will spend time to answer your question : http://www.catb.org/~esr/faqs/smart-questions.html

  • #6
    New to the CF scene
    Join Date
    Apr 2004
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts
    NightFire, great code!

    I'm confused now, because I have it working exactly like that, with a blue.css and a red.css in the folder.

    I then put the code into the document I want it in, and there's a style.css (what I changed default.css to), red.css, and blue.css. And all the styling from style.css disappears, and the selector doesn't work.

    So I now have:
    index.php
    PHP Code:
    <?
    session_start
    (); 
    if(isset(
    $_POST['submit'])){ 
        
    $_SESSION['cssselector'] = $_POST['cssselector']; 
         
        switch(
    $_SESSION['cssselector']){ 
            case 
    'blue'
                
    $css "blue.css"
                break; 
            case 
    'red'
                
    $css "red.css"
                break; 
        } 
    }else{ 
        
    $css "style.css";

    ?>
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3 Transitional//EN">
    <html>
    <head>
    <!-- TemplateInfo codeOutsideHTMLIsLocked="true" -->
    <!-- TemplateBeginEditable name="doctitle" -->
    <title>Alex-Simpson.com :: </title>
    <!-- TemplateEndEditable --> 
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
    <!-- TemplateBeginEditable name="head" --><!-- TemplateEndEditable -->
    <script language="JavaScript" type="text/JavaScript">
    <!--


    function MM_jumpMenu(targ,selObj,restore){ //v3.0
      eval(targ+".location='"+selObj.options[selObj.selectedIndex].value+"'");
      if (restore) selObj.selectedIndex=0;
    }
    //-->
    </script>
    </head>

    <body>
    <!-- Loads of body content -->
              <td width="344">
    <form action="<? echo $_SERVER['PHP_SELF']; ?>" method="post" > 
        <select name="cssselector"> 
            <option value="blue">Blue</option> 
            <option value="red">Red</option> 
            etc 
        </select> 
        <input type="submit" name="submit" value="Change style"> 
    </form> 
    </td>
            </tr>
            <tr> 
              <td height="5"></td>
              <td></td>
            </tr>
          </table>

    </td>
    </tr>
    </table>
    </body>
    </html>
    A blue.css, red.css, and style.css in the folder. And it's just not working - see here.

    Thanks a lot for your help.

  • #7
    Senior Coder Nightfire's Avatar
    Join Date
    Jun 2002
    Posts
    4,265
    Thanks
    6
    Thanked 48 Times in 48 Posts
    You've not added the link to the css
    <link rel="stylesheet" type="text/css" href="stylesfolder/<?php echo $css;?>">

  • #8
    New to the CF scene
    Join Date
    Apr 2004
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Working like a dream.

    Thank you.


  •  

    Posting Permissions

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