Programming


I ran across this piece of code in a program I am updating. The original author is a great game designer, but only a fair programmer. He also seems to have very limited experience with SQL.

[php]
if ( $grab == ‘Ammo’ ) {
$check = mysql_query(”SELECT * FROM BF WHERE owner= ‘None’ AND country=’$fetch->location’”);
$num_rows = mysql_num_rows($check);

if ( $num_rows != 0 ) {
if ( $fetch->location == $location ) {
mysql_query(”UPDATE BF SET owner=’$username’, profit=’0′ WHERE country=’$fetch->location’ LIMIT 1″);
$this->content .= “You got the Bullet Factory!”;
}
}
}
[/php]

When writing database applications, you need to be as good at writing queries as you are at writing code. Here’s a rewrite:

(more…)

In our last exciting episode, I promised a crude flowchart and I don’t want to let anyone down. You’ll need to click on it to see the whole thing. Feel free to open it in another window and follow along.

As I read my previous posts it occurred to me that you may not see where I am leading with all these files. The idea I have in mind is to allow editing a particular section of the web site in just one file, but more importantly, I want to limit the changes made to that file to affect only that portion of the web site we are making changes to. As long as each section edit maintains well formed markup, that should happen in each instance.

(more…)

In our last exciting episode, I presented a very basic use for SSI. Let’s take a closer look. Here’s an example include directive which pulls in the contents of a file named content.html located in my www directory.

[html]

[/html]

Now that’s pretty powerful if you have never seen it before, but it gets old quick. Let’s add a little power using an Apache variable.

[html]


[/html]
(more…)

A CMS is great for people who don’t know HTML, but what about those of us who do know HTML and CSS and all those other things?

A CMS is a great way to set up a web site for a customer. Weighing in at 15 to 20 Megabytes, they are often cheaper than forcing your customer to pay for every little update. If you have the tie to learn even a small part of their features you can serve your web site design customers well and keep their costs down.

Quite a few years ago I was working with Apache on a web site and we needed quick and dirty method of creating content for each page which used common headers, footer, menus, etc. Perhaps I was too inexperienced to know about Content Management Systems or perhaps there were just none around.

(more…)

I guess this is more a reminder to myself than useful information, but here goes.

Recently, I read up on PHP 5.x header() function. I ran across this tidbit of info.

HTTP/1.1 requires an absolute URI as argument to ยป Location: including the scheme, hostname and absolute path, but some clients accept relative URIs.

I don’t know about you, but I find RFCs incredibly boring, so I’m glad that some insomniac took the time to find this gem.

Following this standard eliminates another problem in a different language. In Perl, many people correctly use CGI.pm to perform redirects. The CGI manual gives the same advice. Use full URLs to redirect.

Here’s a mini guide to redirection: (Basically, you just print the redirect out.)

(more…)

The jQuery JavaScript library offers some quick and dirty solutions to many problems. I was perusing a jQuery email list when I ran across a simple problem that a beginner JavaScript coder like me could solve. I’ll simplify it a little here to focus this article.

Basically, the poster wanted to add form fields to a table, but he wanted to increment a field counter in the HTML and in the id and name tags. Just to make it a little more difficult, the field numbers needed to be padded with leading zeros.

Adding the table rows is trivial in jQuery and it turns out that leading zeros are pretty easy too. I like the “everything is an object” approach to JavaScript. You can extend even the built-in objects including the String object. Let’s tackle that first.
(more…)

A common question on the beginner programming mailing lists goes something like this:

have been trying to open a web page into internet explorer using the getprint command and then opening the file for internet explorer. The getprint command only downloads the code to the command window. I have used the following code and have used the getprint line before and after the internet explorer line ,

To the expert this might look similar to “How to apply a phillips head screw with a tire iron?” The obvious answer is “You don’t. You use a screwdriver.”

If you find yourself about to ask a question in this form, you are limiting yourself. Try to look for a solution that does not involve the tire iron. If inspiration doesn’t inspire you, ask the beginner programming group How to do A and leave off the limiting factor B.

In general, subroutines should receive data, process that data, and then output something. There are some exceptions. In void context, we have no output. In the case of information retrieval from a fixed source there may not be any input. An error logging routine might have input and output but no processing.

Good subroutines only affect and are affected by their own environment. If events exterior to the subroutine affect it, you have probably done something wrong. Here’s an example of a poorly written subroutine. foo() is using $bar. $bar has not been passed to it. Remember that a well written subroutine has data passed to it. Good subroutines only affect and are affected by their own environment. Deleting the $bar variable outside this subroutine affects the subroutine.

[perl]
sub foo {
return $bar;
}
[/perl]
(more…)

CGI.pm is a module included in the standard perl distribution. It is the most commonly used module for writing perl scripts that interact with the web.

I run across these mistakes a lot when reviewing beginner perl code. The first mistake has to do with the dual nature of perl modules. A perl module can be accessed either as a function-oriented module or as a object-oriented module or by both methods.

CGI.pm allows both. Under the hood, that makes for some messy looking code. In practice, we find a lot of bloated code. Normally, we would use either the function oriented or the OO version, not both.Here are a few of the most common errors I see.

(more…)

The author of this module chose to use some file scoped variables in a module. This effectively created some global variables used outside the sub. This is not the recommended way to write a subroutine, but I needed an example and this one was handy. :)

[perl]
my $htmltree;
my $node;
my @prevnodes;
my $htmloutput;

sub start {
my $tagname = shift;
my $attr = shift;
my $newnode = {};

$newnode->{tag} = $tagname;
foreach my $key(keys %{$attr}) {
$newnode->{$key} = $attr->{$key};
}
$newnode->{content} = [];
push @prevnodes, $node;
push @{$node}, $newnode;
$node = $newnode->{content};
}
[/perl]
(more…)

Next Page »