Tag Archive: PHP

Creating an even Simpler MVC Framework – Part 1

Creating an even Simpler MVC Framework – Part 1

Update: I rewrote the code for the framework and added page title generation as well, take a look at part 2.

After building and digging through the source of the simple MVC framework provided by Anant Garg, I was able to discover the true nature of the MVC framework. Actually, I’m able to modify his framework to an even simpler framework which just use View and a little Controller in the MVC (Model-View-Controller) with no database connectivity.

The primary use for the MVC pattern is to work with the database. However, sometimes we don’t need to use database to creating a web site. According to my own knowledge, the Model is used to interact with the database and the Controller uses logical to process the database.

I stated that I need to use “a little” Controller because it also controls which page to display and sends it to the View.

In this simple framework (and perhaps all of the MVC frameworks), the most important file is .htaccess which controls the URL. Here is the content of the file copied directly from Anant’s post:

Note that this .htaccess file needs to place in th root directory of your web site instead of public folder because we will be creating a framework that has no directory. You can create public folder or webroot folder if you want, but make sure to change the following code accordingly.

The above code converts the URL path to the url query. For example, the URL http://localhost/mvc/test would be converted to http://localhost/index.php?url=mvc/test.

Next step is to create the index.php file which used to process the url query:

This is what I called “reverse include”, because I had always been include header and footer manually on every page until I found CakePHP MVC framework.

I also stated that this framework will not have any folder. Well, to include header and footer, you need to create an includes folder to place all the repeatable scripts, unless you really want to be disorganized 🙂 .

Congratulation, you just created a mini framework. Note that this framework is used for small websites or personal sites where the database is not used at all. This means that it doesn’t have a back-end to edit the page directly on the server.

However, there is also an advantage to it. You don’t need to include the header and footer manually when creating a new page. It will include them automatically. It also can direct to the custom page if the specified page is not found.

About the title issue: I haven’t found a solution to set the title on individual page yet. Right now the title in the header is static. I will update this post once I find a solution to this issue.

Feel free to discuss this framework with me in the comment below.

Write CSS3 with Understandable Variables with LessPHP – Part 3


LessPHP allows you to use variables in your CSS stylesheet for the repeatable blocks of code.

In part 2, I provided you with the content of var.less. This part I will give you an example on how to use this .less file to write your own stylesheet. The provided example is based on the final project of this post on the Nettuts+.

First off, here is the folder structure for this example:

Example folder structure

Folder structure for the example

From top to bottom, they are:

css – folder

less – folder

lessc.inc.php in less folder – LessPHP compiler

main.less in less folder – source file to compile to CSS stylesheet

var.less in less folder – contains the LessPHP variables which I already provided it to you in part 2

index.php in css folder – contains the compile script which provided in part 1

main.css in css folder – contains the compiled CSS styles

index.php in root folder – contains the inclusion of main.css in css folder

Note that some of the code are directly copied from the above source. Also, in order to completely experience this example, you need to use Chrome or webkit compatible browsers.

Feel free to ask any question you have regarding the above code in the comment below.

New Blog Title, New Logo

Note: As this post was written long time ago, I’m currently use Mac OS on the MacBook Air. Until I find a stable way to run Ubuntu on my MBA, I will stick to use OS X.

As you already saw, I changed the title for this blog from “Robby Chen Personal Blog” to “Ubuntu Web Coder “as well as the logo.

One reason for changing the blog title is because I’m going to stick to Ubuntu, specifically Lucid Lynx (10.04.1), since I depend on some PPAs to get the updates. Maverick Meerkat (10.10) just doesn’t work for me. I will wait until Natty Narwhal (11.04) is out and see. If this still won’t work for me, I will stay with 10.04.1 and wait for the next release… Anyway, you get the idea.

As most of you who read this blog in the past know, I used to write code in Netbeans until I discovered the hidden potential of gedit for being such a good IDE. That’s why I abandoned Netbeans and replaced with gedit in the new logo. And I renamed blog title specifically to “coder” because I would like to try writing the code in each post from now on.

Besides the gedit logo in the new logo, I have also included two groups of logos, GNU/Linux and PHP. The GNU/Linux logos include Linux penguin, Chrome OS, Android, and Open Source. The PHP logos include PHP, CakePHP, and WordPress. I also included just released HTML5 logo created by W3C. I regularly use these tools and talked and will continue to talk about them in this blog. That’s why I included them in the logo.

What do you think about the new logo, blog title, and background color?

Optimize MySQL Database (Updated)

Update (04/17/2015): I know that I need to use VPS in addition to optimize MySQL tables for increasing WordPress speed. I will try Digital Ocean later this year. But for now, I will stick to my share hosting account until it expires at the end of this year.

For a long time since I created this blog, I had been struggled with the WordPress’ slowness. I sometimes wondered why are others’ WordPress so fast compared to mine. Then I blamed my hosting provider’s MySQL server not fast enough.

Until today, I googled for “improve mysql performance shared host” and found out about this article on optimizing MySQL performance. I logged into phpmyadmin on the server and look for the word “optimize”. At first, I was not able to find it. After carefully read the article, I found “optimize table” option under the “With selected:” drop down menu at the very bottom of Structure page.

Position of the "With selected:" menu

The arrow shows the location of the “With selected:” drop down menu which contains “Optimize table” option, click the image above to see the enlarge version.

After some experimentation, I was able to manipulate the same task in the command line (in my localhost of course, since ssh access is not available for my hosting account). The query for optimizing tables is easy:

Note that you need back stick operator (`) if you need to use SQL keywords as table name. For example,

Also note that SQL is case-insensitive in case you don’t know.

What do you think about this tip? Please leave your response below.

Download WordPress and Other files Directly to your Server with PHP copy



When I tried to get the development environment working with Chrome OS (CR-48), I thought since Chrome OS is primary working for the cloud, why am I downloading, editing, and testing the website files locally? I found out that my hosting provider, iPage, offers a file manager and a basic text editor to manage and edit my PHP files, and since I use gEdit as my main code editor, I’m pretty comfortable with the text editor.

Basic code editor

The basic code editor available in the control panel

I shortly found some useful file operation functions on the official PHP document, one of them is copy. According to its documentation, the source and destination can both be URLs since 4.3.0. This means that I can download the code from another website directly to my server using the server resources. I wrote a test page to test this function and it worked without problem.

The Code

Note that I’m just starting to learn HTML5, so I can’t guarantee the above syntax is correct.


If you have any questions regarding this script, leave a comment below.

A Very Nice PHP Array Technique


You properly already knew the following code:

This will contain an array of strings:

However, this is not very useful for multi-dimensional arrays where each array in a parent array contains the same keys and different values like the database array. This is where associative arrays come into play. The code below uses the same technique as above without using the array_push function.

Source Code


As you can see from the code, each array in the $strings array is copied into a new $data array. I can then use this newly copied array just after the end of second foreach loop to do something. For example, insert it into another database to change the database driver (MySQL to SQLite, for example).

Please share it in the comments below if you have any other uses for this technique.

Install Web Server with PHP on Ubuntu

Ubuntu Apache PHP


I recently noticed that some of you are not web developers, just general GNU/Linux users, especially through the wallpaper slideshow PHP script comments. Therefore, I decided to write a tutorial post dedicated to those who want to use my script but can’t open it through the browser.


Before I begin, I want to be clear that this tutorial uses command line interface to do all the stuff. If you don’t feel comfortable using the command line, then I suggest you to start using it more often. One more thing I want to mention is that unlike others, I like to use aptitude rather than apt-get because it offers more features and has detailed output during the operations. With these in mind, let’s begin the installation and setup tutorial:

  1. Open the terminal through Applications -> Accessories -> Terminal.
  2. Type

    into the command line, enter the password for your Ubuntu username, and start the installation. Note that by installing phpmyadmin, it will automatically install apache2, mysql-client, php5, and other required dependencies. However, mysql-server is not one of its dependencies.
  3. After the installation, it will ask you to setup mysql-server‘s password and phpmyadmin‘s password. Note that the first setup screen of phpmyadmin is ask you to enter mysql-server‘s password that you entered earlier. After this initial screen, you can whether enter a different phpmyadmin password or the same password as mysql-server. This password is not very important to me since it is used for remotely access phpmyadmin interface which I don’t use.
  4. Now you can access the web server by typing

    into the browser’s address bar to see the welcome page.
  5. Copy the scripts you want to run into the /var/www folder, delete the index.html inside this folder, and refresh the page:
  6. Now you can see the list of all the files available in the /var/www directory. You can click any of the PHP files and begin using the scripts.

Note that if you want to access the phpmyadmin interface, just go to

in your browser and type mysql-server password you setup earlier. As for the use of the phpmyadmin, you need to refer to its documentation.

If you have any issues following above instructions to install the web server or the PHP files are still not working, please feel free to leave a comment below. I will help out ASAP.

Get Filename without Extension using pathinfo PHP function

I recently have a need to import a list of images into the database for easier management. Because they are all PNGs and JPGs, I was trying to find a way to separate each image to filename and extension. I could use two explode() functions to take out PNG and JPG extensions. However, the file name could be thief.jpgTopng.png which use these functions will convert it to thiefTopng.This type of issue is very common at least to me. Thanks to one of the comments on this page, I discovered an useful PHP function for working with file names called pathinfo().

The usage of pathinfo is very easy compare to explode():

As you can see, I use exec() function to execute ls command to list all of the files inside the specified directory.

My First Regular Expression Statement


During my quest searching for more AJAX tutorial using jQuery, I found the jQuery history plugin. It enables the back button and manipulate browser history for AJAX applications. What I loved about this plugin is that the page with a hash sign at the end of the URL will automatically go to the page specified after the hash sign. For example, the URL http://localhost/index.php#products normally would ignoring the hash sign and go to the home page instead of products page. With history, this URL will go to the products page directly once it is entered into the address bar.

On the demo page of history plugin, I noticed a regular expression embedded in the code. I didn’t care about the expression very much at first because I thought I didn’t need it. But after experimenting with it, I came up with this jQuery code:

The regular expression in the last click event is the same as the one in the history demo page. According to regular-expressions.info, it means to remove all the characters before the hash sign along with the hash sign. For example,

After the regular expression used with replace function, the new value will be

The ^ character in the expression means to start with. The .* characters means any character. In the above code, ^.*# means to start with any character until the hash sign.

In the function ajax404() of the above code, I used the regular expression ^.*/ to get rid of all the characters before the backslash (/) sign along with the backslash sign. This produced the following result:

As the function name implies, I used this expression to display the page name when the file is not found. The above expression would produce the following error message:

instead of

if not used with the expression, which is not a good error message.

As with PHP and C, the forward slash (\) represents the escape character. In the code above, the normal usage of backslash (/) character is the same as the use of quotation marks in PHP. If used with escape character, it would output a normal backslash character. For example,

would produce

To learn more about regular expression, visit regular-expressions.info. And if you have any tips on how to work with regular expression, please share them in the comment below.