/ Gists

Gists

On gists

List MySQL Database sizes

MySql

List MySQL Database sizes.sql #

SELECT
  table_schema AS 'Db Name',
  Round(Sum(data_length + index_length) / 1024 / 1024, 3) AS 'Db Size (MB)',
  Round(Sum(data_free) / 1024 / 1024, 3) AS 'Free Space (MB)'
FROM
  information_schema. TABLES
GROUP BY
  table_schema;

On gists

Get Array Values Recursively

PHP

demo.php #

<?php
 
/**
 * Get Array Values PHP Recursively
 *
 * https://davidwalsh.name/get-array-values-with-php-recursively
 *
 * @param array $array The source array.
 * @return array The flattened array values.
 */
function array_values_recursive($array)
{
    $flat = array();
 
    foreach ($array as $value) {
        if (is_array($value)) {
            $flat = array_merge($flat, array_values_recursive($value));
        } else {
            $flat[] = $value;
        }
    }
 
    return $flat;
}

On gists

Sanitize all PHP Super Globals

PHP Protecting

demo.php #

<?php
 
//
// Sanitize all dangerous PHP super globals.
//
// The FILTER_SANITIZE_STRING filter removes tags and remove or encode special
// characters from a string.
//
// Possible options and flags:
//
//   FILTER_FLAG_NO_ENCODE_QUOTES - Do not encode quotes
//   FILTER_FLAG_STRIP_LOW        - Remove characters with ASCII value < 32
//   FILTER_FLAG_STRIP_HIGH       - Remove characters with ASCII value > 127
//   FILTER_FLAG_ENCODE_LOW       - Encode characters with ASCII value < 32
//   FILTER_FLAG_ENCODE_HIGH      - Encode characters with ASCII value > 127
//   FILTER_FLAG_ENCODE_AMP       - Encode the "&" character to &amp;
//
//
// <?php
//
// // Variable to check
// $str = "<h1>Hello WorldÆØÅ!</h1>";
//
// // Remove HTML tags and all characters with ASCII value > 127
// $newstr = filter_var($str, FILTER_SANITIZE_STRING, FILTER_FLAG_STRIP_HIGH);
// echo $newstr;
//  -> Hello World!
//
// ?>
//
 
foreach ($_GET as $key => $value)
{
    $_GET[$key] = filter_input(INPUT_GET, $key, FILTER_SANITIZE_STRING);
}
 
foreach ($_POST as $key => $value)
{
    $_POST[$key] = filter_input(INPUT_POST, $key, FILTER_SANITIZE_STRING);
}
 
foreach ($_COOKIE as $key => $value)
{
    $_COOKIE[$key] = filter_input(INPUT_COOKIE, $key, FILTER_SANITIZE_STRING);
}
 
foreach ($_SERVER as $key => $value)
{
    $_SERVER[$key] = filter_input(INPUT_SERVER, $key, FILTER_SANITIZE_STRING);
}
 
foreach ($_ENV as $key => $value)
{
    $_ENV[$key] = filter_input(INPUT_ENV, $key, FILTER_SANITIZE_STRING);
}
 
$_REQUEST = array_merge($_GET, $_POST);

On gists

Selector Caching in jQuery

jQuery

Selector Caching in jQuery.js #

function SelectorCache() {
    var collection = {};
    function getFromCache(selector) {
        if (undefined === collection[selector]) {
            collection[selector] = $(selector);
        }
        return collection[selector];
    }
    return {
        get: getFromCache
    };
}
 
var selectors = new SelectorCache();
 
// Usage $('#element') becomes:
selectors.get('#element');

On gists

jQuery document ready

jQuery

document-ready.js #

// =======================================================================
//             Document Ready Example 1
// =======================================================================
 
$(document).ready(function () {
  // do jQuery stuff when DOM is ready
});
 
// =======================================================================
//             Document Ready Example 2
// =======================================================================
 
$(function() {
  // jQuery code here
  // This is equivalent to example 1 they literally mean the same thing.
});
 
// =======================================================================
//             Document Ready Example 3
// =======================================================================
 
jQuery(document).ready(function ($) {
  // do jQuery stuff when DOM is ready
});
 
// Adding the jQuery can help prevent conflicts with other JS frameworks.
// 
// Why do conflicts happen?
// Conflicts typically happen because many JavaScript Libraries/Frameworks use the same shortcut
// name which is the dollar symbol $. Then if they have the same named functions the browser gets
// confused!
// 
// How do we prevent conflicts?
// Well, to prevent conflicts i recommend aliasing the jQuery namespace (ie by using example 3 above).
// Then when you call $.noConflict() to avoid namespace difficulties (as the $ shortcut is no longer available)
// we are forcing it to wrtie jQuery each time it is required.
 
jQuery.noConflict(); // Reverts '$' variable back to other JS libraries
jQuery(document).ready(function () {
  // do jQuery stuff when DOM is ready with no conflicts
});
 
// =======================================================================
//               Document Ready Example 4
// =======================================================================
 
$(window).load(function () {
  //initialize after images are loaded
});
 
// Sometimes you want to manipulate pictures and with $(document).ready() you won't be able to do that
// if the visitor doesn't have the image already loaded. In which case you need to initialize the
// jQuery alignment function when the image finishes loading.

On gists

jQuery form select helpers

jQuery Helpers-Filters-Plugins jQuery-plugins

jQuery form select helpers.js #

jQuery.fn.containsOption = function (query) {
  var found = false;
 
  this.each(function () {
    if (this.nodeName.toLowerCase() == 'select') {
      for (var i = 0; i < this.options.length; i++) {
        if (query.value) {
          found = (query.value.constructor == RegExp) ? this.options[i].value.match(query.value) : this.options[i].value == query.value;
        } else if (query.text) {
          found = (query.text.constructor == RegExp) ? this.options[i].text.match(query.text) : this.options[i].text == query.text;
        }
        if (found) break;
      }
    } else {
      return this;
    }
  });
 
  return found;
};
 
jQuery.fn.addOption = function (o) {
  var opt = o;
 
  this.each(function () {
    if (this.nodeName.toLowerCase() == 'select') {
      var option = document.createElement('OPTION');
      option.value = opt.value;
      option.text = opt.text;
 
      if (opt.selected) option.selected = opt.selected;
 
      this.options[this.options.length] = option;
    } else return this;
  });
 
  return this;
};
 
jQuery.fn.clearOptions = function () {
  this.each(function () {
    if (this.nodeName.toLowerCase() == 'select') {
      this.options.length = 0;
    }
  });
};
 
jQuery.fn.removeOptionByValue = function (val) {
  this.each(function () {
    if (this.nodeName.toLowerCase() == 'select') {
      for (var i = 0; i < this.options.length; i++) {
        if (this.options[i].value == val) {
          this.options[i] = null;
        }
      }
    } else {
      return this;
    }
  });
  return this;
};
 
jQuery.fn.removeOptionByText = function (txt) {
  this.each(function () {
    if (this.nodeName.toLowerCase() == 'select') {
      for (var i = 0; i < this.options.length; i++) {
        if (this.options[i].text == txt) {
          this.options[i] = null;
        }
      }
    } else {
      return this;
    }
  });
  return this;
};
 
jQuery.fn.selectOptionByValue = function (val) {
  this.each(function () {
    if (this.nodeName.toLowerCase() == 'select') {
      for (var i = 0; i < this.options.length; i++) {
        if (this.options[i].value == val) {
          this.options[i].selected = true;
        } else {
          this.options[i].selected = false;
        }
      }
    } else return this;
  });
  return this;
};
 
jQuery.fn.selectOptionByText = function (txt) {
  this.each(function () {
    if (this.nodeName.toLowerCase() == 'select') {
      for (var i = 0; i < this.options.length; i++) {
        if (this.options[i].text == txt) {
          this.options[i].selected = true;
        } else {
          this.options[i].selected = false;
        }
      }
    } else return this;
  });
  return this;
};
 
// USAGE
$('select#languages').containsOption({
  text: 'Text'
});
 
$('select#languages').containsOption({
  value: '19'
});
 
$('select#languages').selectOptionByValue('19');
$('select#languages').selectOptionByText('Apache');
 
$('select#languages').addOption({
  'text': 'rubyonrails',
  'value': '100'
});
 
$('select#languages').removeOptionByValue('19');
$('select#languages').removeOptionByText('Apache');
 
$('select#languages').clearOptions(); // deletes all options

On gists

form in popup

JavaScript jQuery

form-in-poup.js #

$(document).ready(function() {
  $('#myform').submit(function() {
    window.open('', 'formpopup', 'width=400,height=400,resizeable,scrollbars');
    this.target = 'formpopup';
  });
});

On gists

ArrayToObject / ObjectToArray

PHP Helpers-Filters-Plugins

object-to-array-vice-versa.php #

<?php

function objectToArray($d) {
		if (is_object($d)) {
			// Gets the properties of the given object
			// with get_object_vars function
			$d = get_object_vars($d);
		}
 
		if (is_array($d)) {
			/*
			* Return array converted to object
			* Using __FUNCTION__ (Magic constant)
			* for recursive call
			*/
			return array_map(__FUNCTION__, $d);
		}
		else {
			// Return array
			return $d;
		}
	}
 
 
 
 	function arrayToObject($d) {
		if (is_array($d)) {
			/*
			* Return array converted to object
			* Using __FUNCTION__ (Magic constant)
			* for recursive call
			*/
			return (object) array_map(__FUNCTION__, $d);
		}
		else {
			// Return object
			return $d;
		}
	}
 

On gists

JS - remove diacritics fn

JavaScript Helpers-Filters-Plugins

make-url.js #

			function makeUrl(s)
			{

				var nodiac = { 'á': 'a', 'č': 'c', 'ď': 'd', 'é': 'e', 'ě': 'e', 'í': 'i', 'ň': 'n', 'ó': 'o', 'ř': 'r', 'š': 's', 'ť': 't', 'ú': 'u', 'ů': 'u', 'ý': 'y', 'ž': 'z' };
				s = s.toLowerCase();
			    var s2 = '';
			    for (var i=0; i < s.length; i++) {
			        s2 += (typeof nodiac[s.charAt(i)] != 'undefined' ? nodiac[s.charAt(i)] : s.charAt(i));
			    }

			    return s2;
			    //return s2.replace(/[^a-z0-9_]+/g, '-').replace(/^-|-$/g, '');
			}

On gists

Sql - nalezeni duplicitnich radku (více sloupců)

MySql

sql-duplicity-rows.sql #

SELECT DISTINCT t1.id
FROM tabulka t1
JOIN tabulka t2 ON t2.sloupec1 = t1.sloupec1
AND t2.sloupec2 = t1.sloupec2
AND t2.id < t1.id