Post your trick

jQuery: selecting elements with multiple classes

Here is a way to select element with multiple classes in jquery.

$(’table.cl1.cl2′).addClass(’abc’);

And to select the element which has one class
but not the other. This, does that:

$(’table.cl1:not(table.cl2)’).addClass(’xyz’);

Second one is pretty interesting which works like if clause.


how to detect all ajax requests using the zend framework
Zend_Controller_Request_Http has a rudimentary method
for detecting AJAX requests: isXmlHttpRequest().
This method looks for an HTTP request
header X-Requested-With with the value 'XMLHttpRequest';
if found, it returns TRUE.

$this->getRequest()->isXmlHttpRequest()

it is important to check that your ajax requests
 contain the request header - X-Requested-With

php array to http url

hi guys,

I came across a beautiful function of php which will convert any php array to the GET parameter for url.

E.g.

$data = array( ‘foo’ => ‘bar’, ‘jimy’ => ‘modi’ );

$url = http_build_query($data);

this will give you

foo=bar&jimy=modi

More info


Convert ASCII code to text in javascript

If a javascript string contains ASCII codes as a result of escaping HTML characters, to convert them into text, use the String.fromCharCode(); and replace functions as follows

str.replace(/&#(\d+);/g, function (m, n) { return String.fromCharCode(n); });

String.fromCharCode() is a static function and will be called by passing the string variable and using ‘String’ only. ie not like other string functions eg str.replace()


How to get Latitude and Longitude from IP address

After searching for more than four to five hours i got the link which give latitude, longitude and other information such as address including city and country. So here is a small function which will give you latitude and longitude of the given ip address.

function getLatLng($ip){

//get the contents from the site by file_get_contents.
$sXML = @file_get_contents(’http://ipinfodb.com/ip_query.php?ip=’.$ip.’&timezone=false’);

//make the response in a xml object so we can parse it.
$oXML = @new SimpleXMLElement($sXML);

$array = array(
‘Latitude’ => (string) $oXML->Latitude[0],
‘Longitude’ => (string) $oXML->Longitude[0]
);
return $array;
}


MySQL FullText Searching

Suppose you want to search a keyword in 5 different columns or almost in all the fields of the table, then MySQL
provides us with fulltext indexing and searching method.

Before using this method , you need to add FullText to the columns in which you want to search.

ALTER TABLE member ADD FULLTEXT(firstname, lastname,address);

Then comes your main query :

SELECT firstname, lastname,address FROM member
WHERE MATCH (firstname,lastname,address) AGAINST (’test’);

The Match will look for “exact match” in the above 3 columns and on successfull match it will return that row.

Suppose in member table there is a an entry with firstname = test then the output will be :

firstname lastname address

test abc xyz

Suppose you want to use wildcards then modify your query :

SELECT firstname, lastname,address FROM member
WHERE MATCH (firstname,lastname,address) AGAINST (’*test*’);


Custom Events in jquery

we can fire custom events in jquery and set an event handler for each event .

This enables loose coupling and code reuse . This can be used to achieve the observer design pattern .

$(’#txtAddress’).keyup(function() {

$(document).trigger(’ADDRESS_CHANGE’);

});

$(’#txtCity’).keyup(function() {

$(document).trigger(’CITY_CHANGE’);

});

to now bind the event :

$(document).bind(’ADDRESS_CHANGE’, function(e) {
UpdateName();
UpdateOtherText(e);
});

Hack Away ;)


How to use MySQL FOUND_ROWS()?

MySQL has  a function called FOUND_ROWS which is faster than any other function for counting rows which is returned by the latest query, it is used same as MySQL “LASTINSERT_ID()”.

For example: if we are developing a pagination script  and we want the total number of results returned by the Query, at the same case in pagination we use LIMIT for paging the record using the above function it will not return the limit results it will return the total available results.

How to use? suppose we have 20 records in our table and we are fetching only 10 records in a page, then the $total will return total 20 not 10.

$sql = “SELECT SQL_CALC_FOUND_ROWS * FROM tablename LIMIT 1, 10 “;

$results = mysql_query($sql);

$total = mysql_query(’SELECT FOUND_ROWS() as total’);


Css Hack

I spent about 35 minutes on this one and still havent found a logic to it .

But this hack should be remembered .

consider the markup : <div id = a ><input/><div id = “a_child” ></div>    </div>

if you need to give a bottom border to the div with id = a and expect it to come below the div with id = “a_child”  , then the following css needs to be in place :

#a{  height:auto;overflow:hidden;}

Thanks to vineet and jimit  to get out of this one ..


word wrap for an editable div

To make a div or a paragraph editable set the attribute -  contenteditable = true

give a fixed width to the div : -   width:100px ;

after 100px the word should wrap down , for this add the css property :

word-wrap:break-word;

Happy Hacking :)


Post A Trick !

How does "Kodetricks" work?

We at kodeplay like to share knowledge. With Kodetricks, even you can join us. All you need to do is post a programming related trick if you have one or rate a trick if you like someone else's.



Find us on

Technology

Contact Us