Skip to content

Instantly share code, notes, and snippets.

@cam-gists
cam-gists / duplicates.sql
Created June 20, 2012 22:01
MySQL : Remove Duplicates
DELETE FROM table1
USING table1, table1 as vtable
WHERE (NOT table1.ID=vtable.ID)
AND (table1.field_name=vtable.field_name)
@cam-gists
cam-gists / functions.js
Created June 28, 2012 00:29
JavaScript: NVD3.js
/**
anonymous function to init graph
*/
function callAjax(controller, method, params) {
return $.ajax({
url: '/th_ci/' + controller + '/' + method + '/' + params,
type: 'POST',
dataType: 'json',
success: function(data){}
});
@cam-gists
cam-gists / pop.js
Created June 28, 2012 23:38
JavaScript: Popunder
var PopURL = 'http://www.google.com';
var PopWidth = 1100;
var PopHeight = 700;
var PopCookieTimeout = 3600;
// TH - Popper Date 6-28-2012
//
var P = false;
var W = 0;
var B = null;
@cam-gists
cam-gists / csv.php
Created July 2, 2012 19:37
PHP: generate CSV
<?php
function toCSV(array $data, array $colHeaders = array(), $asString = false) {
$stream = ($asString)
? fopen("php://temp/maxmemory", "w+")
: fopen("php://output", "w");
if (!empty($colHeaders)) {
fputcsv($stream, $colHeaders);
}
@cam-gists
cam-gists / auot.php
Created July 2, 2012 19:39
PHP: Auto Load Classes
<?php
spl_autoload_register(function ($classname) {
$classname = ltrim($classname, "\\");
preg_match('/^(.+)?([^\\\\]+)$/U', $classname, $match);
$classname = str_replace("\\", "/", $match[1])
. str_replace(["\\", "_"], "/", $match[2])
. ".php";
include_once $classname;
});
@cam-gists
cam-gists / template.php
Created July 2, 2012 19:45
PHP: Template Class + usage
<?php
//template.class.php
class Template
{
protected $dir;
protected $vars;
public function __construct($dir = "") {
$this->dir = (substr($dir, -1) == "/") ? $dir : $dir . "/";
$this->vars = array();
@cam-gists
cam-gists / fgc.php
Created July 2, 2012 19:47
PHP: file_get_contents vs. cURL
<?php
$html = file_get_contents("http://example.com/product/42");
$context = stream_context_create(array(
"http" => array(
"method" => "POST",
"header" => "Content-Type: multipart/form-data; boundary=--foo\r\n",
"content" => "--foo\r\n"
. "Content-Disposition: form-data; name=\"myFile\"; filename=\"image.jpg\"\r\n"
. "Content-Type: image/jpeg\r\n\r\n"
@cam-gists
cam-gists / valid.php
Created July 2, 2012 21:15
PHP: Validate Vars
<?php
// Check Range of Value
$value = filter_input(INPUT_GET, "value", FILTER_VALIDATE_INT,
array("options" => array("min_range" => 15, "max_range" => 20)));
if ($value) {
// run my code
}
else {
// handle the issue
}
@cam-gists
cam-gists / 1darray.php
Created July 2, 2012 21:34
PHP: single Dimension Array Iterator
<?php
// an array (using PHP 5.4's new shorthand notation)
$arr = ["sitepoint", "phpmaster", "buildmobile", "rubysource",
"designfestival", "cloudspring"];
// create a new ArrayIterator and pass in the array
$iter = new ArrayIterator($arr);
// loop through the object
foreach ($iter as $key => $value) {
@cam-gists
cam-gists / mda.php
Created July 2, 2012 21:36
PHP: Multi Dimensional Array Iterator
<?php
// a multidimensional array
$arr = [
["sitepoint", "phpmaster"],
["buildmobile", "rubysource"],
["designfestival", "cloudspring"],
"not an array"
];
// loop through the object