Skip to content

Instantly share code, notes, and snippets.

@vordan
vordan / build_tree.php
Last active February 5, 2023 09:13
Build a tree from a flat array in PHP
<?php
//--------------------------------------------------------
// Example:
//
// $cars = [
// ['id' => 999, 'value' =>'CARS', 'parent_id' => 0 ],
// ['id' => 11, 'value' =>'Toyota', 'parent_id' => 999],
// ['id' => 1, 'value' =>'Avalon', 'parent_id' => 11 ],
// ['id' => 2, 'value' =>'Corolla', 'parent_id' => 11 ],
// ['id' => 3, 'value' =>'Camry', 'parent_id' => 11 ],
<?php
/**
* Looks for unquoted keys in a json string and fixes them ie: {a:"b"} => {"a":"b"}
* @param string $string A json string that is suspect
* @return string A valid json string
*/
function fix_json($string){
// (no qupte) (word) (no quote) (semicolon)
$regex = '/(?<!")([a-zA-Z0-9_]+)(?!")(?=:)/i';
function getBase64FromImageUrl(url) {
var img = new Image();
img.setAttribute('crossOrigin', 'anonymous');
img.onload = function () {
var canvas = document.createElement("canvas");
canvas.width =this.width;
canvas.height =this.height;
@vordan
vordan / datatables.js
Created June 8, 2022 18:24 — forked from usmcamp0811/datatables.js
Custome PDF in DataTables Example
$(document).ready(function() {
// Function to convert an img URL to data URL
function getBase64FromImageUrl(url) {
var img = new Image();
img.crossOrigin = "anonymous";
img.onload = function () {
var canvas = document.createElement("canvas");
canvas.width =this.width;
canvas.height =this.height;
var ctx = canvas.getContext("2d");
@vordan
vordan / install_php5.3_on_ubuntu.txt
Created March 7, 2022 07:05 — forked from szihaj/install_php5.3_on_ubuntu.txt
How to install PHP 5.3 on Ubuntu
sudo add-apt-repository ppa:sergey-dryabzhinsky/php53
sudo apt-get update
sudo apt-get install php53-common php53-cli
# to see a list of available packages:
# apt-cache search php53
# ex.: sudo apt-get install php53-mod-mysql
# Apache module:
sudo apt-get install libapache2-mod-php53
@vordan
vordan / nearby-coordinates.sql
Created December 15, 2021 19:40 — forked from statickidz/nearby-coordinates.sql
Ordering with SQL by nearest latitude & longitude coordinates (MySQL & SQLite)
---
METHOD 1
This should roughly sort the items on distance in MySQL, and should work in SQLite.
If you need to sort them preciser, you could try using the Pythagorean theorem (a^2 + b^2 = c^2) to get the exact distance.
---
SELECT *
FROM table
ORDER BY ((lat-$user_lat)*(lat-$user_lat)) + ((lng - $user_lng)*(lng - $user_lng)) ASC
@vordan
vordan / long-polling.html
Created November 20, 2021 08:43 — forked from kixxauth/long-polling.html
A Node.js http long polling server for HTTP streaming.
<!DOCTYPE html>
<html>
<style type="text/css">
body {
font-size: 18px;
background: #000;
color: #fff;
}
#container {
width: 600px;
@vordan
vordan / gist:0dd0e382e6d1dfe277a05254f1cdf3ad
Created February 21, 2017 20:37 — forked from tleen/gist:5110817
Format a Javascript Date to HTML5 time element datetime using moment.js
var html5datetime = moment(yourDate).format('YYYY-MM-DDTHH:mm:ssZ')
@vordan
vordan / domIsReady.js
Created February 19, 2017 15:20 — forked from devbyray/domIsReady.js
Vanilla JavaScript Document Ready (Cross-Browser)
var domIsReady = (function(domIsReady) {
var isBrowserIeOrNot = function() {
return (!document.attachEvent || typeof document.attachEvent === "undefined" ? 'not-ie' : 'ie');
}
domIsReady = function(callback) {
if(callback && typeof callback === 'function'){
if(isBrowserIeOrNot() !== 'ie') {
document.addEventListener("DOMContentLoaded", function() {
return callback();
@vordan
vordan / multi_dashboard.html
Created January 31, 2016 08:35 — forked from battlehorse/multi_dashboard.html
A Google Charts dashboard setup where multiple datatables are used to power a single 'logical' dashboard.
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=utf-8"/>
<title>
Google Visualization API Sample
</title>
<script type="text/javascript" src="http://www.google.com/jsapi"></script>
<script type="text/javascript">
google.load('visualization', '1', {packages: ['controls']});