...

View Full Version : Question regarding font switcher



Errica
03-18-2007, 05:52 PM
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

// 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_' . substr( md5($sett_address), 0, 8 );

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_font, time()+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 @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>

Forsaken
03-18-2007, 05:54 PM
do you mean a roll over? I don't quite understand what your asking

Errica
03-18-2007, 09:06 PM
The current code generates two links like so:

Arial (javascript:void(0)) / Times (javascript:void(0))

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

Forsaken
03-18-2007, 09:14 PM
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

dniwebdesign
03-19-2007, 05:07 AM
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.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum