Integer to Roman numeral

April 21, 2008

Changes and integer to a roman numeral.

	private function numberToRoman($num)
	{
	     $n = intval($num);
	     $result = '';

	     $lookup = array('M' => 1000, 'CM' => 900, 'D' => 500, 'CD' => 400,
	     'C' => 100, 'XC' => 90, 'L' => 50, 'XL' => 40,
	     'X' => 10, 'IX' => 9, 'V' => 5, 'IV' => 4, 'I' => 1);

	      foreach ($lookup as $roman => $value)
	     {
	            $matches = intval($n / $value);
	            $result .= str_repeat($roman, $matches);
	            $n = $n % $value;
	      }

	      return $result;
	}

Originally found here

Optimised AJAX Call

March 29, 2008

A nice way to manage AJAX calls written by Dustin Diaz.

	var asyncRequest = function() {
	  function handleReadyState(o, callback) {
	    if (o && o.readyState == 4 && o.status == 200) {
	      if (callback) {
	        callback(o);
	      }
	    }
	  }
	  var getXHR = function() {
	    var http;
	    try {
	      http = new XMLHttpRequest;
	        getXHR = function() {
	          return new XMLHttpRequest;
	        };
	    }
	    catch(e) {
	      var msxml = [
	        ‘MSXML2.XMLHTTP.3.0′,
	        ‘MSXML2.XMLHTTP’,
	        ‘Microsoft.XMLHTTP’
	      ];
	      for (var i=0, len = msxml.length; i < len; ++i) {
	        try {
	          http = new ActiveXObject(msxml[i]);
	          getXHR = function() {
	            return new ActiveXObject(msxml[i]);
	          };
	          break;
	        }
	        catch(e) {}
	      }
	    }
	    return http;
	  };
	  return function(method, uri, callback, postData) {
	    var http = getXHR();
	    http.open(method, uri, true);
	    handleReadyState(http, callback);
	    http.send(postData || null);
	    return http;
	  };
	}();

Originally found here

Disable the Firebug extension

Useful for pages containing a lot of javascript that might bring Firefox to a grinding halt if it’s not disabled.

	if (! ('console' in window) || !('firebug' in console)) {
	    var names = ['log', 'debug', 'info', 'warn', 'error', 'assert', 'dir', 'dirxml', 'group', 'groupEnd', 'time', 'timeEnd', 'count', 'trace', 'profile', 'profileEnd'];
	    window.console = {};
	    for (var i = 0; i < names.length; ++i) window.console[names[i]] = function() {};
	}

Originally found here