5.13 Abbreviating Input


You have a bunch of commands, but you don't want to make the user type them in. Therefore, you need to find a way to let the user abbreviate his input as much as possible.

Technique

Create associative array mapping and then get the substring of the input, like so:

 <?php function do_function ($option) {     switch ($option) {           case "Send":                print "Sent";                break;           case "Delete":                print "Deleted";                break;           case "Open Mail":                print "Mail Opened";                break;           case "Read Message":                print "Message Read";                break;           case "Reply-to Message":                print "Reply-to Message";                break;       } } $mappings = array ("S" => "Send",                    "D" => "Delete",                    "O" => "Open Mail",                    "R" => "Read Message",                    "T" => "Reply-to Message"); do_function ($mappings[strtoupper (substr ($input, 0, 1))]); ?> 

Comments

Allowing for abbreviations makes your function more forgiving of user input, and saves your user the time of having to type in exactly what he wants. This function creates an associative array in which the first letter the user enters is really all that matters.



PHP Developer's Cookbook
PHP Developers Cookbook (2nd Edition)
ISBN: 0672323257
EAN: 2147483647
Year: 2000
Pages: 351

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net