jump to navigation

sorting a multidimensional array in php February 28, 2008

Posted by chris in : php , add a comment

This will sort a multidimensional array such as this:

Array
(
    [0] => Array
        (
            [customer_ID] => 1000
            [firstName] => Chris
            [lastName] => N
            [email] =>
            [phone] => 111-111-1111
            [joined] => 2007-07-12 12:52:38
        )

    [1] => Array
        (
            [customer_ID] => 1001
            [firstName] => wendy
            [lastName] => p
            [email] =>
            [phone] => 111-111-1111
            [joined] => 2007-07-11 09:21:51
        )

    [2] => Array
        (
            [customer_ID] => 1003
            [firstName] => Lance
            [lastName] => B
            [email] =>
            [phone] => 111-111-1111
            [joined] => 2007-07-11 09:21:51
        )
)

sortBy function

	function sortBy($x, $y)
	{
		$reverse = $this->direction;
		$sort = $this->orderby;
		if($reverse=='true')
		{
			$numberToTimeBy = -1;
		}
		else
		{
			$numberToTimeBy = 1;
		}
		if(isset($sort))
		{
			if ( $x[$sort] == $y[$sort] )return 0;
			else if ( $x[$sort] < $y[$sort] ) return -1*$numberToTimeBy;
			else return 1*$numberToTimeBy;
		}
	}

Honestly, I don't completely understand how this works. A friend at work sent me this function and I modified it a bit. Basically you need to set the $reverse variable to either true or false. This toggles the sorting from ascending to descending and vice-versa. Next set the $sort variable to the array index you want to sort by. The function gets called like this:

usort($this->dataArr,array('NameOfClass','sortBy'));

So $this->dataArr is the array you want to sort, NameOfClass is the name of the class you're sorting in, and sortBy is the name of method within this class to call. Pretty interesting stuff huh.

Now if we were to use usort to sort the example array I gave in this post by first name we would do it like this:

$this->direction = 'false';
$this->orderby = 'firstName';
usort($this->dataArr,array('MyClass','sortBy'));

The following output would be given:

Array
(
    [0] => Array
        (
            [customer_ID] => 1000
            [firstName] => Chris
            [lastName] => N
            [email] =>
            [phone] => 111-111-1111
            [joined] => 2007-07-12 12:52:38
        )

    [1] => Array
        (
            [customer_ID] => 1003
            [firstName] => Lance
            [lastName] => B
            [email] =>
            [phone] => 111-111-1111
            [joined] => 2007-07-11 09:21:51
        )

    [2] => Array
        (
            [customer_ID] => 1001
            [firstName] => wendy
            [lastName] => p
            [email] =>
            [phone] => 111-111-1111
            [joined] => 2007-07-11 09:21:51
        )
)

I've been building a datagrid class and this has been a big help in sorting by column etc...

making a scrolling div February 25, 2008

Posted by chris in : ajax/dom/javascript , add a comment

Needed to make a div element scrollable with no browser scroll bars in it and was unable to find any code. So I quickly threw together a proof of concept before developing for the client. Yes the initial “thing” I created is rather hideous, but the code is easy to modify and make pretty. View source to see the javascript code.

click here to view

appending and creating new elements in javascript February 25, 2008

Posted by chris in : ajax/dom/javascript , 2comments

Recently I discovered that using innerHTML can cause problems when you are heavily modifying the DOM via javascript. A co-worker was nice enough to introduce me to the createElement and appendChild methods.

Creating a drop down

var select = document.createElement('select');
select.id='student';
select.name='student';
select.className='fieldGray';
select.style.fontSize='10px';

Creating an input box

var input = document.createElement('input');
input.id='tip_amount';
input.name='tip_amount';
input.className='fieldGray';
input.style.width='30px';
input.onblur=getTip;

Now to append these elements to the DOM

var div = document.getElementById('destinationDiv');
div.appendChild(select);
div.appendChild(input);

Resources
http://developer.mozilla.org/en/docs/DOM:document.createElement
http://developer.mozilla.org/en/docs/DOM:element.appendChild