Friday, April 26, 2013

jquery autocomplete source function

For e.g., if the user types john smith in the inputBox, and you want to return all the elements which contain either john or smith or both, then use the following code : 
 
$( "#inputBox" ).autocomplete({
source: function (request,response) {
var terms = request.term.trim().replace(/\s+/g,' ').split(" "); //Now terms is an array ['john','smith']
var respData = {}; 
terms.forEach( function(term) { 
lookUpArray.forEach(  function (lookUpElem) {
var regexp = new RegExp(term, "gi");
if(lookUpElem.match(regexp)) {
respData[lookUpElem] = 1;

});
});
var results = [];
for(var k in respData) results.push(k);
response(results);
}
});

lookUpArray is a global variable here - if it contains ['john dao' ,'smith dao', 'john smith', 'singh bone'] - then the first 3 elements will be returned for the query john smith.

Friday, April 19, 2013

How can you not love PHP?

1. Once I wanted to set up something like Wikipedia on my own. I got hold of MediaWiki - written in PHP/MySql. Dumped it on my shared host. In less than an hour I was up and running.

2. Once I wanted to run an e commerce site. I sifted through half a dozen e commerce engines - all written in PHP/MySql. Dumped in on my shared host. In a couple of days I was up and running.

3. Once the mighty Google Reader closed down - for once and for all. I dumped Tiny Tiny RSS Reader on my shared host. In a couple of hours I was up and running.

How can I not love PHP  after all this and more?

Tiny Tiny RSS Reader : This XML Document is Invalid

I just came across this problem for this feed : http://techcircle.vccircle.com/feed/

The problem being the whitespaces at the beginning of the xml.
So we have to trim the response.

Here is the solution : 

1. Open the file lib/simplepie/simplepie.inc
2. Somewhere around the line #1342 - You will find : 

// Loop through each possible encoding, till we return something, or run out of possibilities
foreach ($encodings as $encoding)
{
// Change the encoding to UTF-8 (as we always use UTF-8 internally)
if ($utf8_data = $this->registry->call('Misc', 'change_encoding', array($this->raw_data, $encoding, 'UTF-8')))


Add the following line as the first statement in the foreach loop : 
$this->raw_data = trim($this->raw_data);

Now the code should look like : 

// Loop through each possible encoding, till we return something, or run out of possibilities
foreach ($encodings as $encoding)
{
$this->raw_data = trim($this->raw_data);
// Change the encoding to UTF-8 (as we always use UTF-8 internally)
if ($utf8_data = $this->registry->call('Misc', 'change_encoding', array($this->raw_data, $encoding, 'UTF-8')))

Blog Archive