TalkPHP

TalkPHP (http://www.talkphp.com/forums.php)
-   Advanced PHP Programming (http://www.talkphp.com/advanced-php-programming/)
-   -   dynamic accesskeys (http://www.talkphp.com/advanced-php-programming/2198-dynamic-accesskeys.html)

Gibou 02-06-2008 03:03 PM

dynamic accesskeys
 
Hi everybody !

This topic, just for sharing a practical use of php5 to make dynamic accesskeys.

For those who don't know what are accesskeys:
In order to validate the accessibility of your website, it could be useful to give access to your menu links without needing the mouse (i.e. only keyboard). It's very simple in xhtml. For instance, your have a link named "my super link of death" and it's the first one in your menu. The code can be:

HTML Code:

<a href="target page" accesskey="a">my super link of death</a>
Thanks to this code, on Firefox, pressing Alt+Shift+a will equival to clic on the link. The accesskeys are generaly letters so menu mustn't have more than 26 links :p
The first link has the accesskey 'a', the second, 'b' and so on.
Ex: Wedus.org :: Système d'information d'aide à l'éducation .::. Politique d'accessibilité


The aim of the php5 class:
The problem is when you insert a link between two of them in the menu, you have to change the others accesskeys to keep the order. And you have to modify the page containing the list of accesskeys with their description and the page where the link redirects.
The class manages that.

PHP Code:

<?php

class Accessibility
{
    private static 
$key 'a';
    private static 
$tableKeys = Array();

    public function 
makeAccesskey($linkName null$desc null)
    {
        
$tmp = Array(
        
"key" => self::$key,
        
"linkName" => $linkName,
        
"desc" => $desc
        
);
        
self::$tableKeys[] = $tmp;
        unset(
$tmp);
        return(
'accesskey="'.self::$key++.'"');
    }
    
    public function 
getTableKeys()
    {
        return(
self::$tableKeys);
    }
}

?>

In the menu, you just have to insert your links like that:

PHP Code:

<a href="index.php" <?php echo Accessibility::makeAccesskey("Home","Go back to home page");?>>Home</a>

It will insert the good accesskey and save a list with the association of letters / links names / description of the page targeted.
To create the page listing the accesskeys, you just have to create a short script parsing the hashtable $tableKeys.
For instance:

PHP Code:

<?php
$tableKeys 
Accessibility::getTableKeys();
foreach(
$tableKeys as $row)
{
    echo
'
    <tr>
        <td>Alt + Shift + '
.$row["key"].'</td>
        <td>'
.$row["page"].'</td>
        <td>'
.$row["desc"].'</td>
    </tr>'
;
}
?>

Now, if you want to insert a link, just insert the line in the menu and the rest will be automatic ^^

Hoping it can be useful :)

ReSpawN 02-06-2008 06:55 PM

Awesome mate, was wondering how OGame did it. I'll try it out a.s.a.p.

Tanax 02-06-2008 09:36 PM

Very good article!

Althought.. what is this good for?
I mean.. what will it create, that is better than just having a regular link like
HTML Code:

<a href="index.php">Home</a>
??

Sorry if it's a newbie question..

Gibou 02-06-2008 09:53 PM

Yes,

Do not forget that some peoples in front of the screen don't have your mobility.
Accesskeys and tabindexes have been created for them because, sometimes, they can not use both the keyboard and the mouse in the same time.
Shortcuts for links have so been invented.

In fact, mani criticizes are made to accesskeys because:
- all browsers don't manage them by the same way (on IE, you have to press Alt+Shift+letter AND press enter).
- the combination is awful and with one arm, try to press Alt+Shift+M on an azerty keyboard ^^

But it's a good way to don't exclude some peoples :)


All times are GMT. The time now is 07:49 AM.

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0