Course Assignments

My Almost Completed Second WordPress Theme

I have been making this second Worldpress theme for the final project in my CMS class. Compare to the first theme I made, it has more customized features such as custom background and custom navigation menu, the design of the theme is more complex, and the theme layout becomes more standard. Here is an overview of its features copied from this page:

  • Custom background
  • Custom menu (only allow one level of drop-down menu)
  • Three widget areas
    • Left widget area – default widget is categories if no widget is set
    • Right widget area – default is the search form
    • Bottom widget area – default is tag cloud
  • Show category and tag list at the end of each post, display a message  if there is no category / tag assigned
  • Custom 404 page
  • Display 10 recent posts under each post

For more information on each of the above features, and to see the code snippets for them, read the original post.

This theme is still incomplete because I need to add the comments area on the post page, as well as some design changes. The deadline for this final project is next Thursday, July 22. I still have some time to finish up.

I will post the final progress of the theme and its source code when it’s completed.

Google Maps API and ColdFusion

While I was experimenting with Google API for my CA 288 ColdFusion class final project, I discovered that the ColdFusion variables can be used within the script tag. So I tried it out. I put the cfoutput tag outside the script tag in order to use the ColdFusion variables inside the script tag. And then I added cfif statement inside script tag to set the default latitude and longitude values to New York if there is no URL variables. Here is the cfelse part of the code:

var co_lat=#URL.a#;
var co_long=#URL.o#;
initialLocation = new google.maps.LatLng(co_lat,co_long);
var marker = new google.maps.Marker({
position: initialLocation,
map: map
});
contentString = "<br />#URL.d#:" + "<br />" + "Latitude: " + co_lat + "<br />" + "Longitude: " + co_long;
map.setCenter(initialLocation);
infowindow.setContent(contentString);
infowindow.setPosition(initialLocation);
infowindow.open(map);

I also used ColdFusion variables to convert them into the JavaScript variables as you can see on the first two lines of code. I also added a marker to the map to show the accurate location for the data provided. This is best used for creating maps from a list of places. For usage example, go to http://160.253.0.40/Students/Students/0111/browserGeo.cfm?d=Addo%20Entrance&a=-33.44431&o=25.73872. To download the source code, visit http://code.google.com/p/ca288-final-spring-2010-mc/downloads/list

PHP Form Validation Tip

PHP

While doing the assignment for my CA 282 (http://zebra0.com/Chen/?project=3) PHP class today, I came up with a trick to minimize the amount of code that I need to write for the validation rule of each form element. This trick especially best used for validating forms with lots of form fields. The following is the PHP code:

if (isset($_GET['submitted'])) {
  $field="username,password,pwconfirm,fname,lname,address,city,state,zip,email";
  $fieldName="User Name,Password,Confirm Password,First Name,Last Name,Home Address,City,State,Zip,Email";
  $field = split(",",$field);
  $fieldNm=split(",",$fieldName);
  for ($i=0;$i<10;$i++) {
    if (empty($_POST[$field[$i]])) {
      $errorMsg .= $fieldNm[$i]." is required.<br />";
      $success .= "False";
    }
    else {
      $success .= "True";
    }
  }
  if (!strstr($success,"False")) {
    $errorMsg="The form successfully submitted.";
  }
}
else {
  $errorMsg = "";
}
echo "<div id="noVal">".$errorMsg."</div>";

As you can see from the code above, I use split function to convert the combined form field names and the actual name for each form field to arrays. I then used a for loop to loop between form fields to validate the form. This is a simple form. It only checks all the form fields that left blank. You can use continue command inside the for loop for certain form fields that don’t need to be validated. You can also use email validation and password=password_confirm validation inside the for loop. Also note that the noVal id at the end of above code is just used to display the error massages to red color and bold.

The ColdFusion website is now fully data-driven

I learned a way to write code for the menus by using database instead of using cfswich. Now the course website is completely database-driven. I used the CMS approach to redesign the links in each menu. Here is the entire code for the content page that corresponce to the menu items:

<cfquery name=”qGetURL” datasource=”DSN0111″>
SELECT *
FROM tblMenu
WHERE 0=0 AND
<cfif isDefined(“URL.projectid”)>
menuID = #URL.projectid#;
<cfelseif isDefined(“URL.pageid”)>
menuID = #URL.pageid#;
<cfelse>
menuID = 18;
</cfif>
</cfquery>

<cfoutput>
<cfset filePath = ExpandPath(“#qGetURL.linkLoc#”)>
<cfif fileExists(filePath)>
<cfinclude template=”../#qGetURL.linkLoc#”>
<cfelse>
<div id=”noVal”>Not Found</div>
<cfinclude template=”index.cfm”>
</cfif>
</cfoutput>

As you can see from the above code, the file location and the file names are stored in the database instead of the page content. This way, I can create web pages inside site directories and then include it to the website through database without change the above code.

I also plan to create a ColdFusion web application that can directly update the database without launch MS Access. Right now I’m using CFEclipse to write the ColdFusion code on my Fedora system and virtualized Windows XP through VirtualBox to modify and viewing the database. My goal for this web application is to allow me to modify or view the database with less or no MS Access usage.

For those who don’t know, my ColdFusion course is Microsoft Access based, not MySQL based. I successfully installed ColdFusion 9 on my Fedora system, but I failed to install MS Access driver onto the system. That’s why I plan to create this application in the next few weeks.

Learning PHP Object Oriented Programming

Today I have learned about using Object Oriented Programming (or OOP) in PHP. It is very easy once you understand the structure of the OOP and have lots of practice on this technique. I learned this while I was looking over my Google Reader before my PHP class. I’m taking beginning PHP course this semester. This means that the OOP topic is not included in this course. You need to know basic PHP structures like functions and loops in order to understand OOP. Fortunately, I already self-learned PHP language during the end of year in 2009 through Lynda,com. So I tried to use OOP in the PHP script. At first. I think that I will practice more of this technique because this is new to me. I followed the simple tutorial on this website. After finishing the tutorial, I went to attend the PHP class. To me surprise, it had a test today. I happened to need more practice over the OOP. I took the second part of the test which was the hands-on test entirely using OOP. Here (http://zebra0.com/Chen/exam1.php) is the Object-oriented based test page. The following is one of the classes I created:

class todayDate {
  public $month;
  public $mesg;
  public function __construct($tdMon) {
    date_default_timezone_set("America/New_York");
    $tdDate = date($tdMon);
    $this->month=$tdDate;
    switch ($tdDate) {
      case "June":
      case "July":
      case "August":
        $this->mesg="Drive safely: school is in session";
      break;
      default: $this->mesg="Enjoy your vacation: Drive safely";
    }
  }
  public function getMonth() {
    return $this->month;
  }
  public function displayMsg() {
    return $this->mesg;
  }
}

I just learned the basics of Object Oriented Programming so I can only write these basic classes. As I learn more about OOP, I will improve my code for better readability and performance.

For more information and to learn more about Object Oriented Programming in PHP, visit this article on devarticles.com.

Successfully using ColdFusion array for the First Time Without Knowing Anything

I just improved my ColdFusion class website (http://160.253.0.40/Students/Students/0111/). I used more fileExists functions to automate the detection of the project files in the URL variables. Now I don’t have to worry about adding the projects to the cfswitch statement anymore. Here is the code snippet for one of my cfcase statements:

<cfcase value=”4″>
<cfif fileExists(filePath[4])>
<cfinclude template=”../projects/project4.cfm” />
<cfelse>
<div id=”noVal”>This project is not available yet!</div>
<cfinclude template=”index.cfm” />
</cfif>
</cfcase>

Noted that I used an array in the fileExists function. Here is the cfloop statement for the filePath array:

<cfloop from=”1″ to=”10″ index=”i”>
<cfset filePath[#i#] = ExpandPath(“projects/project#i#.cfm”)>
</cfloop>

As you can see, I used an array inside cfset statement to store 10 ExpandPath functions. This is my first time to use the array in a ColdFusion variable. In fact, I didn’t know anything about how to use ColdFusion array. I just applied the array based on the other programming languages that I have already known. According to course syllabus, we should learn ColdFusion array at later chapter.

At first, I used filePath#i# inside the loop to declare a regular ColdFusion variable. ColdFusion outputted an error stated that “ColdFusion was looking at the following text: #” . Then I tried the traditional method of adding an array by surrounding the loop variable with a pair of brackets. I was very surprised that it actually worked.

I applied the same technique to the title of each webpage so that I no longer forget about adding project title to the title.cfm when completed a project. Here is the code snippet for one of the cfcase statements:

<cfcase value=”7″><cfif fileExists(filePath[7])>#titleName[7]#<cfelse>#defaultTitle#</cfif></cfcase>

The defaultTitle variable is the default title on the site homepage.

The following are the additional updates for the site:

  • Projects are now re-organized
  • Project 1 has become homepage
  • Project 2 has changed to project 1
  • New project 2, Advanced SQL, was completed and added to the homepage

Computer Concepts Assignment Aid Web App released

Comparison between original and digitized version

Note:  This is an old post. It may contain links that no longer exists.

I finally completed the project and will be released under CS 110 homepage over robbychen.com soon. The following are some features of the web application (NOTE: This application is primarily made to help me to finish the assignments easier. However, anyone can use it if it helps to make life easier. This web app doesn’t contain any database-related function nor server/client log script, so you can safe to use it):

  • Each text field is auto expandable thanks to the autoResize (http://james.padolsey.com/javascript/jquery-plugin-autoresize/) jQuery plug-in.

Each text field will automatically expand once text in the text field reached the bottom of the text field.

  • The ability to add more rows using PHP.

The default number of rows is 20. Once you reached the last row at the bottom of the page, the Add a New Row button will appear. NOTE: this button will disappear if I don’t enter anything to any three of the text fields, it assumes that I had completed entering the data. After clicking the button, the page will refresh and it will scroll to the bottom of the page to letting me to enter data in the new row. I use the scrollTo jQuery plug-in to achieve this effect.

  • The option to print out the completed sheet after clicking the Submit button.

Once I clicked the Submit button, two buttons will appear on both top and bottom of the page, print button and edit button. It allows me to print out the completed sheet in order to hand in to the professor. The buttons on the page will not print out with the assignment sheet. I specified the print style sheet to hide the buttons while printing.

  • The option to edit the fields after it was submitted by clicking the Edit button.

The Edit button is simply using history.back() Javascript function to go back one page. I have not think of a way to achieve this without using database or session variables yet since my goal is to not leave any trace of the application on the computer and on the server.

Here is the side-to-side comparison between the original (non-digitized) version of the assignment sheet and digitized one:

Comparison between original and digitized versionComparison between original and digitized version. Click the image to enlarge

The following is the URL for this web app if you want to go to it directly:

http://www.robbychen.com/cs110/worksheet/

Changing Plan

I just found out this morning school is open today despite that today is President’s Day in the US. I have to postpone my project to Wednesday since I don’t have access to a scanner to scan the original assignment sheet. Right now I’m completing assignment for Computer Concepts class. I know the web app for helping me to complete this assignment is not finished yet. Since this assignment is due tomorrow, I have to complete this by hand. Besides, if I finished building the web app, I don’t have access to a printer to print out the assignment, and I have classes until ten o’clock on both evenings today and tomorrow.

So I decided to delay my web app “release” to Wednesday. Today I’ll complete my Computer Concepts assignment. The completion of the web app will be tomorrow. On Wednesday, I will scan the original assignment sheet to my laptop, compare between the web app and the original, and upload the project. As I wrote on previous post, it will be uploaded under the CS 110 homepage on robbychen.com website.

Creating my First Web App

I’m currently creating my first web application to aid my class assignment, specifically CS110 (Computer Concepts). The reason I came up with this web app idea is because there are 12 Chapters in the textbook for the course and we must answer the questions at the end of every chapter, but there is only one sheet of assignment paper. The professor told us to xerox some copies for future assignments. I thought this is a good idea to create a web app based on this sheet of paper. Originally, I planned to use scanner to scan it to my PC and directly working on it. Because there aren’t any good OCR tool available in GNU/Linux, as far as I know, and my writing is terrible, I decided to convert it to a web app.

Today I started creating the web app. I finished coding it using HTML and PHP and putted it to test. I used it to complete the Chapter 2 assignment. During the test, I was beginning to come up inspirational ideas, like auto change to the next text field in the same table column when it reaches maximum length and move the last incomplete word to the new text field like word processor. Unfortunately, I only knows little about JavaScript. I’m researching on how to do these. Maybe I will keep these ideas for my next project if I didn’t find anything on how to accomplish these.

I plan to complete the web app tomorrow. It will be “released” under CS 110 homepage on the robbychen.com website.