Skip to content

Instantly share code, notes, and snippets.

@ajcrites
ajcrites / gist:3695911
Created September 11, 2012 04:13
Update from mysql_* to PDO
/**#@+
* OLD EVIL METHOD
*/
mysql_connect('localhost', $username, $password)
mysql_select_db('App');
$userid = mysql_real_escape_string($_GET['userid']);
$query = <<<SQL
SELECT
username
FROM
@ajcrites
ajcrites / gist:3695946
Created September 11, 2012 04:25
Unnamed vs. Named prepared statements in PDO
//UNNAMED
$query = "SELECT userid FROM t1 WHERE (username = ? OR useremail = ?)
AND usertoken = ?";
$prepared_statement = $db_connection->prepare($query);
$prepared_statement->execute(array($_POST['login'], $_POST['login'],
$_POST['token'])
);
//NAMED
$query = "SELECT userid FROM t1 WHERE (username = :login
<?php
class DB extends PDO {
public function __construct($dsn = null, $user = null, $pass = null) {
$dsn = $dsn !== null ? $dsn : "mysql:host=localhost;dbname=$_SERVER[DBNAME]";
$user = $user !== null ? $user : $_SERVER['DBUSER'];
$pass = $pass !== null ? $pass : $_SERVER['DBPASS'];
parent::__construct($dsn, $user, $pass);
$this->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
}
@ajcrites
ajcrites / 0-all-together.css
Last active October 10, 2015 21:08
CSS for fancy buttons
.final {
/* Basic Styles for the look */
color: white;
padding: 8px;
cursor: pointer;
font-size: 100%;
font-family: Arial;
/* Old browser background-color */
background: #0d4e99;
@ajcrites
ajcrites / gist:3818056
Created October 2, 2012 10:25
Ad-hoc jQuery Ajax Queue
function AjaxQueue () {
this.q = [];
}
AjaxQueue.prototype = {
push: function (options) {
options = $.extend({'complete': function () {}}, options);
var originalComplete = options.complete
var qa = this;
options.complete = function () {
@ajcrites
ajcrites / gist:4562156
Last active December 11, 2015 06:49
Truncate element text length to 100 characters, but do not split words (you can go over 100 characters to do so). If it is truncated, i.e. shorter than the original length, add `...`
/* Setup */
for (var x = 0; x < 5; x++) {
$("<div>").appendTo("body");
}
$("div").each(function () {
$(this).text((new Array(Math.floor(20 + Math.random() * 4))).join('text '));
});
/* Truncation */
@ajcrites
ajcrites / gist:4566175
Last active December 11, 2015 07:28
Attempt at mediator implementation for handling `before`/`after` controller requests.
<?php
$controller = $container->create($request->controller);
$mediator = $container->create('BeforeMediator');
$controller->registerMediator($mediator);
$controller->triggerActions();
$response = $container->create('Response');
$controller->{'action' . $request->action}($request, $response);
@ajcrites
ajcrites / gist:4576528
Last active December 11, 2015 08:58
Passing defined methods/functions (sort of)
<?php
class Mediator {
private $events;
public function attach($event, Callable $callback) {
if (!isset($this->events[$event])) {
$this->events[$events] = array();
}
$this->events[$event][] = $callback;
}
@ajcrites
ajcrites / Script.js
Last active December 11, 2015 21:49
Autocomplete Demo
$("input").autocomplete({
source: $('li').map(function() {
return $(this).text();
}).get(),
close: function () {
$(this).trigger('blur');
},
minLength: 0
})
.on('focus', function () {
@ajcrites
ajcrites / results.md
Last active December 12, 2015 06:49
Attempts to check how efficient multiple strpos checks are relative to the same number of PCRE function checks. String length has a significant impact on finding a string with a regular expression pattern. You can specify how long the string is via an argument to the script. `strpos` is always faster; period. It takes the same amount of time eve…
$ php strposvsstriposvspregbench.php 1
0.40396499633789
0.58028793334961
0.93481206893921
0.91582679748535

$ php strposvsstriposvspregbench.php 100
0.42313885688782
0.50706505775452

4.8115060329437