Skip to content

Instantly share code, notes, and snippets.

View nadavrt's full-sized avatar

Nadav Rotchild nadavrt

View GitHub Profile
@nadavrt
nadavrt / ajax.js
Created February 18, 2020 01:03
Ajax Without Jquery
/**
* Performs an XMLHttpRequest. There are several ways in which to pass the data, each producing it's own key/value output on the server.
* Using urlencoded (the default) or JSON will send the all the data in key/value pairs. However, sending the data using form-data will cause
* all the data, aside from files, to be send as one parsable string inside the "data" key.
* @param Object args The arguments for the Ajax call. The args object can contain the following arguments:
* String method required The method to use (mainly GET/POST/UPDATE/DELETE).
* String url required The URL to send the HTTP request to.
* String type Optional The content type to send the request as. Possible values are:
* - application/x-www-form-urlencoded (default)
* - application/json
@nadavrt
nadavrt / oop_pdo_crud.php
Last active September 30, 2018 08:39
Procedural and Object Oriented PDO CRUD Examples
<?php
class PDOHandler{
private $serverName;
private $dbName;
private $username;
private $password;
private $pdo;
private $stmt;
public function __construct($serverName, $dbName, $username, $password)
@nadavrt
nadavrt / mergeObjects.js
Last active October 24, 2018 02:38
A one-line JavaScript function that merges two objects.
/**
* Merge two objects. Warning: this function permanently changes obj1 due to it being passed by reference.
* If a property/method exists in both objects obj2's will overwrite the value that existed in obj1.
* @param obj1 The object to merge into.
* @param obj2 An object with properties to add to obj1.
* @return NULL
**/
function mergeObjects(obj1, obj2)
{
Object.keys(obj2).forEach(function(key) { obj1[key] = obj2[key]; });
@nadavrt
nadavrt / decapitalize.js
Last active June 15, 2018 06:50
A one-line JavaScript function that changes the first letter of a string to lower case, leaving the rest of the string untouched.
function decapitalize(str)
{
return str.charAt(0).toLowerCase() + str.substr(1);
}
@nadavrt
nadavrt / foreach.js
Created December 11, 2017 08:41
Native JavaScript foreach function for objects
//The function
Object.prototype.foreach = function(callback)
{
for (var key in this) {
if (this.hasOwnProperty(key)) {
callback(key, this[key]);
}
}
};
@nadavrt
nadavrt / is_mobile.php
Created September 22, 2017 09:27
Check whether the user is using a mobile device (PHP).
<?php
/**
* Check whether the user is using a mobile device.
* @param NULL
* @return Boolean $is_mobile True if this is a mobile device. Otherwise FALSE.
**/
function is_mobile()
{
if ( empty($_SERVER['HTTP_USER_AGENT']) ) $isMobile = false;
elseif ( strpos($_SERVER['HTTP_USER_AGENT'], 'Mobile') !== false // many mobile devices (all iPhone, iPad, etc.)
@nadavrt
nadavrt / data_dump.php
Last active September 14, 2017 05:33
A function that dumps a variable's data. Can show pretty arrays and objects when combined with the Chrome extension PHPView.
<?php
/**
* Dump Data function. Used for debugging.
* @param Mixed $data
* @param Boolean $pretty Set to TRUE to show a prettified version of the output. Only works in Chrome with the PHPView extension.
* @return NULL
**/
function dd($data, $pretty = FALSE)
{
if ($pretty) print_r($data);
@nadavrt
nadavrt / xml_to_array.php
Last active August 31, 2017 06:30
A PHP function to natively create an array from an XML object
<?php
/**
* Natively create an array from an XML object
* @param obj $xmlObject The XML object to process.
* @param arr $out The generated array. This parameter is used nodes back to the function recursively.
* @return arr $out The XML object as an array.
**/
function xml_to_array ( $xmlObject, $out = array () ){
foreach ( (array) $xmlObject as $index => $node )
$out[$index] = ( is_object ( $node ) ) ? xml_to_array ( $node ) : $node;