Tutorials Category

PHP Class for Date Time calculations - Part 2

Monday, July 21st, 2008

In my previous posting I touched on a PHP class called DateClass (by Steve Powell) that I downloaded from phpclasses.org. The DateClass package actually contained 2 separate classes. One for manipulating dates and the other for date spans.

This post will cover how to use the DateSpanClass to easily calculate the difference between 2 dates, in intervals from seconds to years. Sadly, however, the class have some bugs which needs to be fixed before it can be used.

(more…)

PHP Class for Date Time calculations - Part 1

Monday, July 14th, 2008

Performing data/time calculation is basic requirement in any programming project. No serious application can hide from it, so thankfully PHP itself comes with some very useful data/time functions. But, I feel that those PHP date/time functions are probably great for data/time display and formatting, but not so much for calculating for things like the beginning/end of the week, the number of seconds between two dates etc.

Again thankfully, after a visit to phpclasses.org, I found a nifty little class written by Steve Powell (way back in 2004) called DateClass which did what I wanted. Unfortunately, the documentation was only a class reference sheet, and doesn’t come with any examples to quickly get things going.

(more…)

A look at the Google Graph class

Tuesday, June 3rd, 2008

In my previous post about creating simple graphs using Google Charts API, I mentioned about a PHP class, called Google Graph, which Ryon Sherman wrote that encapsulates this API, making it easier to code in PHP.

I incorporated this class in my CakePHP project, and found that there were a couple of problems with it.

(more…)

CakePHP: Using scaffolding for rapid application building

Tuesday, May 20th, 2008

ScaffoldingThat’s right, I’m still on my CakePHP journey. I’ve been spending quite a bit of time now trying to match what I want to build with how to go about doing it in CakePHP. We all know that the blog example is pretty simplistic - a real world app won’t be quite so straightforward. At the moment, I’m trying to build up an intranet of sorts, with lots of different independant apps, and it’s driving me crazy… How would I structure the it? Should I use ‘plugins’? The folders, the views etc. Yes, it’s all still a big mess.

In any case, I found that CakePHP has a really nifty feature that really helps with sorting out the mess, and it’s called “scaffolding”.

(more…)

CakePHP: Working with ‘Associations’

Thursday, May 8th, 2008

This is probably going to be the first of many postings of my exploration with CakePHP. This post will briefly look at the CakePHP’s ‘Associations’ feature. Associations is “the relational mapping provided by the [CakePHP] model”.

In non-Cake speak, we’re talking about standard SQL joins - the mapping of relations between SQL tables. As you may know, doing SQL joins can be quite messy with unwieldy SQL strings. Thankfully, CakePHP provides a very simple way of joining tables.

(more…)

Creating simple graphs using Google Charts API

Thursday, April 24th, 2008

Thinking back a fews years ago, I was involved in building some reporting modules with graphs and charts to show our customer utilisation of our data center. After trying a number of different open source graphing packages for PHP, I finally found JpGraph, which I daresay is probably the best graphing package available for PHP. (In my experience anyway.)

However, JpGraph requires other dependencies like GD libs - the kind of thing which we may not always have control over if we host with web hosting providers. Thankfully, there’s now a simple and easy way of building simple good looking graphs with just an <img> tag.

(more…)

Using regular expressions to extract content

Tuesday, April 22nd, 2008

PHP provides a number of really neat regular expression functions. You can find the list of the regex function at the PHP site.

But the one that I’ve had most fun with is the preg_match_all() function which I’ve been using to do content extraction from an HTML page.

(more…)

Parsing a URL querystring into variables in PHP

Thursday, April 17th, 2008

It’s common knowledge you can read variable from a URL querystring by using $_GET array in PHP, but that’s only possible if the URL is “executed” in the browser.

For those who might not know, querystrings are those variable-value pairs that appears behind ‘?’ of a URL. For example,

http://www.mysite.com/index.php?variable1=1&variable2=1

gives the querystring “variable1=1&variable2=1″.

What happens if you end up reading a URL from, say, a text file and you want to parse the querystring? In this case, the $_GET won’t work. So instead, PHP provides a function called parse_str(), which will convert the querystring into actual PHP variables within the scope of the code.

(more…)

Checking a string is alphanumeric in PHP

Friday, April 11th, 2008

There may be times when you need a way to check that a string only contains alpha-numeric characters. That means only alphabets A-Z nad numbers 0-9.

Most PHP-ers will probably use regular expressions and the function preg_match(). I will admit that regular expressions is really powerful, but it’s a real pain to learn and master. Fortunately PHP provides another function you can use.

Check out ctype_alnum() function.

(more…)

PHP Operators: Assignment

Monday, February 11th, 2008

The main assignment operator is ‘=’, which basically assigns a value on the right to a variable on the left. If you think this is the same as an equal sign in mathematics, don’t! Think of it as a ‘assigned to’ so whatever is on the right is assigned to the left.

Here’s a simple example:

(more…)

Google