Tooltip
If you are looking for nice tooltip libraries, I recommend trying qTip2. It’s a jquery plugin. And it’s got most of the pop-up tooltip features you can imagine.
I personally like this feature, that you can assign different behaviors (by different actions) to the same element. See below:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 | // Create our first tooltip
$('.selector').qtip({
content: 'Mouse entered',
show: {
event: 'mouseenter',
solo: true // Only show one tooltip at a time
}
})
// Remove the previous tooltips data
.removeData('qtip')
// Create our second tooltip
.qtip({
content: 'Click',
show: {
event: 'click',
solo: true // Only show one tooltip at a time
}
}); |
With the above code, you can have a specified mouseover preview tooltip, and a detailed view upon a mouse click. Very handy!
Check out their demo page for all the cool features!
rounded corners
An ordinary div
A div with rounded corners
The second div looks so much more fun and professional at the same time!
As it turns out, all you need is one or two extra lines in order to add these rounded corners. See code below:
1 2 3 | #my_div { border-radius: 15px; } |
If you want more fine-tuning, try the following. The first parameter is the horizontal radius of the rounded corner, the second the vertical.
1 2 3 | border-top-left-radius: 10px 5px; border-bottom-right-radius: 10% 5%; border-top-right-radius: 10px; |
Play around with it and see if you can make something like the following.
PHP array delete an item
Say, you have an array like the following, how to delete “three”?
1 | $arr = array("one", "two", "three", "four", "five"); |
PHP has this function called unset, with which you can do:
1 | unset($arr[2]); |
But… yea… there is no direct way to do something like array_delete_value(“three”)… I don’t understand why there is no easy way to do deletion by value…
You will need to get the array index first, whenever you want to delete by value. We will use array_search to get the index or key.
1 2 3 | if ( ($key = array_search("three", $arr)) !== false) {
unset($arr[$key]);
} |
The above is better than traversing the whole array linearly.
If you need to do a global delete, use a while loop.
1 2 3 | while ( ($key = array_search("three", $arr)) !== false) {
unset($arr[$key]);
} |
PHP encode JS decode
Very often I need to encode some string in PHP, then pass that to javascript. For example, say I am using PHP to generate some javascript functions. See below:
1 2 3 4 5 6 7 | $s = "some text from somewhere";
$js_code = "
function do_something(some_text){
alert(some_text);
}
do_something('$s');
"; |
The above javascript code would work fine until one day you have some symbols in $s, for example the single quote. (There may be more troublemakers, but at least this is a common one).
So now you start thinking about encoding and decoding. It turns out there are so many ways to do them… Just which one do you need?
Below is my solution, you are welcome and encouraged to find your own solution.
1 2 3 4 5 6 7 8 9 | $s = "some text !@#$%^&*()_+[]\{}|;':",./<>?"; // of couse this is not syntactical, in reality think of this string coming from a db
$s_encoded = rawurlencode($s);
$js_code = "
function do_something(some_text_encoded){
some_text = unescape(some_text_encoded);
alert(some_text);
}
do_something('$s_encoded');
"; |
Hope this can save your time.
jquery datepicker date format
If you are using the jquery datepicker and have been displaying the date in the usual mysql date format, you probably will find it annoying that the date picker gives you the slash date format back. Here is how to keep the mysql date dash format all throughout.
1 2 3 4 5 6 7 | var queryDate = '2012-01-10',
dateParts = queryDate.match(/(\d+)/g)
realDate = new Date(dateParts[0], dateParts[1] - 1, dateParts[2]);
// months are 0-based!
$('#datePicker').datepicker({ dateFormat: 'yy-mm-dd' }); // format to show
$('#datePicker').datepicker('setDate', realDate); |
The code above will translate the mysql date to do the initial setup, then use the dash format for the text field.
Ref: http://stackoverflow.com/questions/1953840/datepickersetdate-issues-in-jquery
newline character in title
To create a line break in the title attribute, use “
”.
1 | <a href=# title='line 1
line 2
line 3'>Hover over me!</a> |
PHP command line memory limit error
Very often, when data size gets bigger and bigger, some scripts will break due to default memory limit. I got the following error today.
1 2 | Fatal error: Allowed memory size of 134217728 bytes exhausted (tried to allocate 72 bytes) in /someDir/myScript.php on line 15 Segmentation fault: 11 |
I checked my php help. I am using version 5.3.6.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 | $ php -h
Usage: php [options] [-f] <file> [--] [args...]
php [options] -r <code> [--] [args...]
php [options] [-B <begin_code>] -R <code> [-E <end_code>] [--] [args...]
php [options] [-B <begin_code>] -F <file> [-E <end_code>] [--] [args...]
php [options] -- [args...]
php [options] -a
-a Run as interactive shell
-c <path>|<file> Look for php.ini file in this directory
-n No php.ini file will be used
-d foo[=bar] Define INI entry foo with value 'bar'
-e Generate extended information for debugger/profiler
-f <file> Parse and execute <file>.
-h This help
-i PHP information
-l Syntax check only (lint)
-m Show compiled in modules
-r <code> Run PHP <code> without using script tags <?..?>
-B <begin_code> Run PHP <begin_code> before processing input lines
-R <code> Run PHP <code> for every input line
-F <file> Parse and execute <file> for every input line
-E <end_code> Run PHP <end_code> after processing all input lines
-H Hide any passed arguments from external tools.
-s Output HTML syntax highlighted source.
-v Version number
-w Output source with stripped comments and whitespace.
-z <file> Load Zend extension <file>.
args... Arguments passed to script. Use -- args when first argument
starts with - or script is read from stdin
--ini Show configuration file names
--rf <name> Show information about function <name>.
--rc <name> Show information about class <name>.
--re <name> Show information about extension <name>.
--ri <name> Show configuration for extension <name>. |
Looks like -d with do it. So I ran the following:
1 | php -d memory_limit=512M myScript.php |
It worked fine.
If you can afford to change your php.ini file, use – -ini to see where your php.ini file is, then make your changes accordingly. You can also use -n to ignore your php.ini file, but then you will be using default parameters which may be even harder to debug.
Validate line by line
Here is a good compilation of codes doing string format validation in C#, VB.NET, Java, Javascript, PHP, Perl, Python, and Ruby. It’s a good reference page.
http://answers.oreilly.com/topic/224-how-to-search-line-by-line-with-a-regular-expression/
Stop UTF 8 with javascript
Below is how you can detect non-ascii characters with javascript.
1 2 3 4 5 6 7 8 | s = $("#something").val();
for (var i=0; n < s.length; i++) {
var c = s.charCodeAt(i);
if (c >= 128) {
alert('Invalid character \'' + String.fromCharCode(c) + '\'');
return false; // or throw an error
}
} |
Of course if you have to take in non-ascii values, then be prepared to set up your environments first. The links below will help.
http://us.php.net/manual/en/function.base64-encode.php
http://pureform.wordpress.com/2008/03/23/make-your-website-completely-utf-8-friendly/