Friday 4 March 2011

how to make Alphabet Navigation Menu with PHP?

In this post we are going to discuses about alphabet navigation.before that do you know generating alphabets in a loop.if don’t know look at below example once










1<?php










2










3for ($i=65; $i<=90; $i++) {










4echo chr($i);// chr returns specific character










5//out put :ABCDEFGHIJKLMNOPQRSTUVWXYZ










6}










7?>




In the above example chr() function returns specific character & chr() also accepts negative numbers as an ascii code[char(-100)]

You can make above output with range() function,look the example :














1<?php foreach(range('A','Z') as $i) {










2echo $i; }










3?>




output:ABCDEFGHIJKLMNOPQRSTUVWXYZ

now you can easily understand below alphabet navigation menu














1<?php










2$LetterList = range('A','Z');










3foreach ($LetterList as $litter) {










4echo '<a href="http://example.com/letter.php?letter=' . $litter. '">' .$litter. '</a>'.' ';










5}










6?>




Outpup : A B C D E F G H I J K L M N O P Q R S T U V W X Y Z

you can make above output in another way














1<?php










2$LetterList = array_merge(array('0-9'),range('A','Z'));










3foreach ($LetterList as $value) {










4echo '<a href="http://example.com/letter.php?letter=' . $value . '">' .$value . '</a>'.' ';










5}










6?>




out put:A B C D E F G H I J K L M N O P Q R S T U V W X Y Z

The description of the functions used in this tutorial are as follows

chr() this function return a spesific character

range() this function crate an array containing range of elements

foreach()foreach is used to loop over all elements of an array

array_merge() this function merges one or more arrays

No comments:

Post a Comment