In this post we are going to discuses about alphabet navigation,before that do you know How to generate alphabets in a loop with php built in functions,take a look at the below examples,
for ($i=65; $i<=90; $i++) { echo chr($i); // chr returns specific character //out put :ABCDEFGHIJKLMNOPQRSTUVWXYZ }
In the above example chr() function returns specific character & chr() also accepts negative numbers as an ascii code[char(-100)],although you can generate the above output with php built-in function called range(), see the below example once
foreach(range('A','Z') as $i) { echo $i; } //output:ABCDEFGHIJKLMNOPQRSTUVWXYZ
Now i can assume that, you got an idea about the alphabet navigation menu from the above examples,we can do this with in two steps ,very first what we will do is , will generate alphabets array with any of the above methods then store that results in a variable ,in my example $letterList, then loop over all elements of an array, see the complete examples bellow , you can use any of these methods
$LetterList = range('A','Z'); foreach ($LetterList as $litter) { echo '<a href="http://phpis.com/letter.php?letter='.$litter.'">'.$litter.'</a>'; }
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
Another Method
$LetterList = array_merge(array('0-9'),range('A','Z')); foreach ($LetterList as $value) { echo '<a href="http://phpis.com/letter.php?letter='.$value .'">'.$value .'</a>'; }
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
Another Method
for($i=ord('A');$i<=ord('Z');$i++) { echo '<a href="http://phpis.com/letter.php?letter='.strtolower(chr($i)).'">'.chr($i).'</a>'; }
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
