Feed on
Posts
Comments

Improving Our Application

Originally I had intended for this post to include building a simple Content Management System (CMS) using object oriented programing and a mySQL database back end, but I considered that some of my readers who have never used these technologies before may become confused. So instead we will be taking some baby steps and making some small improvements to the application we built in the previous post.

The first improvement we are going to make is changing the datafile. The old datafile was a comma separated value file which would cause some serious problems for anyone who was actually going to try to blog with our simple application. So the first thing I did was replace all the commas with &^&. The &^& symbols would almost never show up together in a blog post so they are a pretty safe bet if you want to use text files to store your posts.

Hello&^&09-04-2007&^&This is my first post!
Testing&^&09-03-2007&^&I am going to test my blog now!

The second modification I made to the data file was adding a “post order”. This number is the order in which the posts are displayed. This is **not** a good longterm solution but for training purposes it will work fine.

1&^&Hello&^&09-04-2007&^&This is my first post!
2&^&Testing&^&09-03-2007&^&I am going to test my blog now!

Our next step is to remove all the post displaying functionality from index.php and place it into a class. In this type of example a class is basically a collection or related functions. Classes can also be used to build objects. A good way to look at Classes and Objects is to think of them like cars. A Class would be the blue-print for a Honda Civic and an Object would be a Blue Civic EX or a White Civic SI. In the following code I have taken all the blog post related functions and placed them into a class called Post.

  1. <?php
  2. class Post {
  3.     function _getRawData($filename) {
  4.         $stream = fopen($filename, "r");
  5.         $raw_data = fread($stream, filesize($filename));
  6.         fclose($stream);
  7.        
  8.         return $raw_data;
  9.     }
  10.     function getPostArray($filename="posts.csv") {
  11.         $raw_data = Post::_getRawData($filename);
  12.         $posts = Array();
  13.         foreach(split("\\n",$raw_data) as $line) {
  14.             $post = explode("&^&", $line);
  15.             if (count($post) <= 1) {
  16.                 break;
  17.             } else {
  18.                 $posts[] = "<p>";
  19.                 $posts[] = "<strong>" . $post[1] . "</strong>
  20. ";
  21.                 $posts[] = $post[2] . "
  22. ";
  23.                 $posts[] = $post[3] . "
  24. ";;
  25.                 $posts[] = "</p>";
  26.             }
  27.         }
  28.         return $posts;
  29.     }
  30. }
  31. ?>

In the code below I have done the same thing as above except this class will store all the functions related to Layout. In a later example we can make the layout class more intelligent to all the user to change the theme and style of their blog.

  1. <?php
  2. class Layout {
  3.     function Header($title=null) {
  4.         $out = Array();
  5.  
  6.         $out[] = ‘<html>’;
  7.         $out[] = ‘<head>’;
  8.         $out[] = ‘<title>’ . $title . ‘</title>’;
  9.         $out[] = ‘</head>’;
  10.  
  11.         return implode("\\n", $out);
  12.     }
  13.     function Content($title=null, $data=Array()) {
  14.         $out = Array();
  15.  
  16.         $out[] = ‘<body>’;
  17.         $out[] = ‘<h1>’. $title . ‘</h1>’;
  18.         $out[] = ‘<p>’;
  19.  
  20.         foreach ($data as $d) {
  21.             $out[] = $d;
  22.         }
  23.  
  24.         $out[] = ‘<p>’;
  25.         $out[] = ‘</body>’;
  26.  
  27.         return implode("\\n", $out);
  28.     }
  29.     function Footer() {
  30.         $out = Array();
  31.  
  32.         $out[] = ‘</html>’;
  33.  
  34.         return implode("\\n", $out);
  35.     }
  36. }
  37. ?>

Lastly we are left with a more simplified index.php file. While this is nowhere near what I would consider to be a “Simple CMS” it is still a good start.

  1. <?php
  2. include(‘./classes/layout.class.php’);
  3. include(‘./classes/post.class.php’);
  4.  
  5. $out = Array();
  6. $out[] = Layout::Header(‘Simple Blog’);
  7. $out[] = Layout::Content(‘Simple Blog’, Post::getPostArray("posts.csv"));
  8. $out[] = Layout::Footer();
  9.  
  10. print implode("\\n", $out);
  11.  
  12. ?>

In the next example we are going to be removing the CSV file and replacing it with a mySQL database, as well as adding some fully object oriented programming examples.

If you like this post, then consider buying me a beer!

Anyone who is thinking about getting into managing websites or even just programming will most likely need to build (or manage the building) of a web application at some point. While trying to teach an online course in building web applications would be an exhausting procedure it would be of great importance to a lot of people to learn the differences between different web applications on the internet. In this series of posts we are going to be covering different implementations of PHP and what their strengths and weakness are.

The first step you need to take is determining what kind of PHP implementation you need. If you are running a small content-based site then you may not need to go out and install Joomla, but on the other hand if you are trying to start the next Wikipedia then writing a flat site with a few PHP scripts may not be a good idea either. In this series of posts I will place most PHP implementation in one of three categories.

The first category is the “flat site with PHP scripts impeded”. This is the simplest and most common form of PHP implementation on the web. This sites basically consists of creating a static HTML page and embedding PHP scripts within it. This can be useful for many smaller or more simple sites because it allows you to add some simple dynamic content without having slowdown caused as a result of processing time or database calls. In many cases using CSV or XML files will do for storing data provided that they don’t contain passwords or other sensitive data.

Example

  1. … Html stuff goes here
  2. <?php
  3.         print "Today is " . date("l") . "!";
  4.         print "";
  5.         print "This is my web page";
  6. ?>
  7. … Html stuff goes here

Advantages

  • Fast
  • Lightweight
  • Simple
  • Don’t need a database
  • CSV or XML files will work fine for content

Disadvantages

  • Limited dynamic content
  • Fast growing pages may become very complex very quickly

Okay, so let’s get started building a simple blog. As you can see below a simple PHP script has been written the takes data from a CVS file and prints it to the screen. it’s pretty simple, but it gets the job done overall. You can even continue to build on this an add a nice style sheet to really spruce your blog up. Next time we’ll build a simple page that allows an author to post to their blog without having to edit the CVS file directly.

  1. <html>
  2. <head>
  3. <title>My Simple Blog</title>
  4. </head>
  5.  
  6. <body>
  7. <h1>Simple Blog</h1>
  8. <?php
  9. function getRawData($filename) {
  10.     $stream = fopen($filename, "r");
  11.     $raw_data = fread($stream, filesize($filename));
  12.     fclose($stream);
  13.     return $raw_data;
  14. }
  15.  
  16. $raw_data = getRawData("posts.csv");                                                        
  17.  
  18. foreach(split("n",$raw_data) as $line) {
  19.     $post = explode(",", $line);
  20.     if (count($post) <= 1) {
  21.         break;
  22.     } else {
  23.         echo "<p>";
  24.         echo $post[0] . "";
  25.         echo $post[1] . "";
  26.         echo $post[2] . "";
  27.         echo "</p>";
  28.     }
  29. }
  30. ?>
  31. </body>
  32. </html>

And here is the CSV file I used.

Hello,09-04-2007,This is my first post!
Testing,09-03-2007,I am going to test my blog now!

In the next post I will be discussing how to improve this simple script (there are some glaring problems with it if you have done this before) and use it to build a simple Content Management System. As we progress on this series of posts we will be building a basic CMS / Blog Management tool.

If you like this post, then consider buying me a beer!

Today while doing my usual morning routine I noticed something familiar on Digg. It seems that the speaker list for Ohio Linux Fest has made the front page of Digg.com! This really floored me because Ohio Linux Fest was what originally got me so excited about the open source community and since last year’s conference I really feel as though I have grown greatly as a programming and a technology enthusiast. I just hope I have the cash to attend this year.

olfondigg.png

Somehow I feel as though the month of September will feel as though it will take forever simply due to my level of excitement. I am epically happy for my friend Dave, who will be speaking this year of software usability. If you plan to attend Ohio Linux Fest then you should defiantly attend Dave’s talk.

If you like this post, then consider buying me a beer!

If you are a fellow technologist then you will most likely want to place code examples in your blog posts at some point. This can become a difficult and frustrating process because many common characters found in source code will not show up on an HTML page. One solution would be to link to the source code file itself and allow users to download it, but this can be annoying to the user (epically if you are showing them a hello world application). Another common solution would be to write a CSS class for <pre> and <code> but you will not have automatic syntax highlighting.

After starting work on ThinkProgramming (now CrankyCoders.com) I began my search for a module which would allow easy syntax highlighting within posts. What I found was Dean Lee’s Source Code Syntax Highlighting Plugin for Wordpress which is built on GeSHi. The Syntax Highlighting Plugin overloads the <pre> tag and it allows you to specify which language you want to use, such as <pre lang=”python”> or <pre lang=”php”>. For example here is some simple code in a few different languages.

C/C++

  1. #include <stdio.h>
  2.  
  3. int checkpass(void); // function prototype
  4. int main (void)
  5. {
  6.     int x;
  7.     x = checkpass(); // calls function
  8.     fprintf(stderr, "x = %d/n", x); // print the value of x
  9.     if (x) // if x is not zero then
  10.        fprintf(stderr, "Password is correct!/n");
  11.     else // x has been returned as a zero
  12.        fprintf(stderr, "Password is not correct!/n");
  13.     return 0;
  14. }

Python

  1. def print_menu():
  2.     print ‘1. Print Phone Numbers’
  3.     print ‘2. Add a Phone Number’
  4.     print ‘3. Remove a Phone Number’
  5.     print ‘4. Lookup a Phone Number’
  6.     print ‘5. Quit’
  7.     print
  8. numbers = {}
  9. menu_choice = 0
  10. print_menu()
  11. while menu_choice != 5:
  12.     menu_choice = input("Type in a number (1-5):")
  13.     if menu_choice == 1:
  14.         print "Telephone Numbers:"
  15.         for x in numbers.keys():
  16.             print "Name: ",x," \\tNumber: ",numbers[x]
  17.         print
  18.     elif menu_choice == 2:
  19.         print "Add Name and Number"
  20.         name = raw_input("Name:")
  21.         phone = raw_input("Number:")
  22.         numbers[name] = phone
  23.     elif menu_choice == 3:
  24.         print "Remove Name and Number"
  25.         name = raw_input("Name:")
  26.         if numbers.has_key(name):
  27.             del numbers[name]
  28.         else:
  29.             print name," was not found"
  30.     elif menu_choice == 4:
  31.         print "Lookup Number"
  32.         name = raw_input("Name:")
  33.         if numbers.has_key(name):
  34.             print "The number is",numbers[name]
  35.         else:
  36.             print name," was not found"
  37.     elif menu_choice != 5:
  38.         print_menu()
  39.  

As you can see, the plugin does a great job of making code examples very easy to read. For anyone who includes code in their blog this plugin is an absolute must. Also be sure to checkout Dean Lee’s other plugins such as the Google Code Prettify for Wordpress which is very similar to the Syntax Highlighting plugin except that is uses Javascript to format the code.

If you like this post, then consider buying me a beer!

Ohio Linux Fest 2007

It’s September and that means it is almost time for Ohio Linux Fest 2007! For those of you who don’t know Ohio Linux Fest is the largest conference and expo for Free and Open Source software professionals and enthusiasts in the Midwest. This is the second year I will be in attendance, and I cannot be more hyped up. Since May when I started at Wellspring I have learned a great deal about PHP, Linux, and Open Source Software in general so am really excited to actually be able to exchange ideas with other attendees instead of last year when I just sat there acting confused. Last year I heard some really great talks on Google and Open Source, Integration with Single Sign On, rBuilder, the Gnome Project, and optimizing queries in mySQL.

This year I am epically excited because my friend Dave will be speaking on Software Usability. From my understanding, the speech will be based on a paper Dave wrote his senior year, regarding how developers can make software easier to use for everyone. This is becoming more and more important every day as software becomes more of and integral part of our every day lives.

There are a number of other speakers I am interested in, but the one that is really driving me is a talk by Jon ‘maddog’ Hall. ‘maddog’ is responsible for making me excited about programming when I began to loose my drive late last year. For those of you who remember, in August/September of 2006 I was really feeling the strain of life come crushing down on me. Partly this was due to Clarion’s inability to keep the CIS students driven to have an interest in their profession outside of the class room. To make a long story short I was feeling very run down, and I was really wondering why I even bothered with outside projects. Personally I felt defeated. But then after hearing maddog’s speech on how quickly the open source community was growing it really energized me to keep my head up and keep working as hard as I could. I can honestly say that if it was not for that speach I would probably be working as a .NET developer somewhere with my spirit broken.

Zack, Colin, and I (plus a number of others) are going to be making the trip down to Columbus Ohio and see all the sights. So if you are interested then let me know and we can arrange to meet up in Columbus. Sorry, but I do not have any more room in my car or hotel room so unless you have made prior arrangements with me you will need to find your own ride and lodging. Luckily I will be staying at the Drury Inn & Suites, the official hotel (w00t!), so if you want to hang out the night before then just let me know.

One only problem I am foreseeing is that Ohio Linux Fest occurs at the end of the money, so money may be a bit tight for me around that time. All “beer money” for this money will be put toward Ohio Linux Fest, and the rest will be donated somewhere. So if you are really feeling generous please “Buy Me A Beer”.

Take care all you Linux geek, and see you in Columbus!

If you like this post, then consider buying me a beer!

So you want to build an awesome PHP Application that will manage your life and do the dishes? Awesome! But first you need to get a web server. This can be a bit tricky if you are not too familiar with how all that magic work, but fear not, there are a number of easy ways to get a web server with PHP support that won’t require hours of configuration time and weeks spend reading through documentation and posting on mailing lists.

First off let’s start with some basics. If you want to setup a web server then the first thing you need to do is realize that Microsoft does not have all the answers. Microsoft IIS is a great service for setting up a simple page, but it has many limitations and unless you are running Windows 2000 or 2003 you will be limited to a small number of concurrent connections. This is where web servers like Lighhttpd and Apache really shine, because they allow all the power you will ever need in a web server.

Okay so let’s get down to business. Just about any geek will tell you to go to Apache or Lighttpd’s respective websites, read through mountains of documentation, download and compile the source code, and then mess with endless, confusing configuration files before you even reach your “Hello World” page. Well, screw that. Manually installing web servers have their place but when you just need to get down to coding you need your server now. Well there are many solutions which will provide you with a robust and stable development environment until you are ready to take you plunge into running a live site.

While there are many different choices available for Windows users, I personally recommend a suite called EasyPHP. EasyPHP consists of Apache, mySQL, PHP, PHPMyAdmin (a mySQL database administration tool written in PHP). EasyPHP is also very easy to install and configure (epically since the “out of the box” configuration will do just about everything you need to do). Plus once you no longer need EasyPHP it uninstalls cleanly so you won’t have to worry about cleaning up the mess that some applications like this leave behind.

Mac OS X users have a bit less choice when it comes to the all in one package. Mac OS X includes a working version of Apache 1.3 and PHP 4, but it does not include mySQL. Also many developers are switching over to Apache 2 and PHP 5 so this may not be the optimal solution. I have used a software suite called MAMP to run a development environment on my Macbook and overall I have had good experiences with it. MAMP contains a simple GUI and allows for some basic configuration (such as choosing between PHP 4 and PHP 5) but it could use a few more features to really stand out from the crowd. MAMP is a bit of a memory hog but as long as you are running with more than 1GB or RAM you should be fine.

Since I do not want to get into the complexities (as well as the thousands of different paths to take) of installing Apache, PHP, and mySQL on a Linux box I will leave the Linux newbies with this link. This is probably the easiest way to get LAMP running quickly on a Linux box. But if you do not have a spare machine laying around you could always download VMWare Player, VMX Builder and get a Ubuntu Image.

Hopefully this posting will come in handy for those looking to setup a quick development environment and get to coding.

If you like this post, then consider buying me a beer!

Moving the Blog

Hey all, I am currently in the process of moving my blog over to a new server. Mr Bob Buskirk has been kind enough to host my blog for the past few months, but I have gotten a hold of a new web host. Things shouldn’t be affected too much but it may take some time for me to get all my content moved over. If things don’t work for a few days don’t despair, it will all be back up and running again in no time.

Since my blog has been on Bob’s server the domain name has been either www.JonDaniel.net or www.thinkcomputers.org/blog/cleric. Well once I move the blog the official domain name will be www.JonDaniel.net, but I will setup a redirect on the old address for awhile.

Since there a number of posts on my blog that I consider “worthless” I have decided to migrate some of my selected older posts manually, so if there is something you want to read then wait a day or so and it might be over.

If you like this post, then consider buying me a beer!

Currently I have passed the three month mark at work, and just about every day of my life has been full of PHP, PHP, and more PHP (with a bit of Python and SQL). I can honestly say that I really enjoy my job and I am happy to come in to work every morning, but I have been noticing a bit of a disturbing trend. Today I went back to some of the code that I wrote in my first month to add a few client-requested features and I noticed that it was down right ugly. Saving objects before any data was inserted, creating and deleting null records in the database, functions that broke other functions if not run correctly, and the list goes on and on. Once I found some of this code I really had to ask myself “Was I this bad in college?” and the answer shocked me.

I logged into my ftp server at Clarion (which I still have access to for another year) and pulled down some of my old Perl, Pascal, and Visual Basic code from high school. While it was nowhere near as complex as the code-base I was working with, it was also nowhere near as ugly and “hackish”. Part of that scares me a bit because I know that in high school and in my “pre-college” days I would never setting for “hackish”. My code had to work and it had to work well, but in my first few weeks at work I noticed that I was willing to settle for “good enough”.

Well I am quite happy to say that a number of my “good enough” projects have been fixed (and in some cases, rebuilt) and have been somewhat brought up to my old standard. I am hoping that in the coming months I can continue to improve as a programmer and never allow myself to settle for “good enough” again.

If you like this post, then consider buying me a beer!

As some of you know I began my search for a house a few months ago, and while I found a few houses I liked many of the financial aspects just didn’t come together. Honestly I had expected that to happen at some level but I just had to see how far along I could get in the home buying process. As well, life goes on.

Shortly after the housing situation ended I could tell that my relationship with my parents began to break down. We were fighting more often, I was angry all the time, and I just wasn’t happy. One day at work I just looked at my whole life situation and decided that I was getting out one way or another. I did some searching online and I found a decent apartment on paper. I went out to take a look at it and it wasn’t half bad, within a week I had signed the lease and moved in. While I have next to no money right now (damn moving expenses) I still feel much happier than I did living with my parents. Once I get a little more moved in I’ll take some pictures of my new place in all it’s goodness.

If you like this post, then consider buying me a beer!

All through my life I have had some rather different views on items that most people look at as either black or white. For example, many children have a tendency to either fully accept or reject the religion of their parents if they are brought up under a particular faith. For me personally I did both, by accepting Christian morals and teachings while throwing out the notion of there only being one “correct” religion. For a very long time I feel as though I had made people wonder what exactly I believe in, and for a very long time I have delighted in making them guess. Many Christians see me as agnostic and many agnostics see me as Christian, and while in a way they are both correct, I would much rather consider that neither of them are. Religion is what you make of it, and any who believe otherwise simply know nothing of religion to begin with.

Politics on the other hand are just as amusing. Both of my parents are democrats, and I am registered democrat but I personally believe that the whole political parties are completely messed up. Democracy is supposed to be a government of the people many issues that America as a people may agree on will never be passed through because it is not in the politician’s best interests. One of the biggest problems in this country is that many people simply do not have their own political views because they believe that politics are black and white. They will blindly follow whatever party the claim to be affiliated with and do not step forward and ask any questions. THIS IS NOT DEMOCRACY, THIS IS A SHEPARD HERDING A FLOCK OF VERY STUPID SHEEP. Come on people, the reason America is in such a bad condition is because people seem to have no interest in the important issues. The day I realized this was the day Newsweek featured an fairly massive story on Losing Afghanistan as it’s cover story in just about every country but the United States. What did we get in America you ask? Well, we got an cover story about some worthless celebrities “Life In Pictures”.

How much longer will this once great country survive in it’s ever deterioration condition?

If you like this post, then consider buying me a beer!

Older Posts »