Skip to content

Instantly share code, notes, and snippets.

@piercemoore
piercemoore / ajaxdetect.php
Created March 4, 2012 00:46
Super Simple AJAX Detection in PHP
<?php
/*
* isAjax determines whether a server request is a standard browser-based request or if it is being loaded via AJAX.
*
* REMEMBER: Do NOT use this for security purposes as it is VERY easy to spoof headers.
*
* @return bool
*/
function isAjax() {
@piercemoore
piercemoore / placefooter.jquery.js
Created March 4, 2012 00:56
jQuery Function to Place Footer at the bottom of the page
<script type="text/javascript">
function placeFooter() {
if( $(document.body).height() < $(window).height() ) {
$("#footer").css({position: "absolute", bottom:"0px"});
} else {
$("#footer").css({position: ""});
}
}
@piercemoore
piercemoore / gist:2253239
Created March 30, 2012 17:37
Handling unlimited/variable function arguments
function showAllArgs( $item1, $item2, $item3, etc...) {
$i = 0;
foreach( func_get_args() as $key => $val ) {
// $val is the value that you supplied to the parameter.
print "You supplied '$val' for argument #$i. \n";
$i++;
}
}
@piercemoore
piercemoore / gist:2292935
Created April 3, 2012 15:29
Simple Recursion Function within a Class
<?php
class Foo {
function __construct( $array ) {
$this->storage = $this->store($array);
}
function store( $array ) {
@piercemoore
piercemoore / redditReaderBasic.php
Created July 13, 2012 16:55
Quick Reddit reader in PHP
<?php
// Full disclosure: I wrote this while writing a Twitter Bootstrap enabled site, so the class tags are Twitter bootstrap enabled. Shouldn't be hard to style, though.
$subReddit = "webdev"; // Enter the subreddit name here with no /r/ in front
$selfTextLimit = 250; // Character limit for self text entries
$pageData = json_decode( file_get_contents( "http://www.reddit.com/r/$subReddit/.json" ) );
foreach( $pageData->data->children as $post ): ?>
@piercemoore
piercemoore / arrayToObject.php
Created July 13, 2012 22:02
Convert a multi-dimensional array into an object
<?php
// Full disclosure: I found this solution here: http://www.richardcastera.com/blog/php-convert-array-to-object-with-stdclass and implemented it both here and in my project.
function arrayToObject( $array ) {
if ( !is_array( $array ) && !is_object( $array ) ) {
return $array;
}
$obj = new stdClass();
if( ( is_array( $array ) || is_object( $array ) ) && count( $array ) > 0 ) {
foreach( $array as $k=>$v ) {
@piercemoore
piercemoore / pm_codepage.php
Created October 3, 2012 07:17
Code page for PierceMoore.com
class Snippets {
public $snips;
function __construct() {
$this->snips = new stdClass;
}
@piercemoore
piercemoore / feedback.js
Created November 3, 2012 01:23
Mongoose Schema to store GitHub Issues
var githubSchema = new Schema({
"url": String,
"html_url": String,
"number": Number,
"state": String,
"title": String,
"body": String,
"user": {
"login": String,
"id": Number,
@piercemoore
piercemoore / print_r_alternative.php
Last active October 12, 2015 09:18
Recurse through an object and display formatted contents
<?php
private function dig( $obj , $level = 0 ) {
$return = "";
$tabs = str_repeat( "\t" , $level );
foreach( $obj as $k => $v ) {
if( self::keyed( $v ) ) {
$return .= $tabs . "$k object: \n";
@piercemoore
piercemoore / medianet_JSON.js
Created December 11, 2012 17:48
Example displaying differences in XML and JSON - JSON Side
{
"Success": true,
"ResultsReturned": 1,
"TotalResults": 1065493,
"Artists": [
{
"MnetId": "1097",
"Name": "Alicia Keys",
"Images": {
"Artist180x80": "http://images.mndigital.com/artists/000/001/097/b.jpeg",