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 5 of 5
  1. #1
    Regular Coder
    Join Date
    Aug 2005
    Posts
    252
    Thanks
    23
    Thanked 0 Times in 0 Posts

    Question regarding font switcher

    This script works fine but it would be ideal if there was simply one link (that toggled between the two fonts) instead of echoing the two links and separating them with a /.

    Please help!


    ffSwitcher.php

    PHP Code:
    <?php

    // SETTINGS

    $sett_influenced_elements 'body';

    $sett_fonts '
      Arial;helvetica
      Times;serif
    '
    ;

    $sett_address $_SERVER['SERVER_NAME'];

    // CLASS

    class FontSwitcher {

      var 
    $fonts;
      var 
    $active_font;
      var 
    $default_font;
      var 
    $unique_string;
      var 
    $influenced_elements;


      
    // Parse settings // Create unique string // Find active font // Check switching
      
    function FontSwitcher () {
        
    session_start();

        
    // Parse fonts from settings
        
    global $sett_fonts;
        
    $sett_fonts explode ("\n"trim ($sett_fonts));
        
    $row_count count($sett_fonts);

        for (
    $i 0$i $row_count$i++) {
          
    $row $sett_fonts[$i];
          
    $row explode (';'$row);

          
    $fonts[trim ($row[0])] = trim ($row[1]);

          if (
    $i == 0) {
            
    $this -> default_font = array('name' => $row[0], 'family' => $row[1]);
          }
        }

        
    $this -> fonts $fonts;

        
    // Create unique string and so
        
        
    global $sett_address;
        
    $this -> unique_string 'font_switcher_' substrmd5($sett_address), 0);

        global 
    $sett_influenced_elements;
        
    $this -> influenced_elements $sett_influenced_elements;

        
    // Find active font
        
        
    $font_session = @$_SESSION [$this -> unique_string];
        
    $font_cookie = @$_COOKIE [$this -> unique_string];

        if (isset (
    $font_sess) AND array_key_exists($font_sess$fonts)) {
          
    $active_font $font_sess;

        } elseif (isset (
    $font_cookie) AND array_key_exists($font_cookie$fonts)) {
          
    $active_font $font_cookie;

        } else {
          
    $active_font $this -> default_font['name'];
        }

        
    $this -> active_font $active_font;

        
    // Check switching
        
    $this -> SwitchFont();
      }

      
      
    // Switch it easy
      
    function SwitchFont () {
        if (isset (
    $_GET['font_switcher'])) {
          
    $new_font $_GET['font_switcher'];

          if (
    $new_font) {
            
    setcookie($this -> unique_string$new_fonttime()+30*24*3600);
            
    $_SESSION[$this -> unique_string] = $new_font;
            
    Header ('Location: '.$_SERVER['HTTP_REFERER']);
          }
        }
      }


      
    // Echoes <style> element with right css definition
      
    function EchoCss () {
        
    $active_font $this -> active_font;
        
    $fonts $this -> fonts;
        if (
    array_key_exists ($active_font$fonts)) {
          
    $family $fonts [$active_font];
          
    $message '';
        } else {
          
    $family $this->default_font['family'];
          
    $message "/* '$active_font' family definition not found. Default font is: '"$this->default_font['name'] ."'  */ \n";
        }

        echo 
    "\n" '<style type="text/css">' "\n" $message $this -> influenced_elements " {font-family: $family } \n</style>\n";

      }

      
    // Links that cause switching :)
      
    function EchoLinks ($separator ' ') {
        
    $fonts $this -> fonts;
        
    $active_font $this -> active_font;
        
    $fonts_count count($fonts);

        
    $active_address 'http://' $_SERVER['SERVER_NAME'] . $_SERVER['REQUEST_URI'];
        
    $active_address_any_par strpos($active_address'?');
        
    $active_address .= ($active_address_any_par) ? '&' '?';

        
    $link_list = array();

        foreach (
    $fonts as $font => $family) {
          if (
    $active_font != $font) {
            
    $link_list[] = '<a href="'$active_address 'font_switcher=' $font .'" rel="nofollow">' $font '</a>';
          } else {
            
    $link_list[] = $font;
          }

        }

        echo 
    implode ($separator$link_list);
      }
      
    // This is the end...

    ?>
    PHP Code:
    <?PHP @include_once("ffSwitcher.php");?>
    <?php $fs 
    = new FontSwitcher(); ?>
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">

    <html>
    <head>
        <title>test</title>

        <?php $fs -> EchoCss (); ?>

    </head>

    <body>

    <p><?php $fs -> EchoLinks (' / '); ?></p>

    <p>Hong Kong Phooey, number one super guy. Hong Kong Phooey, quicker than the human eye. He's got style, a groovy style, and a car that just won't stop. When the going gets tough, he's really rough, with a Hong Kong Phooey chop (Hi-Ya!). Hong Kong Phooey, number one super guy. Hong Kong Phooey, quicker than the human eye. Hong Kong Phooey, he's fan-riffic!</p>

    </body>
    </html>

  • #2
    New Coder
    Join Date
    May 2006
    Location
    California
    Posts
    47
    Thanks
    0
    Thanked 0 Times in 0 Posts
    do you mean a roll over? I don't quite understand what your asking

  • #3
    Regular Coder
    Join Date
    Aug 2005
    Posts
    252
    Thanks
    23
    Thanked 0 Times in 0 Posts
    The current code generates two links like so:

    Arial / Times

    I'd like it so that only one link is generated and that one link toggles between Arial and Times.

  • #4
    New Coder
    Join Date
    May 2006
    Location
    California
    Posts
    47
    Thanks
    0
    Thanked 0 Times in 0 Posts
    I think this is where your problem lies,

    $link_list[] = '<a href="'. $active_address . 'font_switcher=' . $font .'" rel="nofollow">' . $font . '</a>';

    you have a for loop, and each time each font gets its own link. Take out the a href and add it before you display it with the alternates, and what i would do is use javascript to check the current font and if its clicked to change it.

    so have

    $link_list[] = $font;

    and where the implode is make the link
    Last edited by Forsaken; 03-18-2007 at 08:17 PM.

  • #5
    Regular Coder dniwebdesign's Avatar
    Join Date
    Dec 2003
    Location
    Carrot River, Saskatchewan
    Posts
    845
    Thanks
    15
    Thanked 10 Times in 10 Posts
    In order to do the dynamic switching after you click on the text you would need to use JavaScript. Once a user click the text would change to the other choice. Checkout the JavaScript forum or search google.
    Dawson Irvine
    CEO - DNI Web Design
    http://www.dniwebdesign.com


  •  

    Posting Permissions

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