Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I have an array containing friends list of a user on Facebook.

I have to display that list and provide a text field on the top of the list to enter first letter of any of his/her friends name. When the user enters a letter in the text field, the program should display only the names that start with the entered letter, all other names should vanish.

In addition the names should be sorted alphabetically.

How do I perform this task?

share|improve this question
    
there are many ways, you can do this all via javascript, or you can do it using php... or both.. javascript + php (+ ajax). tell us more, what did you try? –  MireSVK May 3 '12 at 18:24
    
you mean the autocomplete? do you wish to perform this using ajax? or purely php? –  sree May 3 '12 at 18:24
    
This is a job for REGEX no matter what language you use! –  RiverC May 3 '12 at 18:26
1  
@MireSVK I uesd PHP to display friends list. Friends list is stored in a PHP aray and I am displaying it using HTML form. Now, what I need is, when the user enters a letter in the text field the program should work as I mentioned in question above. Please tell me, what event should I add with the text field to get the entered letter and immediately display the names without pressing any button etc. Each name in the friend list follows a checkBox, user can select multiple names. –  qurban May 8 '12 at 20:32
1  
@sree In autocompletor, if it is possible to select multiple names at a time, then you are right. I need the same thing. –  qurban May 8 '12 at 20:37

2 Answers 2

up vote 1 down vote accepted

First I have to ask are you using javascript or anything to filter or is it just server side code using php?

Aside from that here's the steps to how I would do it just on the php side.

1) Sort the entire list of all the user's friends so the full array is in order. Take a look at this http://www.php.net/manual/en/function.sort.php.

2) When the user inputs their single letter, you simply just have to:

  • Create a new array to store the results of the user's search
  • Loop through your large array of all the friends and insert the matches into the smaller array
  • Display the small array as it will already be sorted from having the master list already sorted.

    for ($i = 1; $i <= $masterFriendsList.length; $i++) {
        if (substr($masterFriendsList[i],0,1)==$yourMatchValue){
            $filteredArray[]=$masterFriendsList[i];
        }
    }
    
share|improve this answer
1  
I tried this. But how to extract data from that text field without submitting it as a form. At the time, when the user enters a letter into text field the program should immediately display those names, without pressing any button etc. I think you can understand my problem. if not, please read my question. Thanks :) –  qurban May 8 '12 at 20:41
1  
You can just send it through javascript to the server side using ajax with an onchange event. When the user presses a key, the onchange event should fire and you can send the updated text field data to your php code. –  jeschafe May 15 '12 at 17:09

However you do this, you are probably going to need some ajax. Unless you just sort and filter the list on the client side. There is already a library for tabular data called Datatables will do this for you.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.