Skip to content

Instantly share code, notes, and snippets.

View glafarge's full-sized avatar

Guillaume Lafarge glafarge

View GitHub Profile
@glafarge
glafarge / request.sql
Created February 27, 2014 16:40
Find rank of an user based on score
SELECT id, username, score,
FIND_IN_SET(
score, (SELECT GROUP_CONCAT(DISTINCT score ORDER BY score DESC) FROM score)
) as rank
FROM scores;
@glafarge
glafarge / vanilla.md
Last active August 29, 2015 14:06
jQuery vs Vanilla JavaScript

jQuery vs JavaScript

$(document).ready(function() {
  // code…
});
document.addEventListener("DOMContentLoaded", function() {
  // code…
});
<form>
<div class="btn-group" data-toggle-name="is_private" data-toggle="buttons" >
<label class="btn btn-default">
<input type="radio" name="private" value="1"> Yes
</label>
<label class="btn btn-default">
<input type="radio" name="private" value="0"> No
</label>>
</div>
<input type="hidden" name="is_private" value="0" />
@glafarge
glafarge / check_array.js
Created February 10, 2016 17:59
Check if variable is Array
/**
* Check whether an object is Array or not
* @type Boolean
* @param {object} subject is the variable that is
* tested for Array identity check
*/
var isArray = (function () {
// Use compiler's own isArray when available
if (Array.isArray) {
return Array.isArray;
@glafarge
glafarge / cron_dispatcher.php
Created March 31, 2016 11:07
Using CRON with CakePHP 3.x if you want to call a controller/action quickly
<?php
$_SERVER[ 'HTTP_HOST' ] = 'localhost'; // or something like yourdomain.tld
require dirname(__DIR__) . '/config/bootstrap.php';
use Cake\Network\Request;
use Cake\Network\Response;
use Cake\Routing\DispatcherFactory;
if(PHP_SAPI == "cli" && $argc == 2) {
@glafarge
glafarge / data.xml
Created February 23, 2018 11:44
Find element by text content with XPath in Python
<root>
<element>A</element>
<element>B</element>
</root>
@glafarge
glafarge / update.sql
Created June 12, 2018 14:12
[MySQL] Increment a col with a counter at once
SELECT @i:=0;
UPDATE contacts SET position = @i:=@i+1 WHERE location_id=1 ORDER BY id;
@glafarge
glafarge / update.sql
Created June 13, 2018 17:43
Change all tables prefixes in database at once
SET @database = "database_name";
SET @old_prefix = "old_prefix_";
SET @new_prefix = "new_prefix_";
SELECT
concat(
"RENAME TABLE ",
TABLE_NAME,
" TO ",
replace(TABLE_NAME, @old_prefix, @new_prefix),
@glafarge
glafarge / promise.js
Created November 16, 2018 08:26
Avoid flickering spinner
var spinner = document.querySelector('.spinner');
var content = document.querySelector('.content');
var refreshButton = document.querySelector('.refreshButton');
function networkRequest() {
return new Promise((resolve, reject) => {
const requestTime = 200; // request duration (for simulation) => 200ms
setTimeout(resolve, requestTime, { payload: { 'foo': 'bar' } });
});
@glafarge
glafarge / mapbox-hover-click.js
Created December 6, 2018 10:09
Mapbox markers hover + click
const map = new mapboxgl.Map({
container: 'map',
style: 'mapbox://styles/****/********',
center: [-0.579180, 44.837788],
zoom: 12.0
});
map.on('load', function() {
// ====================================