Flukey Posted November 21, 2006 Share Posted November 21, 2006 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 Quote Link to comment Share on other sites More sharing options...
hitman6003 Posted November 22, 2006 Share Posted November 22, 2006 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] Quote Link to comment Share on other sites More sharing options...
Flukey Posted November 22, 2006 Author Share Posted November 22, 2006 Nice Code. Thanks Very Much. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.