Jump to content

AccessKeys PHP Task


Flukey

Recommended Posts

I've been set the following task for a company to do.


So, i've done this function so there are no duplicate access keys, e.g. if i didn't check for duplicates, the accesskey for prices and products would both be 'p'.
If i find a duplicate i just use the first and second letter of the word. e.g. products access key would be 'pr'. The only problem is, this isn't exactly the most reliable way of doing it.

How would you do it?

Heres my code:

[code=php:0]

function add_accesskeys($input) {
      // Generate Access Keys
      $input_count = count($input);
      $access_key = array();
      for($i=0; $i<=$input_count; $i++)
      {
        // Get Access Keys
        $access_key[$i]= substr($input[$i]['title'], 0,1);
      }
     
      // Check for duplicates. If duplicate is found, use first and second letter for key
      for($i=0;$i<=$input_count;$i++){
          $found = 0;
          for($x=0; $x<=$input_count; $x++){
              if($access_key[$i] == $access_key[$x]){
                $found = $found + 1;
              }
            // Duplication Found
              if($found == 2){
                // Replace Key With New One
                $access_key[$i] = substr($input[$i]['title'], 0, 2);
              }
          }
      }
      for($i=0; $i<$input_count; $i++)
            {
            $first_letter = substr($input[$i]['title'], 0,1);
            $filename = $input[$i]['filename'];
            $title_remain = substr($input[$i]['title'],1) ;
            $links = $links . "<li><a href=\"$filename\" accesskey=\"$access_key[$i]\"><em>$first_letter</em>$title_remain</a></li>";
            }
            $output = "<ul>". $links . "</ul>";
            return $output;
  }


[/code]

Regards,

Jamie
Link to comment
https://forums.phpfreaks.com/topic/28011-accesskeys-php-task/
Share on other sites

A while loop works well here.

[code]<?php

$input = array(
array( 'filename' => 'home.html',
'title' => 'home page'
),
array( 'filename' => 'products.html',
'title' => 'products'
),
array( 'filename' => 'promotions.html',
'title' => 'promotions'
),
array( 'filename' => 'private.html',
'title' => 'private'
),
array( 'filename' => 'contact.html',
'title' => 'contact'
),
array( 'filename' => 'prices.html',
            'title' => 'prices'
)
);

function go($input) {
$data = array();

foreach ($input as $item) {
//determine the highlighted portion
if (!array_key_exists(substr($item['title'], 0, 1), $data)) {
$x = substr($item['title'], 0, 1);
$data[$x] = '
<li><a href="' . $item['filename'] . '" accesskey="' . $x . '"><em>' . $x . '</em>' . substr($item['title'], 1, strlen($item['title'])) . '</a></li>';
} else {
$x = substr($item['title'], 0, 1);
$y = 1;

while (array_key_exists($x, $data)) {
$y++;
$x = substr($item['title'], 0, $y);
}

$data[$x] = '
<li><a href="' . $item['filename'] . '" accesskey="' . $x . '"><em>' . $x . '</em>' . substr($item['title'], $y, strlen($item['title'])) . '</a></li>';

}
}

return '<ul>' . implode("", $data) . '</ul>';
}

echo '<style>em { font-weight: bold; }</style>';
echo go($input);
?>[/code]
Link to comment
https://forums.phpfreaks.com/topic/28011-accesskeys-php-task/#findComment-128255
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.