Archive for July, 2004

Optimization

This evening I didn’t have much time to code on the next section, so I took a few minutes to look through the code a little…

On both the Category Add and Category Edit screens, I had very similar code for displaying a dropdown box of the possible Parent Categories. The major difference is that the dropdown box for the Add screen should always start with None selected, but the Edit screen should select another item. Passing a variable to the function added that distinction… While doing a bit of testing, I realized a minor problem. On the Edit screen, it was possible to select the category being edited AS the Parent category… Talk about circular logic!

This was also fixed with another variable being passed to the function. $excludeID defaults to a value of -1 (which no real category will have). If the dropdown box that I need should have a particular category excluded, I just pass the ID of the category into $excludeID.

July 28, 2004 at 3:38 pm Leave a comment

Where to begin

That’s often the question when writing a decent sized project… I already have my database schema created, otherwise I wouldn’t even be asking this question…

In this case, there are really two very seperate parts of this project.
1. The presentation to the end user. With categories and data in the database, one portion of the application should be the user interface.
2. Where does all the content come from? The ADMIN area, of course…

Since you really need to have data before you can display it, I decided that item 2 (above) is the place to start…

Priority #1: The ADMIN area must be secure… After all, who wants hackers logging in and changing your FAQs to FUs… An administrator user and password must be used to gain access, and each and every page must be secure. Since I was unsure of the best way to handle this, I turned to Harry… Harry who? Harry Fuecks, that’s who. He wrote the PHP Anthology (a 2 volume set), which gives good, solid solutions to common PHP problems. I’ll probably write a review later, but so far I’ve not even read half of it. Included is the excellent SPLIB, a good library of PHP classes. For example, Sessions and MySQL are easy to use in PHP, but Harry has classes for both. Why use classes? When a third class ties two others together (such as the Auth class), it makes things so nice… (I did have to apply my iisCompat.php include file to get Auth working, so there may yet be a bit of work in it, even with this set of books.)

Anyhow, with Authentication handled, I needed a menu…

Since I’m using an ASP based FAQ solution currently, I’ve decided to use the HTML menus that it comes with as a base.

With that easily handled, I set off to handle the Category section.

My categories are significantly different than the Categories of my ASP FAQ system. My ASP sysem uses flat categories, not even allowing one level of sub-categories. They are sorted alphabetically when displayed to the user. I want an unlimited number of sub-categories, and I want to choose how they will be displayed to the end user. (The sort order)…

To this end, my Category table includes these fields:
ID
Name
ParentID
SortOrder

(Actually, there are a few more, but I’m not doing anything with that functionality yet, so we’ll leave them out of this, thank you.)

The ID field is an autonumber field which MySQL will control. The Name field is (gasp!) the Name of the category. The ParentID is the ID of the parent category (if this is a sub-category). Top level categories have a ParentID of 0. Finally, there is SortOrder. This field is an INT, and tells us the order (in a list) that this category will be displayed.

First up was adding a category… Done in very little time.

Next up was sorting… This took a bit of thought, but I finally came up with a good solution. I decided on “Up” and “Down” links in the table next to each category name. The Up and Down links were returning you to the same PHP page, but with an action of “swap” in the URI, and with ID1 and ID2 being the Category ID’s of the two categories that are having their sort orders swapped.

Next up? Editing a category… Almost too easy…

Finally, Removing a category.. Now, this was tricky. As Kevin Yank’s book (Build your own Database Driven Website using PHP and MySQL) pointed out, deleting data from a database is about the hardest part, at least when there are relationships to other tables.

In my case, there are relationships to other tables.

First, each FAQ question is going to be assigned to at least one category. Not too much trouble here… Since I’m using a lookup table, I’ll just delete all entries for the deleted category. I’ll need to point out to the Administrator that there are “unfiled” questions so they can manually file them, but that’s later.

Secondly, since I’m using the ParentID field, I need to set that to 0 for any categories who’s ParentID pointed to the deleted category. Easy enough.

Lastly, the SortOrder field… This is a tough one.. I actually thought on this a bit over the course of a few days before settling on this method as the best… But I still think there should be a better way. When a new category is added, the SortOrder field is set to the number of the new category, so it will be displayed last in the list. If a category is deleted, this would leave a gap if we do nothing, which only causes problems when future category changes take place. So, we must revisit EVERY category that has a SortOrder higher than the one of the deleted category, and decrement it by one.

Code to do this would look something like:


for ($x = $sortOrder+1; $x query($sql){
$sql = 'UPDATE category SET SortOrder = ' . ($x -1) . ' WHERE SortOrder = ' . $x . ';';
$result = $db=>query($sql);
}

Of course, after writing this, I sat back and thought: This is insane… There has got to be a better way… Issuing a separate SQL query for almost every category in the list is… well, too stupid for words. Honestly, for a small category table like this it wouldn’t be a big deal, but I needed to “think SQL” better… After a bit of thought and thumbing through the Advanced SQL chapter of Kevin Yank’s book, I came up with the “better way”. This query:

$sql = “UPDATE category SET SortOrder = SortOrder – 1 WHERE SortOrder > ” . $sortOrder;

Using that query statement, I only need a single Query and MySQL does all the heavy lifting.

So, that’s it for the category table manipulation… I would suppose that the questions table would be next…

July 27, 2004 at 3:30 pm Leave a comment

PHP under IIS: Server Variables

In my research thus far, I’ve found that PHP running under IIS isn’t quite the same as PHP under Apache. Most of the code you find on the internet is written to work with Apache, but the very same code may not work properly under IIS… In cases like this, you don’t really get error messages about problems… Things just don’t work like you expect! As you can imagine, this can be very frustrating.

One issue that I ran into rather quickly was the fact that $_SERVER[‘REQUEST_URI’] doesn’t work under IIS… In these cases, we have to build our own. To do so, I wrote iisCompat.php. Rename the file from .doc to .php after you’ve downloaded it.

Note that you may have to select a different line depending on if you are running in ISAPI or CGI mode.. I found that on a note about IIS compatibility issues and haven’t confirmed that independently, but thought I should make a note of it in my code in the event that I run into an issue in the future…

July 25, 2004 at 3:16 pm Leave a comment

FAQ Project

I run a few other sites and I’m currently running an ASP FAQ system on them, but I’m considering re-writing it in PHP to modify and add some features.

Let’s go over the pros and cons…

Pros:
1. Get my feet wet with a fairly serious PHP Web application, but not one that is terribly complex.
2. I have an existing ASP system to use as a template for what the end product could look like.
3. Will get jump-started fairly quickly because very little planning is required.

Cons:
1. Well… I can’t think of any…

With this list of Pros and Cons, I think I can safely say that a Project: FAQ is upcoming…

July 24, 2004 at 3:14 pm Leave a comment

My Programming History

I recently realized that I hadn’t adequately explained what my programming, PHP, or web development experience level is.

I’ve been programming in various languages on multiple platforms now for about 20 years. Languages that I’ve worked with extensively (and roughly in order) include: Turbo Pascal, C, C++, Rexx, C#, and now PHP. I’ve written applications in these languages on platforms ranging from DOS, OS2, Palm OS, Solaris, and Windows.

Applications that I’ve written include a BBS Door game, a Palm Address book, an SNMP Trap-mapper, numerous Rexx utility programs, various Windows command line applications, a Windows GUI application to certify Frame Relay circuits, and (within the last 6 months) numerous small command-line PHP utilities for Solaris.

The vast majority of my PHP experience (at this point, at least) is with a Solaris system, with command-line PHP apps… Early this year I realized the power that PHP had in the form of the many extensions that are easily enabled. After explaining the advantages, my managers allowed for a trial run of PHP to see how well it worked in our environment. The ability to read and update a MS-SQL database from Solaris has proven to be a huge time-saver, not to mention the ability to easily script telnet sessions, and even perform SNMP queries directly from PHP. We have been slowly converting from Rexx to PHP for quite a few functions.

Sounds fairly impressive, right?

Now, I have very little web development experience…

1. My first web application was written in C# and it simply accepted input from a Solaris application via HTTP POSTs, populated a MS-SQL database, and displayed filtered and sortable results sets on various web pages. It has been in use for about 2 years now, so it must not be that terrible.

2. More recently I’ve written a C# app to manage an APC remote reboot switch which only has a single user.. My C# application takes a username and password from the user, determines which ports they can manage, shows the status, and lets them reboot the device(s).

3. My most recent web development project was related to MRTG. It was written in PHP and essentially restricts a customer’s view of MRTG to only the ports they are utilizing.

So, my main PHP experience thus far is writing database related command-line utility applications for Solaris. Oh, and by the way, my PHP web experience is with IIS under Windows, which throws another monkey wrench into things.

There you have it. I am by no means an expert on PHP for the web, but do have a good bit of PHP experience with unusual uses of PHP, and I hope to be able to share some of those experiences with you here, as well as share in the building-up of my web-related skills.

July 23, 2004 at 3:11 pm Leave a comment

Directory Paths in PHP

The way directory paths work in PHP isn’t quite the way I had anticipated… (Until recently, I accessed all include files with the full path, so this wasn’t an issue.)

Let’s say you have the following directory/file structure:

/index.php
/config/config.inc
/includes/auth.php

Your index file in the root directory contains this statement:
include_once(“./includes/auth.php”);

and your auth.php file contains:
include_once(“../config/config.inc”);

In this situation, if you access the auth.php file directly via a web browser, as in “http://mysite.com/includes/auth.php”, it will work as expected, loading the config.inc file and running happily.

However, if you access the index.php file, you will get error messages indicating that the config.inc file can not be found.

Initially, this confused me… I would have thought that the path you use in any included file would be from the location of that file, but it appears that PHP uses the location of the calling file… In this case, the root… So, it was attempting to go up a level, then look for a config directory that didn’t exist…

So, just keep that in mind when designing your directory structure and performing includes, and you’ll be fine…

July 21, 2004 at 3:09 pm Leave a comment

Build your own Database Driven Website using PHP and MySQL, 2nd ed.

I bought this book by Kevin Yank from sitepoint.com. It arrived last Tuesday and I’ve already read the bulk of it… (I skimmed the basic PHP parts.) This book assumes only that you know a bit about HTML, so it is very good for someone with very little PHP programming or MySQL database background. People already intimately involved in using PHP and MySQL would probably be thoroughly bored, but then they shouldn’t be buying this book.

In my case, I’ve been using PHP extensively for about 6 months now at my day job. I don’t have an incredible database background… In fact, I’m using MS-SQL (not MySQL) with PHP in the bulk of my projects, and I’m not doing much in the way of relationships between various tables. With that in mind, you might think that I wouldn’t have bothered with this book. The big difference between me and the average PHP developer is that the last 6 months that I’ve really been using PHP, almost 100% of that has been command-line PHP, not web-based.

The portions of the book dealing with basic PHP were not required reading for me, but what I did read (of those sections) was well written and easy to follow…

Chapter 5, “Relational Database Design” is simply wonderful. In it, Kevin tells the reader in easy terms the general rules to follow in designing your database. He talks about the differences between “one-to-one”, “one-to-many”, and “many-to-many” relationships while using examples along the way to really drive the point across. I had some lightbulb moments where I realized how I’d be able to do some of the things that I want to do with my CMS project.

In Chapter 6, he goes on to detail a CMS for his fictional joke website. Most of the basics that I’m going to need as I write my own CMS in the upcoming weeks are here.

Chapter 9 gets into some of the more Advanced SQL query types that you can perform. LEFT JOINs, GROUP BY, and Aliases are covered here, among several other advanced topics.

Kevin rounds out the numbered chapters with more advanced topics. He tackles file uploads, semi-dynamic pages (which I will definately want to use), how to store, retrieve, and display binary data with MySQL and PHP, and finally a simple shopping cart application.

There is a section about using cookies and sessions for authentication purposes, but I found that Kevin’s sitepoint article to be much more informative than the information in the book. Perhaps there just wasn’t enough room in the book for this article too, but it was exactly what I was looking for.

In conclusion, if you are interested in making a dynamic site with PHP and MySQL, this book will be of significant assitance. It is the largest collection of clear and concise information on this topic that I’ve located.

July 20, 2004 at 3:04 pm Leave a comment

Sample Spec sheet

Here’s a rough draft of a the first spec sheet I wrote up for an CMS long ago:

1. Security
a. Must have a secure authentication method for authors/admins
(Would have not mentioned this on the Spec sheet, but I've never done it before)
b. Need to have author/admin access levels
c. Possibly be able to limit authors to writing in certain categories
2. Category Support
a. at least 3 levels deep (sub-categories)
b. A method to allow one article to fit into multiple categories
c. Online category management by admin (not just via direct DB manipulation)
3. Article Management
a. Article List page should have options to
i. List all articles
ii. Search text of articles for keywords, Category, or Status
iii. Edit/Delete article
b. Articles should have these DB fields:
i. ID, Status (visible, hidden), AuthorID, Date, Title, Summary, Content, Deleted, AllowComments, and Featured fields.
c. Should use an embedded WYSIWIG editor
d. Should allow uploading of images
e. Should parse text of article for information pertaining to the placement of uploaded images
f. Should allow for multi-page articles via embedded page breaks
4. Template system
a. Should be CSS based (another thing I need to learn better!)
b. Need to do more research on templating methods....
5. Semi-Static HTML pages
a. Since the content of the site will only be updated when new articles and news are added, the site should be pure HTML for presentation to the user, at least in the article pages. This allows for much higher volumes of traffic to the site with minimal server load.

July 19, 2004 at 3:00 pm Leave a comment

Application Design

When writing an application from scratch, the very first thing you need is a plan. You need detailed design specifications. After all, it’s hard to write a piece of software if you don’t know everything that you want it to do.

My first step in this process is to closely examine the existing market. Start with the Open Source projects out there, but you also need to look at commercial applications. These are usually more polished and have more features that professionals want, not just the geeky features we tech-types want. For some of these commercial systems you might only be able to get screenshots. And that’s fine, as all we are doing is design work at this stage anyhow. Once you’ve worked your way through a few open-source projects, you can probably figure out how a commercial package works simply by looking at screen shots.

Oh, and do not give up on a feature at this stage just because you can’t see how to implement it. Put it down anyhow, and research how it’s done once you are further along in the design process.

Once you’ve gotten your general spec list down, think about it.. Give it a few days and think a bit on it each day. Your spec list shouldn’t be something that you write down in 30 minutes and then start programming. It should be a well thought-out document, and it should be as specific as possible.

The reason that you should get this as set-in-stone as possible before moving forward is simple. It is much easier to code a project as a whole, with all pieces accounted for from the begining than it is to come back later and try to add pieces in. It is inevitable that you will want to do that, but the fewer “add-ins” you have of this nature, the better.

July 18, 2004 at 10:27 am Leave a comment

HTML for the World Wide Web, 5th Edition

Elizabeth Castro’s “HTML for the World Wide Web” is a book that needs to be on the bookshelf of anyone wishing to become a web developer. In fact, if you already know HTML fairly well, this book will do a good job serving you as a reference. It’s 480 pages packed with easy to understand language, example HTML code, and screenshots.

Aside from the complexities of HTML, the 5th edition also deals with XHTML and CSS, but you are not likely to become an expert on CSS from the content of this book. This book really shines in the way it teaches you how to use HTML. If you want to learn CSS, get a book dedicated to it, as much more could be written on it than is in this text.

Personally, I mostly use this book as a reference. Whenever I need to figure out how to do something that I’m unsure of in HTML (or when something isn’t working the way I would expect), I simply look it up in the index and get to work. The author uses clear, consise, and fairly short explanations on how to get the job done. You definately won’t get bored with any theory… Every page is in two column format. The outermost column is explanation text, while the innermost column contains segments of HTML demonstrating the current topic, and a screenshot showing the results of the HTML. Many pages also contain Tips, sometimes mentioning browser incompatibilities, or related topics with references to other pages in the book.

I’ve been using various editions of this book for the last few years and it hasn’t disappointed me when I’ve had questions about any HTML issues. Get it… You’ll be glad you did.

July 16, 2004 at 6:23 pm Leave a comment

Older Posts


Calendar

July 2004
S M T W T F S
 123
45678910
11121314151617
18192021222324
25262728293031

Posts by Month

Posts by Category