Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to encode large HTML lists for jQuery?

I have a PHP script that fetches a relatively large amount of data, and formats it as HTML unordered lists for use in an Ajax application.

Considering the data is in the order of tens to possibly more than a hundred KB, and that I want to be able to differentiate between the different lists with Javascript, what would be the best way to go about doing this?

I thought about json_encode, but that results in [null] when more than a certain amount of rows are requested (maybe PHP memory limit?).

Thanks a lot, Fela

like image 919
Fela Maslen Avatar asked Oct 24 '22 21:10

Fela Maslen


2 Answers

Certain illegal characters in the string could be breaking the json_encode() function in PHP which you will need to sanitize before this will work correctly. You could do this using regular expressions if this becomes a problem.

However, if you are sending requests with that amount of data it may be unwise to send this using AJAX as your application will seem very unresponsive. It may be better to get this data directly from the database as this would be a far faster method although you will have to obviously compromise.

like image 154
Daniel West Avatar answered Oct 27 '22 11:10

Daniel West


I cannot click up, but I agree with Daniel West.

Ensure your strings are UTF-8 encoded or use mysql_set_charset('utf8') when you connect. The default charset for mysql is unfortunately Latin/Windows. Null is the result of a failed encoding because of this, if it was out of memory, the script itself would fail.

like image 36
Peter Postma Avatar answered Oct 27 '22 10:10

Peter Postma