Category Archives: Software

PHP – Generating Unique ID’s

There may be situations where you need to generate a unique string. I have seen many people use the md5() function for this, even though it’s not exactly meant for this purpose:


// generate unique string
echo md5(time() . mt_rand(1,1000000));

There is actually a PHP function named uniqid() that is meant to be used for this.

// generate unique string
echo uniqid();
/* prints
4bd64c947233e
*/

// generate another unique string
echo uniqid();
/* prints
4bd67c9412340
*/

You may notice that even though the strings are unique, they seem similar for the first several characters. This is because the generated string is related to the server time. This actually has a nice side effect, as every new generated id comes later in alphabetical order, so they can be sorted.
Read more »

PHP – Functions with Arbitrary Number of Arguments

You may already know that PHP allows you to define functions with optional arguments. But there is also a method for allowing completely arbitrary number of function arguments.

First, here is an example with just optional arguments:

// function with 2 optional arguments
function foo($arg1 = '', $arg2 = '') {

echo "arg1: $arg1\n";
echo "arg2: $arg2\n";

}
foo('hello','world');
/* prints:
arg1: hello
arg2: world
*/

foo();
/* prints:
arg1:
arg2:
*/

Now, let’s see how we can build a function that accepts any number of arguments. This time we are going to utilize func_get_args():

Read more »

JQuery – Accessing Native Properties and Methods

So you’ve learned a bit of JavaScript, and have learned that, for instance, on anchor tags, you can access attribute values directly:

var anchor = document.getElementById('someAnchor');
//anchor.id
// anchor.href
// anchor.title
// .etc

The only problem is that this doesn’t seem to work when you reference the DOM elements with jQuery, right? Well of course not.


// Fails
var id = $('#someAnchor').id;

So, should you need to access the href attribute (or any other native property or method for that matter), you have a handful of options.

// OPTION 1 - Use jQuery
var id = $('#someAnchor').attr('id');

// OPTION 2 - Access the DOM element
var id = $('#someAnchor')[0].id;

// OPTION 3 - Use jQuery's get method
var id = $('#someAnchor').get(0).id;

// OPTION 3b - Don't pass an index to get
anchorsArray = $('.someAnchors').get();
var thirdId = anchorsArray[2].id;

JQuery – Detect AJAX Requests with PHP

Certainly, for the huge majority of our projects, we can’t only rely on JavaScript for things like validation, or AJAX requests. What happens when JavaScript is turned off? For this very reason, a common technique is to detect whether an AJAX request has been made with your server-side language of choice.

jQuery makes this ridiculously simple, by setting a header from within the $.ajax method.

 // Set header so the called script knows that it's an XMLHttpRequest
 // Only send the header if it's not a remote XHR
 if ( !remote ) {
 xhr.setRequestHeader("X-Requested-With", "XMLHttpRequest");
 }
 

With this header set, we can now use PHP (or any other language) to check for this header, and proceed accordingly. For this, we check the value of $_SERVER['HTTP_X_REQUESTED_WITH'].

YUI – Define anonymous function

Defining an anonymous function in order to keep all variables out of the global scope. Inside the anonymous function, define some shortcuts to utils that will be used frequently (Dom and Event).


(function () {
 var Event = YAHOO.util.Event,
 Dom = YAHOO.util.Dom;
}());

Inside the the anonymous function, use the onDOMReady method of the Event utility to instantiate an Overlay and a Button when the page’s DOM is ready to be scripted.


Event.onDOMReady(function () {
 var oCalendarMenu;

// Create an Overlay instance to house the Calendar instance
 oCalendarMenu = new YAHOO.widget.Overlay("calendarmenu", { visible: false });

// Create a Button instance of type "menu"

var oButton = new YAHOO.widget.Button({
 type: "menu",
 id: "calendarpicker",
 label: "Choose A Date",
 menu: oCalendarMenu,
 container: "datefields" });
});