Use PHP to count number of files in a directory

PostMar 21st, 2012 | Comments (0)
Here's a simple bit of PHP code to get the total number of files in a directory using the glob() and count() functions:

$num_files = count(glob($_SERVER['DOCUMENT_ROOT'].'/path/to/directory/*'));

Or here's how to only count files with a .jpg extension:

$num_files = count(glob($_SERVER['DOCUMENT_ROOT'].'/path/to/directory/*.jpg'));

How to kill all MySQL sleeping processes

PostJan 30th, 2012 | Comments (1)
If you need to kill all sleeping processes in MySQL, here's a quick 'n' easy PHP solution:

$result = mysql_query("SHOW processlist");
while ($myrow = mysql_fetch_assoc($result)) {
    if ($myrow['Command'] == "Sleep") {
        mysql_query("KILL {$myrow['Id']}");
    }
}

HTML5 audio not playing in Firefox? Use OGG.

PostFeb 24th, 2011 | Comments (0)
If you're attempting to play sounds using the new HTML5 <audio> tag (or using the .play() Javascript API method), and it isn't working in Firefox, it's because Firefox won't play MP3s or WAVs. You'll have to convert your sound files to OGG files.

if you're a Mac user, download the XiphQT Quicktime component. It will let you export sound files as .ogg files using Quicktime Player 7 (located in your Utilities folder).

Remove the default padding from a jQuery UI dialog

PostJan 12th, 2011 | Comments (1)
If you find the default padding on jQuery UI dialogs to be as annoying as I do, here's how you can remove it.

Make sure the follow style loads after the jQuery UI stylesheet has loaded, and it will override the default padding:

.ui-dialog .ui-dialog-content {
    padding: 0;
}

I like to have my sites' main stylesheet load after the jQuery UI stylesheet, so that I can include any jQuery UI overrides in it.

Toggle a MySQL field between 0 and 1

PostDec 20th, 2010 | Comments (1)
Here's a simple example of how to toggle an integer (most often a tinyint in my case) field in MySQL between 0 and 1:

UPDATE table SET int_field=MOD(int_field+1,2) WHERE id=123

Facebook og: <meta> tags not working? Lint the page.

PostDec 8th, 2010 | Comments (4)
If you're implementing a Facebook Share or Facebook Like button, and your og: meta tags are being ignored, it's probably Facebook's overzealous cache.

The quickest way to clear Facebook's cache, and force Facebook to use the info from your og: meta tags, is to Lint the URL.

Enter the URL of the webpage that contains the og: meta tags in the Linter, and Facebook will clear its cache for that page. Your Share or Like button should then start pulling the correct info from your og: meta tags.

Check for modifier keys when using jQuery's .keyPress()

PostJul 6th, 2010 | Comments (4)
I was developing a simple chat log for a website, and I wanted the user to be able to submit a message when they hit the "enter" key but not if the "alt/option" key is being pressed. If the "alt/option" key is being pressed while the user hits the "enter" key, I want a carriage return to be entered in the chat field instead.

The jQuery .keyPress() doc page is a bit light on details, so here's how you can check for modifier keys with the .keyPress() event handler:

$('textarea#chat_message').keypress(function(e) {
    if (e.which == 13) { // enter key was pressed
        if (e.altKey) { // alt/option key is down
            // don't submit
        }
        else {
            // submit
        }
    }
});

The mofifier keys you can test for are:

altKey - alt/option key
ctrlKey - control key
shiftKey - shift key
metaKey - control key on PCs, control and/or command key on Macs

The Goose is Loose

PostMay 4th, 2010 | Comments (0)
I finally got around to posting screenshots of the Goose is Loose project I did last year for the Libertyville Sunrise Rotary Club.

Goose is Loose Festival


Midwest Farm Connection and Negev Direct Marketing

PostApr 21st, 2010 | Comments (1)
I just added a bunch of screen shots from my latest couple client projects:

Midwest Farm Connection




Negev Direct Marketing


Replace all quotes (&quot;) inside HTML tags using PHP

PostMar 31st, 2010 | Comments (1)
If you've ever stored HTML tags in a database and filtered the contents through PHP's htmlentities(), you've probably run into the problem of the quotes inside the tags being converted into &quot; entities.

Example:

<a href="http://www.epigroove.com/">Epigroove.com</a>

turns into:

<a href=&quot;http://www.epigroove.com/&quot;>Epigroove.com</a>

Most browsers can handle this, but IE can not.

Solution:

$text = preg_replace('/<([^<>]+)>/e', '"<" .str_replace("&quot;", \'"\', "$1").">"', $text);

This replaces all instances of '&quot;' inside HTML tags.
Visit the archives to see older posts