Andrew Fiebert, Programmer and DBA
The blogfolio of
developer Andrew Fiebert

Archive for January, 2008

Breaking news: Multitasking Makes You Stupid and Slow

Monday, January 28th, 2008

Yea thats right, we finally get a good excuse for taking on less work. For the details, read on. By the way this is ripped off verbatim from Slashdot.

“Multitasking messes with the brain in several ways. At the most basic level, the mental balancing acts that it requires — the constant switching and pivoting — energize regions of the brain that specialize in visual processing and physical coordination and simultaneously appear to shortchange some of the higher areas related to memory and learning. We concentrate on the act of concentration at the expense of whatever it is that we’re supposed to be concentrating on… studies find that multitasking boosts the level of stress-related hormones such as cortisol and adrenaline and wears down our systems through biochemical friction, prematurely aging us. In the short term, the confusion, fatigue, and chaos merely hamper our ability to focus and analyze, but in the long term, they may cause it to atrophy.”

Looping though XML cart items in Google Checkout

Monday, January 21st, 2008

There is a Google Group setup to discuss Google’s Checkout API only the group sucks. I have no patience to read though thousands of meaningless posts just to find what I’m looking for and sadly searching Google for help on their API is actually pretty tough.

Basically when you are working on the responsehandler.php file, the file that gets callbacks from Google on all kinds of notifications, it isn’t initially clear how you receive the data and then utilize it. Your variables wind up looking something like $data[$root]['shopping-cart']['merchant-private-data']['VALUE'] where $root is a case value switching between the different types of notifications you can receive, in this case it is new-order-notification.

The problem arises when you try and get all of the items out of the shopping cart. An item name for example is $data[$root]['shopping-cart']['items']['item']['item-name']['VALUE']. Now items occurs once in the hierarchy but each cart item is contained in the <item></item> tags. Simply put, in a DFD items:item would be labeled as 1:N.

What does the code to get this done look like? This:

$items = get_arr_result($data[$root]['shopping-cart']['items']['item']);
foreach ($items as $item) {
$insert_into_order_contents=”
INSERT INTO order_contents SET
`google-id`=”.$data[$root]['google-order-number']['VALUE'].”,
`email`=’”.$data[$root]['shopping-cart']['merchant-private-data']['VALUE'].”‘,
`item-id`=”.$item['merchant-item-id']['VALUE'].”,
`item-name`=’”.$item['item-name']['VALUE'].”‘,
`unit-price`=”.$item['unit-price']['VALUE'].”,
`quantity`=”.$item['quantity']['VALUE'].”,
`owner`=’”.$item['merchant-private-item-data']['VALUE'].”‘
“;
$result3 = mysql_query($insert_into_order_contents,$db);
}

How to convert NULL to zero in MySQL

Monday, January 21st, 2008

Today I was pulling a query from MySQL and some of my columns were NULL. Now this isn’t the first time I’ve gotten NULL as a result and usually my code has an if statement in there to swap out the NULL values with zero. The problem is this time that I don’t have the option to edit the code after the query has been returned, I need the actual query results to have NULL removed from them. This lead to my discovery of COALESCE.

I have a column SUM(`order-total`) which will return the total dollar amount a user has spent on the website only some users have yet to make any purchases.

COALESCE is used in the following way:

COALESCE(SUM(`order-total`),0.00)

This acts just like str_replace in PHP. You can use COALESCE in any part of your query, it doesn’t only have to be where you specify the columns you want returned.