Useful Code Snippets:
Below are some useful techniques for those who already know what they're doing.
Please visit my websites "Under One Roof".
Enjoy!
ps. if you find errors or have suggestions, please let me know. |
|
|
MySQL:
"Week of Month" or "Occurence of Weekday" select statement.
MySQL does not have this built in function but this fairly complicated statement is a work around. Typical use would be selecting the 3rd monday of the month. Displayed with "SET" for clarity.
Versions lower than 4.1:
Versions 4.1 and above: (using LAST_DAY)
It works by applying the following formula:
SELECT FLOOR ( x / ( 7 + ( 7 / ( ( y + 1 ) - z ) ) ) + 1.03 );
Where:
d = date
x = day of month
y = days in month
z = 1st day of month
7 = days in a week
In a nutshell:
It basically divides the date by a week, calculating for incomplete weeks in the month.
Uses FLOOR and +1 instead of CEIL to prevent errors if zero.
Adds another .03 to correct for short months.
|
Drag and Drop: |
Retrieving Dynamic Data From External File:
Here is a technique for retrieving data sans XMLHttpRequest. An AJAX workaround.
Javascript:
Perl: (or other server-side scripting language)
HTML:
|
Javascript:
Image preview before uploading:
Useful for admin functions. Your file path may be different.
Javascript:
HTML:
|
Perl:
Using the second of the year as a 9 digit unique identifier:
Example: 823366360
|
Perl:
Parse the above identifier back to human readable:
Example: Thu, 21 Aug 2008 18:26:00 GMT
|
Procmail & Perl:
Extract sender from email using formail and pass to a Perl script: (then kill it)
.procmailrc recipe:
Perl script:
|
.htaccess:
"dupe" images directory for SSL access.
First:
Mirror the directory path to images directory inside the secure_html directory and place the following .htaccess file in it.
(ie. "/public_html/images/" = "/secure_html/images/").
Do not mirror the actual image files.
.htaccess code:
|
Javascript:
Using an associative array instead of conditionals to "flip" a variable.
Javascript:
test
|
Locally stored passwords:
Use a locally stored Javascript src file to write password information into your administration page. innerHTML & IDs might be more elegant
In the admin.js file stored locally on your computer.
In the head of your admin page: (the path to admin.js - yours will be different)
In the body of your admin page: (Perl generated)
|
Miscellaneous Perl:
Informative MySQL errors: (email to admin)
Order string to hash:
Print table columns:
|