SiteCrafting Blah Blah Blog

May. 14, 2008 at 4:15pm

Dumping Duplicates

Deleting all semi-matching rows in MySQL

One of the more recent additions to the SiteCrafting CMS arsenal is a comprehensive error logger, tracking all PHP and MySQL errors (by default... other error types can be created on a case by case basis) that occur in new sites we build. Errors are stored in our own intranet system with a timestamp, error body and a site ID (assigned to each client at a different stage of our project workflow). The table looks something like this:

+----+---------------------+---------------+---------+
| id | logTime             | text          | project |
+----+---------------------+---------------+---------+
|  2 | 2008-05-14 14:42:15 | A PHP Error   |       1 |
|  3 | 2008-05-14 14:42:26 | A PHP Error   |       1 |
|  4 | 2008-05-14 14:42:34 | A PHP Error   |       1 |
|  5 | 2008-05-14 14:42:47 | A MySQL Error |       1 |
|  6 | 2008-05-14 14:42:56 | A MySQL Error |       1 |
|  7 | 2008-05-14 14:43:05 | A PHP Error   |       2 |
|  8 | 2008-05-14 14:43:10 | A PHP Error   |       2 |
|  9 | 2008-05-14 14:43:21 | A MySQL Error |       2 |
+----+---------------------+---------------+---------+
8 rows in set (0.00 sec)

Obviously sometimes we get duplicate errors coming through. Aside from being mere mortals who aren't always fast enough to correct an error before it recurs, one of the first stages of debugging is to try and replicate the error. These are often pretty easy to manage. It's pretty trivial to search for all matching errors, check them all, and delete them. Sometimes, though, this just doesn't cut it. Like when there are a few different errors with 10,000 occurrences apiece.

Read more

Comments (0) | Posted in Coding Techniques, MySQL by Joe Izenman


Feb. 14, 2008 at 4:29pm

Touch the Vote

Rock it as well, if you must

This time of year you either get hooked or get sick of everything politics. That's right, it's a Presidential election season and the 24-hour news cable channels are firing out tons of info both via your TV and online. Some worthy strides in web development practices have also come about on one news site in particular that may help political junkies get their fix anytime, anywhere.

Read more

Comments (0) | Posted in Coding Techniques, Javascript by Kevin Freitas


Jan. 29, 2008 at 3:15pm

PHP Patterns, Part III

The TO Pattern

The Template Object (or TO) is a design pattern of my own that I developed to fill the role of the View layer in the MVC model. As you have probably figured out, the purpose of the TO is to handle everything related to the user interface. The idea here is to separate the interface as much as possible from the rest of the application, so that we could do a complete rewrite of an application without ever touching (or accidentally "breaking") the view portion. Read more

Comments (0) | Posted in Coding Techniques, From the Workbench, PHP, Software Engineering by Nick Williams


Jan. 4, 2008 at 3:21pm

Anonymous Recursion with JavaScript

A new way of solving an old problem

A nice feature of JavaScript is that functions are objects. As such, they do not need a name if they are used right away. But what if you need a short anonymous recursive function? arguments.callee becomes your new best friend.

Read more

Comments (0) | Posted in Coding Techniques, Javascript by Paul Sayre


Nov. 14, 2007 at 3:48pm

PHP Patterns, Part II

The DAO and VO Patterns

In this installment, we will be looking at two patterns that have been 'borrrowed' from Java. If you've had any development experience with J2EE, you are probably well aware of how handy Data Access Objects and Value Objects can be. If you haven't, don't fret! This article was written especially for you!

If you've never heard these terms before, you may be wondering why I have chosen to group them together within one article. The simple explanation is ... well you'll see. For now just accept that they go hand-in-hand, much like salt and pepper or peanut butter and jelly or <insert clever combination here>.

Excited? Let's dig deeper...

Read more

Comments (4) | Posted in Coding Techniques, From the Workbench, MySQL, PHP, Software Engineering by Nick Williams


Sep. 27, 2007 at 3:17pm

PHP Patterns, Part I

The Registry Pattern

This is the first in a planned series of articles discussing the application of various PHP patterns. If you're still getting your feet wet developing in PHP (or programming languages in general), you may be wondering what a "pattern" is in the first place. The best place to find a quick explanation is over in Wikipedia's article (which does a much better job than I could hope to do within the scope of this article).

Now, let's move on to the fun part . . .



Read more

Comments (7) | Posted in Coding Techniques, PHP, Software Engineering by Nick Williams


Aug. 9, 2007 at 4:14pm

Getting the Client’s IP Address

There are a number of reasons why you may want to capture a web user's IP address. You may want to only allow certain IP addresses to view your website, to allow only one vote per IP address, track unique clicks on links and buttons, a location of web users on Google Maps using MASHUP, and whatever else you can think of.

Below are a couple of examples to get the client's IP address in PHP and ASP.net, along with a couple of things to watch for.

Read more

Comments (0) | Posted in ASP.NET 2.0, Coding Techniques, From the Workbench, PHP by Ken Foubert


Jul. 23, 2007 at 5:29pm

ASP.net 2.0 Gridview vs. Custom Coding

A balance of needs and time

I recently decided to try the Gridview control in asp.net 2.0. I read some documentation and decided that it would be very easy to implement. I started by following a step-by-step tutorial on creating a table using the Gridview control, which uses the SqlDataSource control. This step-by-step tutorial included details on how to add paging, column sorting, updating a record, and deleting a record. The tutorial can be found here.

I was actually excited when I got this to work and how easy it was to do. It would only take a few minutes to create a simple, paged, sortable table where a user can delete and update records right on the page. Please note that simple is the operative word here.
Read more

Comments (2) | Posted in ASP.NET 2.0, Coding Techniques, From the Workbench by Ken Foubert


Jun. 4, 2007 at 11:31am

ASP.NET 2.0 Tips - Shared Functions

Visual Basic for ASP.NET 2.0

When developing a recent project for ASP.NET, there was a need to migrate a large number of generic functions that were created in PHP into the .net web project. For .net 1.0, you were able to add a code page that could be used for creating a bunch of functions. However, in .net 2.0, you still had the ability to add a code page, but it had to be a class. This meant placing all of our migrated functions as methods inside a class. To use a generic function, such as generateNewPassword(), you would have to first create a new object for the class and then call the method. Read more

Comments (0) | Posted in ASP.NET 2.0, Coding Techniques, From the Workbench by Ken Foubert


Apr. 18, 2007 at 1:08pm

PHP PEAR and include_path

When attempting to use the SMTP library from PEAR for a project, I was having problems with including the file. I kept getting a file stream error for this code:

require 'Net/SMTP.php';

I figured the problem had to be related to the include_path setting in the configuration file.

Read more

Comments (0) | Posted in Coding Techniques, From the Workbench, PHP by Ken Foubert


Mar. 12, 2007 at 10:27am

Cleaner forms through collapsible textareas

More form, less space

Some really large forms can be a bit of an eyesore especially to the people filling them out. One thing that always bothered me was how bulbous most textareas had to be just to make enough room for a user to feel they could type what they needed. So I set out to figure a good way to clean up my forms a bit while still allowing the multi-line input of textareas.

Read more

Comments (3) | Posted in Coding Techniques, CSS, XHTML by Kevin Freitas


Feb. 28, 2007 at 1:25pm

PHP Strings - Gotcha!

Yesterday I was working with a script that saves an uploaded image to a database. If you've ever seen a binary file as a string, there's a ton of gobbelty-gook that is unreadable to people. For some very odd and inexplicible reason, my script only saved the first four character's worth of data to the database, and that's not very helpful. I did some digging, and revealed a weird gotcha in the way PHP treats strings.
Read more

Comments (0) | Posted in Coding Techniques, PHP by Dave Poole


Feb. 2, 2007 at 10:53am

Query conservationist

The fewer the queries the better

I always enjoy trying to do my work creating web applications using the fewest number of database queries possible. It's kind of a pride thing, I guess. That and I suppose it makes to improve an app's performance and reduce the load on a server. If you're into that kind of thing. ;)

A technique I use subtitutes the temptation of using queries whose results call queries and instead uses only two.

Read more

Comments (3) | Posted in Coding Techniques, MySQL, PHP by Kevin Freitas


Jan. 19, 2007 at 4:52pm

Emulating Form POST in PHP

There's small choice in rotten apples.

Recently, I had a need to POST a form, using PHP script, to a remote url.  As I started to research a solution, I soon realized there were very few limited options.

Read more

Comments (4) | Posted in Coding Techniques, From the Workbench, PHP by Ken Foubert


Dec. 11, 2006 at 4:36pm

Names 1, IDs 0

I never enjoy finding quirks in something so seemingly simple when I'm coding but it certainly happens. The following is a tale of how I ran into trouble with a couple attributes that should be worlds apart.

Read more

Comments (1) | Posted in Coding Techniques, Javascript by Kevin Freitas


Dec. 1, 2006 at 8:41am

Use Functions for Readable Code

Code Soup versus Short Stories

On my way to meeting with a client, I got into a conversation about using functions in your code. One of the guys learned to write assembly first, and so his stance was that creating functions was only feasible when you used the same code more than (about) three times. That view makes a certain sense - don't deal with the overhead of creating tons of functions for stuff you'll seldom use.

I argue that you should use functions where ever possible, first because it makes code easier to generalize (and thus re-use), but also because it makes code much more readable.

Read more

Comments (1) | Posted in Coding Techniques, PHP, Software Engineering by Dave Poole


Nov. 8, 2006 at 1:36pm

The Joys of Object Oriented PHP

It's pretty common for PHP developers to make complex and difficult to maintain scripts, and I am no exception. I write my code in two distinctly different ways: scripts that do a whole bunch of things depending on input, and classes that do a bunch of things depending on how they are called, but from lots of different scripts. But which is better?

It's a nice ego boost for any prrogrammer to know they created a script/function/class that is really useful, and well designed, but at some point the code stops being well designed, and becomes gross. I think that point is when you have one script that displays an entire (SQL) table of data, views a particular row, edits that row, can delete that row, or can add new row to the table.

If my Software Engineering professor sees me doing that, I imagine that he'll reevalute my grade in his class.
Read more

Comments (2) | Posted in Coding Techniques, Critiques, PHP by Dave Poole


Oct. 27, 2006 at 11:25am

MS SQL Server 2005 text and ntext

text, ntext, and image data types have been deprecated

How I discovered that text, ntext, and image data types have been deprecated and replaced by varchar(max), ntext(max), and varbinary(max).

Recently, I needed to perform a query to update a number of links for the Pierce Count Library website. I thought I could use a simple REPLACE() string function, unfortunately, this turned out not to be the case. In order to update text and ntext datatypes using a query you're limited to a few functions, in my case I would be forced to use SUBSTRING() or UPDATETEXT().

Read more

Comments (1) | Posted in ASP.NET 2.0, Coding Techniques, From the Workbench, MS SQL Server by Ken Foubert


Oct. 24, 2006 at 9:41am

Group By Queries for MS SQL Server

Applying MySQL Wizardry to MS SQL Server

Back in the day, we worked on developing a specialty application that was basically a lead generation system with a database that stored over 200,000 records, with a potential for a lot more. The web application displayed numerous reports that calculated totals from disparate sources. We discovered that once our client began adding all their data that those reports were running slower and slower and slower.

The problem was that we had one primary query to pull the records out, then, as the code looped through each record, several other queries were needed to calculate the disparate totals. That resulted in numerous database calls that slowed the entire web application. That's when Mike discovered MySQL Wizardry, that used the SUM(IF()) and the GROUP BY clause, problem solved.
Read more

Comments (4) | Posted in Coding Techniques, MS SQL Server, MySQL by Ken Foubert


Oct. 18, 2006 at 10:46am

Navigation Nightmare pt. 1

Something no developer wants to see...

foreach ($left_elements AS $le) {
    ?>
    <table class="leftnav_<?=$le['class']?>">
        <tr>
            <td class="leftnav_<?=$le['class']?>">
                <a href="<?=$le['target']?>" class="<?=$le['theme']?>"><?=$le['name']?></a>
            </td>
        </tr>
    </table>
    <?
}


Nobody wants to see that. Especially not seven different times with seven tiny variants covering seven different if conditions.

Each higher 'class' number is a deeper indentation. Apparently it's necessary for this class identification to be repeated for the table, table cell and link. Converting this to a nested list format (part of my current task) is going to be an adventure.

Well... at least it's indented nicely.

Comments (3) | Posted in Coding Techniques, CSS, XHTML by Joe Izenman


Oct. 17, 2006 at 11:33am

Smart Keyword Searching

Earlier this week the brand new Pierce County Library website officially launched. It was a pretty complex build that took a lot of effort between ourselves and the library's staff to make sure that everything worked right for the site's users. One of the more important features on any website with a large amount of content is a solid search function, and this site was no different. With over 300 pages (and still growing), a user could easily have some difficulty tracking down the information that they are looking for. So what can you do to make finding information easier? Read more

Comments (2) | Posted in Coding Techniques, From the Workbench by Mike Ash


Oct. 16, 2006 at 12:57pm

File Extensions

I've been using a fairly complicated function to grab the extension of an uploaded file. I have run across many instances where I have wanted to split up the file name from the file extension, or just find the file type. Since MIME types are not reliable enough for grabbing a true extension, I have been using this aforementioned function.
Read more

Comments (5) | Posted in Coding Techniques, PHP by Mark Neidlinger


Oct. 13, 2006 at 4:56pm

Into the Nested Table Abyss

At least it's clean code!

While I was taking some time to evaluate a 3rd-party application for a customer I took a peak at their site's HTML. I was pretty shocked when I found some very strictly organized code that contained gobs of nested tables. At one count I found a structure that was 10 tables deep! I suppose since enjoying creating clean mark-up and mostly table-less designs for the last few years I've forgotten my roots.
Read more

Comments (2) | Posted in Coding Techniques, CSS, XHTML by Kevin Freitas


Oct. 13, 2006 at 10:59am

A Piece of Query Cake

Making Inserts Sucks

If you've ever created SQL queries with PHP, you probably know what a pain it can be to create insert and update statements. I really, really (really) don't like it. As I was working on my personal site, and exploring possible frameworks to use, I came across CodeIgniter. They have a great database interaction library, especially the function for creating the insert queries.

Today, armed with only the descriptions of CodeIgniter's query helper functions, I spent 20 minutes trying to duplicate some the effect of the insert and update functions. I've never seen the code, or even used it, but I didn't have to see the code to write a similar function. Both functions take a table name and an associative array of column names and values. The update function also requires a WHERE statement, and it can't be blank. This is different from CodeIgniter, and that's so you don't accidentally reset all of the passwords in the mysql users table, or any table for that matter. And then, *poof*, the function gives you a nice sql statement.

I'll never have to write another "INSERT blah blah blah" again. Yay!
Read more

Comments (2) | Posted in Coding Techniques, MySQL, PHP by Dave Poole


Oct. 11, 2006 at 7:29pm

The Three 'M'-igos

Why butter fingers break apps

Happens all the time, right? One minute you're cranking out line after line of quality code then the next it all comes to a screeching halt for no apparent reason. I immediately go into troubleshooting mode. A quick and simple redirect using a header("Location: ...") function should pose no danger of messing up my app -- but today it did.
Read more

Comments (3) | Posted in Coding Techniques, PHP by Kevin Freitas