Zero Padded Numbers

function getZeroPaddedNumber($value, $padding) {
       return str_pad($value, $padding, "0", STR_PAD_LEFT);
}

Usage

echo getZeroPaddedNumber(123, 4);
// outputs "0123"
Avatar of Chris Coyier
Chris Coyier on

Generate CSV from Array

function generateCsv($data, $delimiter = ',', $enclosure = '"') {
       $handle = fopen('php://temp', 'r+');
       foreach ($data as $line) {
               fputcsv($handle, $line, $delimiter, $enclosure);
       }
       rewind($handle);
       while (!feof($handle)) {
               $contents .= fread($handle, 8192);
       }
       fclose($handle);
       return $contents;
}

Usage

$data = array(
       
Avatar of Chris Coyier
Chris Coyier on

Concatenate Array for Human Reading

function concatenate($elements, $delimiter = ', ', $finalDelimiter = ' and ') {
       $lastElement = array_pop($elements);
       return join($delimiter, $elements) . $finalDelimiter . $lastElement;
}

Usage

$array = array('John', 'Mary', 'Ishmal');
echo concatenate($array);
// outputs "John, Mary and Ishmal"

Provide second parameter, …

Avatar of Chris Coyier
Chris Coyier on

Check if Checkbox is Checked

Say that 10 times fast =).

Find out if a single checkbox is checked or not, returns true or false:

$('#checkBox').attr('checked');

Find all checked checkboxes:

$('input[type=checkbox]:checked');
Avatar of Chris Coyier
Chris Coyier on (Updated on )

#76: A Tour of jQuery on a Live Site

I’m busy, you’re busy, we’re all busy trying to meet deadlines and get things done in this crazy web world. I love learning new technologies when I can, but I’m the first to admit that I reach for tools that …

Avatar of Chris Coyier
Chris Coyier on (Updated on )

Click Once and Unbind

Have something happen on a click event, but only once! Unbind the click handler after the element has been clicked once.

$('#my-selector').bind('click', function() {
       $(this).unbind('click');
       alert('Clicked and unbound!');
});

Also see Ben’s comment below for jQuery’s .one() function which is …

Avatar of Chris Coyier
Chris Coyier on (Updated on )

Five Questions with David Walsh

There is probably a good chance if you read this blog that you also know David Walsh. David and I hail from the same hometown (Madison, Wisconsin) and have been friends for years. We’ve worked on a few projects together …

Avatar of Chris Coyier
Chris Coyier on (Updated on )