The One Character Block Comment

When debugging, I often find that I have to comment and un-comment a block of code several times during the process of trying to find out what’s going on. That used to mean typing and deleting comment block characters repetitively, but not anymore. Here’s a simple solution to that problem: Comment or un-comment an entire code block of code by typing or deleting a single character.

I was able to arrive at this solution by combining the one-line comment with the comment block in a way that takes advantage of the rules the different types of comments have to follow.

Continue reading

How Prepared Statements Work, and Why You Should Use Them

Prepared statements differ from normal queries in one major way: Instead of sending one SQL string with the values defined by single quotes in one package, the SQL string and values are sent in two separate calls. Prepared statements themselves are like query blueprints, with placeholders where the values will go. The query values are sent in a separate call with a reference to the prepared statement, and are dropped in place and executed.

This post approaches the problem and presents a solution from a PHP angle, but even if you’re not a PHP developer, you should be able to follow along with some creative substitution.

The Old Way

Prepared statements take longer because they are 2 round-trips to the database for a single query. As soon as you prepare a query, it’s sent to the database with the placeholders you set. So the database engine takes that prepared statement and maps out the query and optimizes it for execution. Then when you call execute() only the values you give are sent to the database, with a reference to that query you just prepared. The database engine drops in the values and runs the query. This is totally immune to SQL injection, because the database engine already knows exactly where the values begin and end (the placeholder marker(s) you set), and therefore never need escaping. The reason SQL injection exists in the first place is because the entire query is interpreted upon execution, values and all. So if anything interferes with the quotes surrounding your values, the engine thinks that value has ended and thus a security hold is introduced. That problem is avoided all together with prepared statements by letting the database engine know ahead of time exactly where to put each value you pass to it later on. There is no need for escaping and there is no need to worry.

Why MVC is so Popular on the Web

The MVC design pattern has been getting a lot of attention in the past few years.  It seems like a new MVC framework pops up every week, and discussions about MVC have become commonplace throughout web programming communities.###MVC is Here to Stay

While MVC is certainly the "new thing" again (funny, because it actually dates back to 1979), it won’t be a passing fad, and it won’t fade quickly – at least not on the web.

Solving the Fundamental Problem of Web Development

Unlike most dektop applications which can be completely coded with one or two different programming languages, websites are a mix of several different programming languages that are constantly changing.  The typical website or web application in composed of no less than 6 different programming languages:

  • Server-side language like PHP/Ruby/Python/.NET, plus:
  • SQL – (Variants: MySQL, MSSQL, PgSQL, SQLite)
  • HTML/XHTML
  • CSS
  • JavaScript
  • XML (Plus specific formats like RSS and Atom and possibly XSLT)
  • JSON if the web application has an API
  • And who knows what else a few years from now…

As a result, the code naturally has to be separated somehow, and the same content has to be able to be displayed in many different formats (most commonly HTML plus XML and JSON for APIs).

OKC PHP User Group Reboot

The local Oklahoma City PHP User Group is re-starting with the okcCoCo as the new venue. The new meetings will be on the second Tuesday of each month, starting with Tuesday, June 09, 2009 at 6:30pm as the first official meeting. Visit the official OKC PHP User Group website to register for meeting reminders and to connect with other local PHP developers.

I will be presenting my talk on Building a Data Mapper with PHP5 and the Standard PHP Library , followed by a discussion on ORMs and whatever else comes up. The presentation will cover all the thought processes, goals, theories, and actual code that goes into building an ORM (or really any other larger project that requires more advance planning). The project that was the basis of this presentation is phpDataMapper – an open-source PHP5 data mapper ORM layer that I started in the fall of 2008. It now powers the model layer of InvoiceMore, a live web application I launched in March 2009.

This is a presentation I have given before at Tulsa TechFest.

Watch the video of this presentation online

jQuery UI Datepicker with AJAX and LiveQuery

I’ve been a little aggravated lately trying to get jQuery UI Datepicker to work correctly on dynamically added fields for creating additional line items to invoices for InvoiceMore. It works great for fields already displayed on the page, but it tends to have major issues with dynamically added fields through AJAX or AHAH. Of course it won’t work out of the box with elements added dynamically to the DOM, so we can use jQuery’s $.live() event (new in 1.3 – you previously had to use liveQuery) to make it work. The Datepicker works by binding to the focus() event by default, but as of jQuery 1.3.2, the ‘focus’ event cannot be monitored by the ‘live’ event function. So we’re stuck with a little work around:

You would think just a simple "$(this).datepicker()" call wrapped inside the live() event would work, but it doesn’t. Turns out that in order to get it working consistently, you have to add the ‘showOn: focus’ config option as well as manually focusing on the element with the focus() event. Charming.

InvoiceMore Launch and Differentiation

invoicemore

InvoiceMore, the startup I have been working on in my spare time for over 7 months, has finally launched. This post actually comes a bit late to the party, because I actually launched InvoiceMore at OpenBeta on March 12, 2009 and blogged about it on the Actridge blog that day. I haven’t even had time to thinkabout sitting down to write this post on my personal blog about the launch until now. That’s a testament to how crazy busy my life has been since I decided to pour all my spare time into starting a business. So what is InvoiceMore, and how is it different?

moreInvoiceMore is an online billing and invoicing application aimed at freelancers and small businesses. It basically provides a super-simple web interface for creating and sending invoices to clients and recording payments for them. You can email and generate PDF invoices, print and snail mail them, and just keep track of your clients and their payments in a really easy and intuitive way. It was created based on my experience from a different billing application I created to fill my own client billing needs for freelance and contract work.

Differentiation

A lot of people ask me why I made InvoiceMore, and how it will be any different from what’s already out there on the market. If you’ve ever used an online billing application, or currently are using one, InvoiceMore works much the same way, with one major exception: Recurring billing. All of the online web-based billing applications I have come across so far do recurring billing the same way: a “recurring invoice template” that has a recurring interval set on it, like “1 month” or “2 weeks”. The problem is, if you have a client with multiple recurring services at different intervals, you have to setup multiple recurring invoice templates, and your client ends up getting more than one invoice per month at least a few months of the year.

Clients don’t ask for recurring Invoices. They ask for recurring products and services. An invoice is the natural end result of the products and services they buy. Competing billing applications make you create and setup what should be the end result: the Invoice. So to solve this problem, I built recurring billing in InvoiceMore in what I believe is a much more natural way: the products and services themselves. So with InvoiceMore, you associate products and services with clients and pick a recurring interval for that association. Then every billing cycle, invoices are automatically generated for that client from the recurring products and services that are due sometime within that billing period. You end up with a single invoice with everything due on it instead of multiple “recurring invoice templates” that are generated and sent independently.

So if you’re interested in learning more, you can try InvoiceMore out for free, or just read the information on the website . Let me know what you think in the comments here, or on the official UserVoice page for feedback and ideas.

Mirror Remote Server With Linux FTP Commands

A client of mine called me last night around 8:00pm a little worried. I had recently setup a hosting account for her on my server, so that she would be able to switch from her current FTP-only solution to a full hosting account with a domain and everything for when she makes a webstie in the future (she only needs the FTP to share files for now). On the phone, she said:>The guy who hosts my files just called me. He got in a disagreement with the guy who manages his servers, and told me to back everything up because it might not be there tomorrow

Wow. In a split second, all your data can be gone. The forever kind of gone. The problem was – and the reason she called me – was that she had amassed so many files over the years, that it would take days to backup using her internet connection, and she only had hours to get it done. Okay, relax, I told her – I’ve got it taken care of. I can use linux shell commands to download all the files to my server from yours. It will be much faster, and the files will go directly to the new server instead of having to be re-uploaded there, saving some very time-consuming steps.

Okay, I thought. I’ll just login, make a big tarball of all the files and grab that with my server. But her file hosting account did not allow shell access, and probably didn’t have the extra space for an additional tarball of all the files anyway. So I’m stuck with the linux ftp commands – or so I thought. Turns out, the mget ftp command does not recursively download folders on most servers. So the best function to use on a remote linux server that you can’t run shell commands on is wget, because wget also supports the FTP protocol. The usage goes like this:

wget -r ftp://user:pass@domain.com

That was going fine, and then the connection was cut-off by the remote server a short way through getting all the files, probably due to some data transfer cap or something. I re-started it, and it cut off again near the same place. So this isn’t enough either, and I still don’t want to do it manually. Luckily, there is a wget flag to ignore already existing files – ‘-nc’. So the whole command to download everything recursively and not re-download files you already have is:

wget -nc -r ftp://user:pass@domain.com

Remember to back up often. You never know when you might find yourself in a sudden and unexpected data loss situation, like Ma.Gnolia did Friday, January 30th. There’s a good discussion happening on the SitePoint open thread on data loss that same some good backup ideas and methods, too.

Interested in Coworking in Oklahoma?

okccoco_logoIf you’re in or around the Oklahoma City area doing freelance work, there’s some very good news for you.  Derrick Parkhurst along with a few members of the local community are opening OKCCoCo – Oklahoma City Coworking Collaborative.  What is Coworking? Coworking is essentially a movement to create a working office environment for freelancers and independent workers that they miss out on working from home.  Individuals will be able to rent a desk or private office to work at every month with a bunch or other really cool and smart people instead of working from home or signing a long-term lease for a real office and still being by themselves.  If you’re a freelancer or an independent worker that is interested in coworking, you should plan on attending the OPEN HOUSE to view the space on Saturday, January 17th at 2pm.

Details and registration are on the OKCCoCo website.

There will be a few additional announcements and activities at the open house, including a speech from Noah Everett, the founder of TwitPic. My own startup InvoiceMore will have a special announcement as well and a bonus for those who sign-up for OKCCoCo with a full or part-time commitment. Come out if you can, and don’t forget to register first! These are exciting times for the Oklahoma technology community!

PHP array_merge – Preserving Numeric Keys

This is just a quick post on PHP’s default behavior of re-indexing numeric keys when using PHP’s internal array_merge and array_merge_recursive functions, because it’s a problem I recently ran into, and was unable to find a quick solution to online.

Basically, the problem is that if you’re using numerically-indexed arrays with a set number that you don’t want to change (like an ID or some other unique identifier), you can’t use array_merge, because it automatically re-indexes all the numeric keys in the array to start with 0 on down in order.  There is no flag or option for the function to NOT do this, but there is another way to achieve the same result using PHP’s little-documented overridden plus operator ‘+’ for appending an array to another array.

So just replace this:

$destinationArray = array_merge($array1, $array2);

With this instead:

$destinationArray = $array1 + $array2;

Both $array1 and $array2 MUST be arrays or a fatal error will be thrown, so you may want to do some type checking or casting before that line of code. The difference is that instead of merging the arrays together, the second array will simply be appended to the first one with no changes.

Note that the plus operator for arrays ‘+’ is only one-dimensional, and is only suitable for simple arrays. If you need a multi-dimensional or complex solution, Keith Devens has a custom merge function that might work for you.

Get the Most From Your Google Analytics Dashboard

main_logoI’m a big fan of Google Analytics.  The service is free, can go on multiple websites using just one account, and displays and processes stats beautifully. But the one thing that’s always annoyed me about Google Analytics is the default dashboard setup when you create a new website profile.

The dashboard is the place for the most important things to be. it should be the single place you can view to and get an overview of all the most important things about your website regarding your visitors without having to drill deeper or go through multiple pages or sub-sections.  But the default dashboard Analytics starts you off with is all wrong, and is almost never the information I really want to see.  Let’s see how we can fix this. Continue reading