Skip to content

Instantly share code, notes, and snippets.

@olimortimer
olimortimer / gist:6932037
Last active December 25, 2015 06:29
MySQL: Find Duplicates
-- Duplicate count
SELECT <column>, COUNT(*) count FROM <table> GROUP BY <column> HAVING count > 1;
-- Find duplicates
SELECT * FROM <table>
INNER JOIN (SELECT <column> FROM <table>
GROUP BY <column> HAVING count(id) > 1) duplicate ON <table1>.<column> = duplicate.<column>
@olimortimer
olimortimer / gist:6795397
Last active December 24, 2015 12:09
JS: Pad number inputs to 2 decimal places
// Used to pad numbers inputs to 2 decimal places
function padNumber($input) {
// Rather than store $(this).val(), it's best to keep checking for changes
// When we move off the input
$input.on('blur', function () {
// Ignore if there's no value
if ($(this).val() === '') return;
@olimortimer
olimortimer / gist:6775681
Created October 1, 2013 08:56
JS: Select2 FastClick Fix
$('#select2').select2({
data: data,
// Add our 'needsclick' to each item, so FastClick doesn't get applied
formatResult: function(result, container, query, escapeMarkup) {
container.addClass('needsclick');
return result.text;
}
});
@olimortimer
olimortimer / gist:6761771
Created September 30, 2013 10:07
JS: Bootstrap Typeahead FastClick 'needsclick'
// Add our 'needsclick' to our a href for each item, so FastClick doesn't get applied
$('#typehead').data('typeahead').render = function (items) {
// We basically do what the normal typeahead function does...
var that = this;
items = $(items).map(function (i, item) {
i = $(that.options.item).attr('data-value', item);
// ...but we add 'needsclick' to the a href
i.find('a').addClass('needsclick').html(that.highlighter(item));
return i[0];
@olimortimer
olimortimer / gist:6113271
Created July 30, 2013 14:14
PHP: Random Password
<?php
function randomPassword($length = 8) {
$password = false;
$characters = 'abcdefghijklmnopqrstuwxyzABCDEFGHIJKLMNOPQRSTUWXYZ0123456789';
$charsLength = strlen($characters) - 1;
for ($i = 0; $i < $length; $i++) {
@olimortimer
olimortimer / parsecsv.php
Last active December 16, 2015 18:50
PHP: Parse CSV into an array
<?php
function parse_csv($filepath, $namedfields = true) {
$max_row_size = 0; // Maximum row size - 0 is unlimited
$delimiter = ','; // The field delimiter
$enclosure = '"'; // The field enclosure character
$fields = $content = false;
// Open our files
@olimortimer
olimortimer / gist:5309228
Last active February 25, 2019 20:54
MySQL: Find and replace non-ASCII characters (ie, after an Excel import)
-- This finds all records with non-ASCII characters
SELECT * FROM locations WHERE NOT HEX(name) REGEXP '^([0-7][0-9A-F])*$';
UPDATE locations SET name = REPLACE(name, '‡', 'á'); -- HEX E280A1 / HEX C3A1
UPDATE locations SET name = REPLACE(name, 'Ž', 'é'); -- HEX C5BD / HEX C3A9
UPDATE locations SET name = REPLACE(name, 'Ÿ', 'ü'); -- HEXC5B8 / HEX C3BC
UPDATE locations SET name = REPLACE(name, 'š', 'ö'); -- HEX C5A1 / HEX C3B6
UPDATE locations SET name = REPLACE(name, 'Š', 'ä'); -- HEX C5A0 / HEX C3A4
UPDATE locations SET name = REPLACE(name, '§', 'ß'); -- HEX C2A7 / HEX C39F
UPDATE locations SET name = REPLACE(name, 'Õ', '\''); -- HEX C395 / HEX 27
@olimortimer
olimortimer / gist:5299980
Created April 3, 2013 10:11
Create chained dropdowns on the fly
<select name="location_tree[1]" id="location-tree-1">
<option value="0" selected="selected">Please Select...</option>
<option value="2">Africa</option>
<option value="203">Asia</option>
<option value="332">Asia Pacific</option>
<option value="1299">Caribbean</option>
<option value="1749">Central America</option>
<option value="1925">Europe</option>
<option value="17949">India &amp; Indian Ocean</option>
<option value="18058">Middle East</option>
@olimortimer
olimortimer / rebuild_tree.php
Last active March 17, 2019 08:04
PHP: Convert 'Adjacency List Model' database to a 'Nested Set Model' database
<?php
rebuild_tree(1, 1);
function rebuild_tree($parent_id, $left) {
// The right value of this node is the left value + 1
$right = $left+1;
// Get all children of this node
@olimortimer
olimortimer / ral_colours.php
Last active April 22, 2023 16:19
PHP: RAL Colours as RGB
<?php
// Includes text RGB to show text as white or black. This value was calculated using;
// $brightness = sqrt( (R * R * .299) + (G * G * .587) + (B * B * .114) )
// If $brightness was greater than 130, then the text was set to white
$ral_colours => array(
'RAL 1000' => array(
'rgb' => '190,189,127',
'name' => 'Green Beige',