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
3 for ($i=65; $i<=90; $i++) {
4 echo 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) {
2 echo $i; }
3 ?>

output:ABCDEFGHIJKLMNOPQRSTUVWXYZ

now you can easily understand below alphabet navigation menu

1 <?php
2 $LetterList = range('A','Z');
3 foreach ($LetterList as $litter) {
4 echo '<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'));
3 foreach ($LetterList as $value) {
4 echo '<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

Advertisements

2 thoughts on “how to make Alphabet Navigation Menu with PHP?

  1. Pingback: PHP « amithmehta
  2. Hi,

    I would like to add pagination for an Alphabet Index, could you assist me in doing so? I have some pagination code to show, but I cannot get it working the way I am after. I appreciate you time in advance!

    Thank you
    Brian

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s