Skip to content

Instantly share code, notes, and snippets.

@vordan
vordan / build_tree.php
Last active February 5, 2023 09:13 — forked from ziishaned/output.json
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 / password.php
Created December 27, 2021 20:35
Generate and restore password hash #php #password #hash
// Save a password hash:
// ======================
$options = [
'cost' => 11,
];
// Get the password from post
$passwordFromPost = $_POST['password'];
$hash = password_hash($passwordFromPost, PASSWORD_BCRYPT, $options);
@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 / mysql-events.js
Last active December 13, 2021 20:00
A node.js package that watches a MySQL database and runs callbacks on matched events. #node #JavaScript #mysql
mysql-events
https://www.npmjs.com/package/sql-events-listener
A node.js package that watches a MySQL database and runs callbacks on matched events.
Install
npm install @rodrigogs/mysql-events
MySQL configuration:
Enabling Binary Logging. Use SHOW BINARY LOGS query to determine whether Binlog has been enabled or not.
@vordan
vordan / Sort an Array of Objects in JavaScript.js
Last active December 13, 2021 11:58
Sort an Array of Objects in JavaScript. How to sort an array of objects by the values of the object’s properties
//-------------------------------------------------------
// Sort an Array of Objects in JavaScript
//
// To sort an array of objects, you use the sort() method
// and provide a comparison function that determines
// the order of objects.
//-------------------------------------------------------
//-------------------------------------------------------
// Suppose that you have an array of employee objects as follows:
@vordan
vordan / format-money.js
Last active December 12, 2021 10:42
Format Money Number
Number.prototype.formatMoney = function(c, d, t){
c = isNaN(c = Math.abs(c)) ? 2 : c;
d = d == undefined ? "." : d;
t = t == undefined ? "," : t;
var n = this;
var s = n < 0 ? "-" : "";
var i = String(parseInt(n = Math.abs(Number(n) || 0).toFixed(c)));
var j = (j = i.length) > 3 ? j % 3 : 0;
return s + (j ? i.substr(0, j) + t : "") + i.substr(j).replace(/(\d{3})(?=\d)/g, "$1" + t) + (c ? d + Math.abs(n - i).toFixed(c).slice(2) : "");