Delete part of the URL string using removeChars with ColdFusion
While I was doing the final project for my ColdFusion class today, I got one problem which the URL query keeps adding up when I was using CGI.QUERY_STRING. For example, the query string becomes “projectid=5&clear=yes” when I just added the “&clear=yes” to the end of the CGI.QUERY_STRING variable. After the page have been refreshed, the query would become “projectid=5&clear=yes&clear=yes”. This became a problem to me because not only the URL query was not right, but the result of the page was also not right.
After numerous trail of using cfif, I remembered that I could use a build-in ColdFusion function to remove the last part of URL query and store it to a variable to use it later. The name of the function is removeChars. It needs three parameters, a string, the position number of where to begin removing characters, and the position number of where to end the removing of characters. For example, I used the following statement to remove the “&clear=yes” characters at the end of the CGI.QUERY_STRING variable and stores it to a new variable called variables.QUERY_STRING:
<cfset variables.QUERY_STRING = removeChars("#CGI.QUERY_STRING#",12,11)>
Of course I would add a cfif statement to avoid continue removing the characters if there is no “clear=yes” exists in the string:
<cfif CGI.QUERY_STRING CONTAINS "Clear=yes">
<cfset variables.QUERY_STRING = removeChars("#CGI.QUERY_STRING#",12,11)>
<cfset variables.clear = "#CGI.QUERY_STRING#">
<cfelse>
<cfset variables.QUERY_STRING = "#CGI.QUERY_STRING#">
<cfset variables.clear = "#CGI.QUERY_STRING#&Clear=yes">
</cfif>
<a href="?#variables.QUERY_STRING#" title="Continue Shopping">Continue Shopping</a><br />
<a href="?#variables.clear#" title="Clear Cart">Clear Cart</a>
You can see result of the above code here along with my final project.
Create a DSN-less database connection using ColdFusion 8
For a long time, I had been searching around online to find a solution to switch to MySQL database for my ColdFusion school server from Microsoft Access database. Since I have no control over the ColdFusion Administration settings, I can’t change the DSN connection.
Recently I have been finishing my remaining undone projects. I use Railo open source CF server and MySQL under Ubuntu to do the assignments for the class. The SQL code I was writing is a bunch of aggregate functions:
SELECT Â b.BookId, Â Â Â b.BookTitle, Â s.SalePrice, Â Â Â Â Â Â COUNT(s.SalePrice) AS bcount, Â Â Â Â Â Â SUM(sd.TotalSale) AS ts, Â Â Â Â Â Â SUM(sd.Quantity) AS qt FROM tblBooks AS b, Â Â Â tblSales AS s, Â Â Â Â tblSalesDetail AS sd WHERE b.BookId=s.BookId AND s.SaleId=sd.SaleId AND s.SalePrice IS NOT NULL GROUP BY b.BookTitle ORDER BY #variables.ob# #URL.s#;
When I was testing these code on the localhost which is MySQL-powered, the page was rendered fine without any errors. However, once I uploaded to the school server which is powered by MS Access, the errors began to appear. Noticeably, I can’t use the name such as b.BookId, b.BookTitle, s.SalePrice without using aggregate functions. It struggled me a bit because the final is due tomorrow.
I then remembered my unsolved solution for connecting MySQL database dynamically through CF code because most of the DSN-less code online only works in CF 5. Once again I began to searching for the solution to release my struggle to the MS Access. Finally, I found a CF custom tag to do just that. It is called query, similar to cfquery. Since it’s a custom tag, the use of it is cf_query. This tag is primary used to connect to the MS SQL server by its author. Fortunately he has given a code snippet to dump out the available JDBC drivers on the server.
After I tested this code, I was surprised to see that my school server supports lots of databases that I don’t know about. I chose the MySQL one and copied its JDBC URL to the CF test page. I noticed that I was unable to connect to my own MySQL server on this web server since my hosting provider doesn’t allow remote connection to MySQL. Therefore I googled for “free mysql hosting” and found a website called FreeSQL.org that specifically for hosting free MySQL databases. I connect to this newly created database by using the cf_query code:
<cf_query jdbcURL="jdbc:mysql://www.freesql.org/ColdFusion" username="username" password="password" qName="qGetData">
Note that ColdFusion is the database name. Within the cf_query beginning and ending tag, I wrote the SQL statements to import the data since the phpMyAdmin link does not work on FreeSQL.org. After that, I replaced the above cf_query code with cfquery in the project that I struggled with.
You can download the above code here along with the custom tag and the original code from its author. The project page that I was struggled with can be found here.
Manage files using PHP on Linux Server
I have been playing around with my Ubuntu Server recently and found out that I have to use SSH each time I need to use the server. This remained me of PHP backtick operator, which has the ability to execute Linux command using PHP. This means that I can write a PHP script to manage the files on my server using Linux commands that I already knew without SSH.
Since I can’t connect my server to the outside world because of Verizon, I don’t have any security risk to change the files permissions on my server to 777. I then wrote the following code to show the list of files/directories under the web server root:
$command = `ls`;
$list = explode("n", $command);
$lslist = "";
foreach ($list as $lsLx) {
   echo $lsLx . "<br />";
}
Here is the code I wrote to use different parameters of li command. For instance, li -a shows all of the files/directories including the hidden ones and li -l shows the long list format.
I want to write a web UI to use with FFmpeg since I use it most of time on my server to convert videos to ogv format. The above code is just my concept of how to use PHP form to execute Linux command dynamically. It enabled me to use it as a base of designing the Web User Interface for Linux commands.
Calculate the degrees from the given Data to draw PHP Graph
During the final exam for my PHP class today, I encountered a math problem that I learned long time ago. The problem is to draw a chart based on the data from the database in order to show the ratio between men and women on a pie chart. The number of men represented as blue and red represent as women. I thought that it was as simple as draw two arcs using imagefilledarc function. However, when I noticed that the data needs to be in degrees in order to use this function. I totally forget how to convert a number to a degree since I haven’t been practice math for two years. Thanks to Google, I learned how to do the conversion on Yahoo Answers. The formula that I came up with is the degree of each men and women equals the number of men or women divide by total number of men and women multiply by 360. Therefore I wrote the following code:
<?php
   $image = imagecreatetruecolor(300, 300);
   $blue = imagecolorallocate($image, 0, 0, 255);
   $red = imagecolorallocate($image, 255, 0, 0);
   $white = imagecolorallocate($image, 255, 255, 255);
   imagefill($image, 0, 0, $white);
$male = $_GET['m'];
   $female = $_GET['f'];
$total = $male + $female;
   $maleDegree = $male / $total * 360;
   $femaleDegree = $female / $total * 360;
imagefilledarc($image, 130, 130, 200, 200, $femaleDegree-270, $maleDegree-90, $blue, IMG_ARC_EDGED);
   imagefilledarc($image, 120, 130, 190, 190, $maleDegree-90, $femaleDegree-270, $red, IMG_ARC_EDGED);
imagepng($image);
   imagedestroy($image);
?>
The f and m variables are URL variables, which means I can pass the number of males and females inside the normal img tag:
<img src="3_image.php?m=<?php echo $numMale; ?>&f=<?php echo $numFemale; ?>" alt="Problem 3 - Graph" />
You can download the source code here to experiment with it. There is a MySQLÂ database file inside the downloaded archive, which it contains the needed MEMBERS table for this script.
My hatred of Microsoft and Adobe as a GNU/Linux user and a web developer
I had used Dreamweaver, Flash, Fireworks, and other Adobe products long before Adobe acquired Macromedia. At that time, I didn’t know the existence of GNU/Linux and open source communities. I didn’t even decide on my career as a web developer yet. Thanks to Microsoft’s announcement of Windows Vista, I decided to look for other Operating Systems beyond Windows that are able to run on the older hardware eternally (by the way, I didn’t know other OSes besides Windows back then), meaning that the future version of OS will still be able to work on old hardware. I found GNU/Linux, specifically Ubuntu.
After one year of switching back and forth between GNU/Linux and Windows, I finally made the decision to stick to GNU/Linux. However, I still would test the browser compatibility in IE through VirtualBox. And here comes my hatred of MSIE. During the course of my web development, I’ve read numerous articles about how Microsoft doesn’t follow the W3C standards for their Internet Explorer to make web developers suffer from browser incompatibility issue. I had enough struggle for the Internet Explorer, I decided to use some jQuery to encourage those who view my website to ditch their Internet Explorer.
I have become to hate Adobe since the day I switched to the Eclipse and NetBeans IDE to develop my projects. After Adobe announced that Flash Builder and ColdFusion Builder are based on the Eclipse, I was excited to know that Adobe finally makes their development tools available on all of the major OSes. It disappointed me when I went to download the beta version of these two builders, it only offers Windows and Mac versions, no GNU/Linux version anywhere. Since Eclipse uses its own EPL license instead of GPL license to govern the use of Eclipse, Adobe can make modification to its code and sell it as their own products. This doesn’t mean that Adobe can discriminate against GNU/Linux users from using their products since Adobe uses Linux on their Adobe.com website.
As an opposite effect, these hatreds actually make me become more involved with open source technologies, such as HTML 5 and Ogg theora. I use open source ColdFusion engine Railo to develop ColdFusion websites as well. I think that ActionScript will be replaced by jQuery and other JavaScript frameworks and FLV will be replaced by either H264 or Ogg Theora depending on which side will win the online video wars.
Use Array to Populate the form with PHP
When I was doing my practice final for the PHP class today, I came across several problems for easier way to populate the form, especially with check box and option form controls. These two controls need to be repeated each time when new items were added to the list. Of course I could copy and paste the previous statement and edit it accordingly, but I want to make it easier by just input the new items into the appropriate arrays. Here is what I did to the multiple selection control:
<select multiple size="5" name="skills[]" id="Skills">
   <?php
     $selected = new prefilll();
     $webprogramming = array("AJAX","ColdFusion","HTML 5","JavaScript","JSP","PHP");
     $computerprogramming = array("Basic","C/C++","Java","Linux");
     $skillset = array("Web Programming"=>$webprogramming,"Computer Programming"=>$computerprogramming);
     foreach($skillset as $skillNum=>$skillsets) {
       echo "<optgroup label="$skillNum">n";
       foreach($skillsets as $programmingSkill) {
         echo "<option";
         echo $selected->selected("skills",$programmingSkill);
         echo " value="$programmingSkill">{$programmingSkill}</option>n";
       }
       echo "</optgroup>n";
     }
   ?>
</select>
And here is the check box control:
<?php
   $counter = 0;
   $checked = new prefilll();
   $jobtypes = array("full"=>"Full Time","part"=>"Part Time","weekdays"=>"Weekdays","weekends"=>"Weekends");
   foreach($jobtypes as $labelFor=>$values) {
     $counter++;
     echo "<input type='checkbox' name='jobtypes[]' value='{$values}' id='{$labelFor}'" . $checked->checked("jobtypes", $values) . " />n";
     echo "<label for='{$labelFor}'>{$values}</label>n";
     if ($counter == sizeof($jobtypes) / 2) {
       echo "<br />n";
     }
   }
?>
Note that I used a class in the above code segments, prefill. This class is used to reselect the items in the check box and option controls if there is a validation error.
Following is the code for the prefill class:
class prefilll{
   function selected($items,$Item) {
     if (isset($_POST[$items])) {
       $items = $_POST[$items];
       if (in_array($Item, $items)) {
           $Selected = " selected";
         }
       else {
         $Selected = "";
       }
     }
     else {
       $Selected = "";
     }
     return $Selected;
   }
function checked($items,$Item) {
     if (isset($_POST[$items])) {
       $items = $_POST[$items];
       if (in_array($Item, $items)) {
           $checked = " checked";
         }
       else {
         $checked = "";
       }
     }
     else {
       $checked = "";
     }
     return $checked;
   }
}
You can download the above source code from here along with the whole source file if you want to experiment with it yourself.
Playing Around with PHP URL Extraction
I discovered today that there is a feature in PHP that can grab current URL through $_SERVER['REQUEST_URI'] and convert it into an array using explode function. I think this is extremely useful to me. Partly because I always wanted to have an URL that is similar to the URL for this blog. Another reason is that I don’t have to edit my sever configuration either directly or through PHP to achieve the same result. The method I use to create an URL that only contains the folder of the page, that is, the page is index.php, might be useful for small websites that have less than fifty web pages or possibly less. I didn’t test it on a large set of web pages because I just thought about using this type of URL format in the final project for my CA 282 class. It is a four-page project, therefore I cannot guarantee that it can work on a larger set of web pages. I personally think that it can become disorganized and confused once I have a little more web pages by using this method. Here is the code if you want to test it. Basically, it includes all the files inside the main index page based on the value at the last slash of URL, e.x. http://www.example.com/folder/item :
$currentURL = $_SERVER['REQUEST_URI'];
$URLpart = explode("/", $currentURL);
$endURL = $URLpart[sizeof($URLpart)-2];
switch ($endURL) {
case 'signup': // http://zebra0.com/Chen/final/account/signup/
include_once 'includes/signup.php';
break;
case 'login': // http://zebra0.com/Chen/final/account/login/
include_once 'includes/login.php';
break;
case 'account': // http://zebra0.com/Chen/final/account/
include_once 'includes/account.php';
break;
case 'signout': // http://zebra0.com/Chen/final/account/signout/
include_once 'includes/signout.php';
break;
default:
include_once 'includes/index.php';
}
And the index.php file inside each of the folder simply include the main index.php at the root of the project folder. Because of the nature of the included file, I used the dynamic path using PHP $_SERVER array to generate an absolute URL for the links and style sheets instead of the static ones.
The Performance of This Blog Has Been Improved
Today I played around with WordPress settings on this blog in order to improve the performance and loading speed of the blog. I have been noticed about the slow loading speed since this blog was created. Because of the lack of free time, I didn’t have any opportunity to investigate and optimize the blog. Since I had free time today, I decided to looking for ways to improve the performance of my blog. I first disabled and deleted most of the plugins used on the blog that I think it doesn’t have any usefulness. I also noticed that some of the plugins that I used are not compatible with the newest version of WordPress. I believed these plugins are the main cause of the slowness of the blog. After I deleted these plugins, I felt the blog loads significantly faster.
Despite that the blog has been improved through deleting the plugins, I still would like to install as much plugins as I like. If you know any technique on how to install plugins without effecting the performance of the WordPress blog, please let me know by leaving a comment below. Thanks.