demechanico
demechanico

Reputation: 361

A large array to be handled in PHP or JavaScript

I have a large array at hand (2300 elements, 280 KB in total, 180 KB in shortest form). I need to handle it either in PHP or JavaScript without loading the page or server too much. There is an autocomplete field in the page and I don't want it to send request to a 180 KB page every time a user types in a letter (as it seems to be highly inefficient to me). I don't want to store all that in the original page 'coz it would slow the page down (wouldn't it?).

I tried to push it into the Cookies, but it seems that there is no room for such huge array in the Cookies. Any suggestions? Perhaps searching within a database is better solution and I should store them in the database instead?

Upvotes: 0

Views: 406

Answers (3)

Denys Séguret
Denys Séguret

Reputation: 382264

Using a database for autocomplete fields is a classical solution indeed. I think that you wouldn't have difficulty finding a PHP/mysql/jquery autocomplete tutorial.

It works very well.

Never send to the client the complete list, it's useless and this solution wouldn't scale when your number of values grows.

Upvotes: 0

Omran Jamal
Omran Jamal

Reputation: 409

I Believe it is Actually Best to Send the input to the Server Through Ajax & wait for the Server-Side Reply if you are concerned about Page Loading times + If your Array is Changing from time to time...

On the Other hand you could store the array in JSON format in a fie on the server & issue it inside JavaScript Through Ajax, That way your user will have to download the array once & later you can rely of the browser cache to speed it up.

eg.

$.get("array.txt",function(array){

   var myArray = eval(array);
   //Now DO WHat YOu WAnt with the Array now Housed in the variable 'myArray'

});

THis is Just an Example, it is not nessecery for you to use jQuery.

Note:-

If you use the second method...Its Best to add some sort of buffer mechanism so that your users wait for it to load the first time a user visits the site in their current session. This is just so that users get what they expect on first try or else they might not see any suggestions or whatever you want to do with the array then they'll get irritated & eventually leave.

You Need to Set the correct Cache Header if using PHP. Normally a txt file that you upload once & never change will automatically be sent to Cache in most Modern Browsers.

Upvotes: 1

Ugo Méda
Ugo Méda

Reputation: 1215

You want to autocomplete with a list that you load only one ? Store it in a JavaScript file and set the proper cache headers so clients only load the list once.

Do NOT set it as a cookie, that's just nonsense.

Upvotes: 1

Related Questions