Skip to main content

Posts

Showing posts from 2011

Query Pivot in MySQL

Basically this blog post is about dynamic columns in MySQL queries.  Since you can't run "pivot" queries in MySQL, how do you write queries that return an arbitrary number of columns (perhaps based on the number of rows in another table)? The answer is simple: you can't... at least not in a single query.  The best way to do this is to run two queries and generate the SQL for the 2nd query based on the results of the 1st query. Imagine this table of product sales: ProductID CustomerID Unit Sales 1 1 20 1 2 50 2 1 20 2 2 70 2 3 90 Maybe you want your result set to look like this... ProductID Customer 1 Customer 2 Customer 3 1 20 50 0 2 20 70 90 Write this query first: SELECT CustomerID, CustomerName FROM `customers` Store the results into $rows.  For each $row, append this string to $fields: ", SUM(CASE WHEN CustomerID=$row[0] THEN `Unit Sales` ELSE 0 END) AS $row[1]" Then... write this query: "SELECT ProductID

IE Sucks

I am so frustrated right now that I am going to dedicate an entire blog post to how crappy Internet Explorer is. With all due respect, Microsoft, IE is terrible. Thank you for your understanding. Sincerely, The Management

Why Node.JS?

This article is for those of us who have heard all of the buzz about Node.JS and they need to convince their boss or their clients to make the switch.  Or, perhaps... you're not quite convinced yet? Why Node.JS? Reason #1 - JavaScript is awesome.  So, in case you didn't know, JavaScript is awesome.  I'll let you read my other blog post  for more details, but I'll also summarize for you: The language itself is extremely powerful.  JavaScript comes packed with modern programming language features like closures, dynamic data types, first-class and higher-order functions, prototypes (not classes), lack of concurrency  (more on this later), exception handling, garbage collection, and much, much more!  These features help make your code better, keep you from making mistakes, and make your applications more bulletproof. JavaScript is widely adopted.  It's the scripting language of the web.  Need I say more?  Almost any device with a web browser can run JavaScript c

Lesson Learned: JavaScript For-In loops

For-In loops in JavaScript are really  nice... until they aren't. Not knowing exactly  how they work might bite you.  I'll start with an example: var arr1 = ["a", "b", "c", "d"]; for(var i in arr1)     console.log(i, arr1[i], i+1, arr1[i+1]); Seems simple enough.  What will be printed? 0 a 01 undefined 1 b 11 undefined 2 c 21 undefined 3 d 31 undefined  Whoops... I am an idiot!  That's not exactly what I wanted.  Note that i is a string , not a number .  I wasted an hour of my life trying to figure out why i+1 was not working properly... only to come to this horribly obvious conclusion.  It was totally worth a blog post... and a beer. Sometimes the old-fashioned for loops are better for Arrays: var arr1 = ["a", "b", "c", "d"]; for(var i = 0; i < arr1.length; i++)     console.log(i, arr1[i], i+1, arr1[i+1]);

Express (JS, of course)

Q: What's ExpressJS? A: quite possibly the coolest thing to happen to Node.JS... EVAR!  This is awesome news for web developers and programmers. OK, you're interested now.  So, if you've followed my blog recently, you know that JavaScript is pure awesomeness.  You also know that server-side JavaScript is the way of the future.  Node.JS is the server-side JavaScript environment.  ExpressJS is the library. Q: So, what do you currently do to write web applications and web services? A: I write PHP code that runs on an Apache web server (or Lighttpd-FastCGI if you prefer).  The PHP code does cool stuff like session handling, parsing URL-encoded POST responses, talking to a database (probably MySQL), generating HTML, etc.  I use HTML, CSS, and JavaScript for the client-side stuff. Q: What do you REALLY want to do? A: Uhhh.... write awesome JavaScript code??? YES!  That's exactly right! You don't need PHP anymore! (Wha???)  You don't even need Apache/Light

JavaScript Closures

What are closures in JavaScript? Taken from  http://jibbering.com/faq/notes/closures/ : The simple explanation of a Closure is that ECMAScript allows inner functions; function definitions and function expressions that are inside the function bodes of other functions. And that those inner functions are allowed access to all of the local variables, parameters and declared inner functions within their outer function(s). A closure is formed when one of those inner functions is made accessible outside of the function in which it was contained, so that it may be executed after the outer function has returned. At which point it still has access to the local variables, parameters and inner function declarations of its outer function. Those local variables, parameter and function declarations (initially) have the values that they had when the outer function returned and may be interacted with by the inner function. For Example: var x; function foo() {     var i = 2;     function bar()

JavaScript Rocks

Here's a bold statement... (so I made the font bold) JavaScript is the best programming language Here's why... The language is extremely powerful .  Let's imagine that we are inventing a new programming language.  The most important thing we will focus on is "power."  That means... write more elegant , more flexible , more readable programs... and write them  even faster . Closures.  A completely awesome, overlooked feature of JavaScript that we all (hopefully) take for granted.  Closures allow you to create a new instance of a function which retains access to the context in which it was created.  Examples are here:  http://en.wikipedia.org/wiki/Closure_(computer_science) . Dynamic data types.  JavaScript variables aren't restricted to any particular datatype.  Store a String, store a number, or store an Object.  JavaScript automatically casts datatypes back and forth as needed. Objects are basically associative arrays, and vice-versa.  Everyone lov

Top 25 Explanations by Programmers when their programs don't work

I found this list absolutely hilarious.  I've starred the ones that I use the most.  :) Top 25 Explanations by Programmers when their programs don't work: 1. Strange...  **** 2. I've never heard about that. 3. It did work yesterday. ** 4. Well, the program needs some fixing. 5. How is this possible? ****** 6. The machine seems to be broken. 7. Has the operating system been updated? 8. The user has made an error again. **** 9. There is something wrong in your test data. 10. I have not touched that module! 11. Yes yes, it will be ready in time. ** 12. You must have the wrong executable. 13. Oh, it's just a feature. ** 14. I'm almost ready. ** 15. Of course, I just have to do these small fixes. **** 16. It will be done in no time at all. *** 17. It's just some unlucky coincidence. 18. I can't test everything! 19. THIS can't do THAT. 20. Didn't I fix it already? ****** 21. It's already there, but it has not been tested. 22. I