Skip to content

Instantly share code, notes, and snippets.

View jlittlejohn's full-sized avatar

Josh Littlejohn jlittlejohn

View GitHub Profile
@jlittlejohn
jlittlejohn / gist:5685384
Created May 31, 2013 14:33
PHP: Namespacing
<?php
namespace myNamespace;
class DateTime{
public function customMethod(){
return 'Fun Times';
}
}
/*
* Create a new DateTime object that's in this current namespace
@jlittlejohn
jlittlejohn / gist:5685375
Created May 31, 2013 14:31
PHP: Exceptions
<?php
try{
$dsn = 'mysql:dbname=test;host=localhost';
$dbh = new PDO($dsn,'username','password');
echo 'Connected to Database';
}catch (PDOException $e){
echo 'Connection Failed: '.$e->getMessage();
}catch(Exception $e){
echo 'A Unknown Error Occurred: '.$e->getMessage();
}
@jlittlejohn
jlittlejohn / gist:5685365
Created May 31, 2013 14:30
PHP: DateTime Class
<?php
/* Get server time */
$date = new DateTime();
echo $date->format('Y-m-d H:i:s');
/* Add 1 Day */
$date->add(new DateInterval('P1D'));
echo '<br>'.$date->format('Y-m-d H:i:s');
/* Change Timezone to UTC */
$date->setTimezone(new DateTimeZone('etc/UTC'));
echo '<br>'.$date->format('Y-m-d H:i:s');
@jlittlejohn
jlittlejohn / gist:5685363
Created May 31, 2013 14:29
PHP: JSON Decode
<?php
/* Set Content Encoding Header */
$json = '[{"type":"cat","name":"Alfie","age":2},{"type":"dog","name":"Bella","age":6}]';
/* Parse JSON to Object */
$object = json_decode($json);
/* Parse JSON to Array */
$array = json_decode($json,true);
/* Print Object */
var_dump($object);
/* Print Array */
@jlittlejohn
jlittlejohn / gist:5685356
Created May 31, 2013 14:29
PHP: JSON Encode
<?php
/* Set Content Encoding Header */
header('Content-type: application/json; charset=utf-8');
/* Array of animals*/
$myArray = array(
array('type'=>'cat','name'=>'Alfie','age'=>2),
array('type'=>'dog','name'=>'Bella','age'=>6)
);
/* Encode to JSON */
$jsonString = json_encode($myArray);
@jlittlejohn
jlittlejohn / gist:5685346
Created May 31, 2013 14:27
PHP: PHP Data Object Class
<?php
try{
/* Connect to Database */
$dsn = 'mysql:dbname=test;host=localhost';
$dbh = new PDO($dsn,'username','password');
/* Create SQL String with parameters */
$sql = 'SELECT name, colour, calories
FROM fruit
WHERE calories < :calories AND colour = :colour';
@jlittlejohn
jlittlejohn / gist:5639677
Created May 23, 2013 21:45
JS: Sticky Footer
function stickyNav() {
var footer = $("footer");
var pos = footer.position();
var height = $(window).height();
height = height - pos.top;
height = height - footer.height();
if (height > 0) {
footer.css({'margin-top' : height+'px'});
}
}
@jlittlejohn
jlittlejohn / gist:5624688
Created May 22, 2013 01:47
SCSS: Keyframes (Mixin)
@mixin keyframes($name) {
@-webkit-keyframes #{$name} {
@content;
}
@-moz-keyframes #{$name} {
@content;
}
@-o-keyframes #{name} {
@content;
}
@jlittlejohn
jlittlejohn / gist:5613047
Created May 20, 2013 15:41
WP: pre_get_posts Filter
// Use as an alternative to query_posts. note: doesn't work for pages
add_action('pre_get_posts','wpse50761_alter_query');
function wpse50761_alter_query($query){
if( $query->is_main_query() ){
//Do something to main query
}
}
@jlittlejohn
jlittlejohn / gist:5535817
Created May 7, 2013 20:24
JS: Back to Top
# back to top
$ ->
$(window).scroll ->
if $(this).scrollTop() > 100
$("#back-to-top").fadeIn()
else
$("#back-to-top").fadeOut()
# scroll body to 0px on click