Chris Nizzardini, Salt Lake City Utah, Web Developer Specializing in LAMP+Ajax Since 2006

My Blog

Here is my awesome blog. You can find information on programming, linux, documentation, tips for code and database optimization, my thoughts and rants, and whatever else I feel like sharing. Feel free to contribute to the blog by posting comments and asking questions.

Archive for 2009

SQL_CALC_FOUND_ROWS – Get Total Rows in MySQL Query

Posted by chris on August 13th, 2009 Comments(0)

The MySQL SQL_CALC_FOUND_ROWS function is a nice way to return how many rows were returned in the query. There has been a lot of discussion in the PHP.net entry on mysql_num_rows regarding this function. The debate centers around whether its more effecient to use MySQLs built in functionality or whether its more effecient to run the same query again using the COUNT() function.

For me, its hard to determine which way is better. Usually its better to leverage your database engine than code. There is not an easy way to tell how database cache plays into this either. I feel using SQL_CALC_FOUND_ROWS is the better option, it eliminates a few extra lines of code, and prevents you from having to update multiple queries. Whether there is a performance penalty in either case is debatable, if its even noticeable…

1
SELECT SQL_CALC_FOUND_ROWS * FROM tbl_customer WHERE entry_date > '2009-01-01';

In a separate query run this (I believe this is connection dependent, so they must be run in conjunction with each other within the life of the same connection).

1
SELECT FOUND_ROWS() AS totalRows;
In SQL (, , )

best way to load your JavaScript

Posted by chris on July 30th, 2009 Comments(0)

Interesting article over at Ajaxian on loading your javascript code. I’m not sure this is the best way in all cases, but when you’re coding in a world of frameworks, multiple js files, wysiwyg editors, and framework plugins and widgets it definitely makes sense in many circumstances.

In JavaScript and Ajax ()

adventures in linux printing, set default printer and firefox command line print

Posted by chris on July 23rd, 2009 Comments (2)

We are in the process of redesigning our packing slips that get sent out to customers with each order. The previous developer went with a solution, html2ps, that has shotty support for CSS and design principals in general. The designer and I decided we needed something else. After trying numerous solutions, a co-worker suggested I give FireFox command line print a try.

Read the rest of this entry »

In Linux, Programming (, , , , , , )

firefox 3.5 fails – bug with ajax callback onreadystatechange

Posted by chris on July 17th, 2009 Comments(0)

I first noticed this bug a few days after upgrading to FireFox 3.5. When you fire an XHR request, the callback function is not being executed when the request completes itself. Instead you see an onreadystatechange error in Firebug.

There have been several bugs reports on this:

http://www.nczonline.net/blog/2009/07/09/firefox-35firebug-xmlhttprequest-and-readystatechange-bug/
http://www.ghastlyfop.com/blog/2007/01/onreadystate-changes-in-firefox.html

My advice is don’t upgrade too FireFox 3.5 until they have released a patch for this bug. Its incredibly annoying to keep retrying your XHR request until it finally succeeds knowing your code is just fine. I have downgraded both my systems to FireFox 3.0.11. My bigger concern is how this ever got passed of by QA?

In Programming ()

zend studio 6 fails – going back to zend 5.5

Posted by chris on June 6th, 2009 Comments (3)

Zend, the PHP Company, took a major step back with their PHP IDE when they moved it too the eclipse platform. I’m not a fan of eclipse, I don’t even like using it for Java development – which its built for. Zend Studio 6 is slow, clunky, the user interface is confusing, the FTP component is horrible.

If you can name it, Zend 5.5 beats Zend 6 at it, except for javascript support. However, I only need a JavaScript IDE for when I am doing heavy JavaScript coding or debugging. The worst part about Zend Studio 6 is the FTP component. Every time you save a file – it refreshes the folders and closes them all. So you can to open the folder again and wait everytime you make a save! Finally I decided to update the program hoping they had fixed this bug. When I updated to the latest version I was no longer able to connect to most of the FTP servers I had (roughly 10), one of which was to this domain.

At the point I uninstalled and went back to Zend 5.5 and wrote this article. Flat out, Zend Studio 6 sucks.

In Programming (, )

MySQL WITH ROLLUP for Easy Automatic Grouped Total Columns

Posted by chris on June 4th, 2009 Comments(0)

Using the WITH ROLLUP modifier in queries using GROUP BY will add an additional row to the result set which sums all columns. This prevents you from having you to write code which adds each column in your programming language.

Description from the Mysql Reference Manual

The GROUP BY clause allows a WITH ROLLUP modifier that causes extra rows to be added to the summary output. These rows represent higher-level (or super-aggregate) summary operations. ROLLUP thus allows you to answer questions at multiple levels of analysis with a single query. It can be used, for example, to provide support for OLAP (Online Analytical Processing) operations.

Example

1
2
3
4
5
6
7
8
SELECT 
	DATE(dateTime) AS order_date, COUNT(*) AS shipments, SUM(shipping_total) AS shipping_total, SUM(hasShipAmt) AS hasShipAmt, SUM(shipping_total)-SUM(hasShipAmt) AS revenue  
FROM 
	tbl_my_orders 
WHERE 
	dateTime BETWEEN '$startDate' AND '$endDate 23:59:59' 
GROUP BY 
	order_date WITH ROLLUP
In SQL (, , , )

Star UML, Free Open Source UML Flow Charting Software

Posted by chris on May 27th, 2009 Comments (8)

Star Uml

A few months ago I was rummaging around my office for a copy of MS Visio, my favorite diagramming tool. Unfortunately I had misplaced it and did not feel like buying a new copy or swiping a pirated version. The free alternatives that I knew of at the time were lacking. My general opinion of most open source desktop software is not that high. The open source community has done great with web apps and server side software, but office tools aren’t really there yet.

Dia is aesthetically appalling and is prone to crashing and umlet is cumbersome and not much better looking. Enter Star UML. Star UML might even be better than Visio. Some of my favorite star uml features are the code generator (supports many languages including PHP), the documentation generator (exports to Word), and the ER diagram. Star UML supports UML diagraming with activity diagrams, use-case models, class diagrams, and many more. Additional extensions give you support for ER database diagrams as well.

StarUml Diagram

PHP Code Generator
The Code generator supports many languages by default, but you will have to download the plugin from the star uml website to get PHP support. Once the plugin is installed (done by simply dropping some files in a plugin folder). Click Tools, then StarUML Generator. In the select the checkbox to the left of “PHP 5 Code Generator v1.0″ and then click Next. Follow the remaining steps are your class diagrams will be converted into wonderful PHP 5 classes. The more time you spend following UML standards, the better the generated code will be.

ER Diagram
This is another plugin that can be downloaded from the Star UML project website. This one took some discovery to get working and unfortunately there isn’t a MySQL code generator for this yet. However the tool works pretty well. Once the plugin has been placed in the right directory and is installed (follow the directions on the project website) you can begin using it.

Open a new project. Select the default option. In your Model Explorer to the right you will see the name of your Model, probably called Untitled with a cube to the left of it. Right-click, select Add, then select Model. Name your model something, how about ER Diagram. Now right click ER Diagram and click Add Diagram, then select ER(IE) Diagram. Now to the left you will see some options for adding a table and linking tables through relations.

Lets add a table, name it anything. By default the table will not be what you want. I named my table “tbl_yo” which now appears in the model explorer. Select the table in the model explorer. In the tables properties (located just below the model explorer) there will be a value called Stereotype. Click the button to the right that has three dots. The stereotype you want to select is called table (ERD). Your table should look a bit different now (the table title is located just above the yellow square. Now add a column called yo_id. This can be done by double-clicking the table and clicking the greenish button. In the model explorer you will see yo_id below tbl_yo. Right click yo_id and select Tagged Values. Checking the PK box will make this a primary key and checking the FK box makes it a foreign key. Selecting either of these will change the logo next to yo_id in the ER view.

Drop me a comment if this helped your or if you feel I missed something, thanks for reading.

In Software (, , , , )

filtering dynamic ip addresses from google analytics

Posted by chris on May 4th, 2009 Comments(0)

Nice post over at epikone.com on filtering out dynamic ip addresses from google analytics: http://www.epikone.com/blog/2009/04/16/count-me-out-gajs-version/

In Seo ()

Using the GeoNames Web Service with PHP

Posted by chris on April 14th, 2009 Comments (2)

The GeoNames web service API, http://www.geonames.org/export/web-services.html, can be used to retrieve information on foreign countries such as postal code, city name, country code, longitude, and latitude. This information can come in very handy in variety of circumstances. I used the information obtained from the API to automate updating a table of countries with the postal code (just needed one in this instance) of every capital in the world.

First you will need to use cURL. I have written a simple cURL class:

Read the rest of this entry »

In Programming (, , , )

Dumping PHP Array Data and debug_print_backtrace

Posted by chris on March 17th, 2009 Comments(0)

By default you cannot write a print_r, var_dump, or debug_print_backtrace to a log in PHP. When you call these methods, PHP immediately echo’s out the data. Instead you must use PHPs ob_start which instructs PHP to not output anything from the script. To access the contents of the buffer use ob_get_contents and to let PHP know its okay to output from your script once again call ob_end_clean.

1
2
3
4
	ob_start();
	var_dump(debug_backtrace());
	$backtrace=ob_get_contents();
	ob_end_clean();
In Programming ()