Monthly Archives: November 2011

Regular Expressions – validating the NIC number

Regular expressions!!! kind of a scary thing for most developers.. 🙂 Well it is scary because its not something that is really user friendly for developers, its a combination of letters, special characters and numbers that kind of make our eyes go the wrong way when we look at them.

Well I normally don’t like to use regular expressions, I just copy from some web site and paste the regular expression when I really need to use one. But I needed to learn how to build my own regular expression and I couldn’t find one on the Internet. What a bummer that was 🙂

What I wanted to do was to validate a National Identity Card (NIC) number (Sri Lankan). Which is 9 number followed by one letter that is v, V, x or X.

So how to write the regular expression for this?

Well you start with

/^

that means that the regular expression is started.

Now we need to tell them that next nine characters should be numbers,

[0-9]

that is how you say numbers 🙂 , how to tell them that next 9 digits are numbers???

[0-9]{9}

the number in curly brackets is the number of times to repeat.

Next we need to tell the regular expression that next letter should be x, X, v or V.

[xXvV]

and then we need to tell that the regular expression is over. you do that by,

$/

The final version of the regular expression for validating the NIC number is,

/^[0-9]{9}[vVxX]$/

It is really easy when you break it down like that. 😀

How to hide the delete button on CGridView in Yii

Just a simple thing but hard to find on the Yii documentation.

just add the following code in the delete button definition section in CGridView implementation,

array(
‘class’=>’CButtonColumn’,
‘buttons’=>array(
‘update’ => array
(
‘url’=>’Yii::app()->createUrl(“accusee/update”, array(“id”=>$data->id))’,
),
‘view’ => array
(
‘url’=>’Yii::app()->createUrl(“accusee/view”, array(“id”=>$data->id))’,
),
‘delete’ => array
(
‘url’=>’Yii::app()->createUrl(“accusee/delete”, array(“id”=>$data->id))’,
 ‘visible’=>’false’,
),
),
),

What to consider when choosing a PHP Framework (For Newbies)

There are numerous PHP frameworks out there. Thats one reason i switched to PHP after doing Java for 3 years. PHP has more fun frameworks available that are easy to learn and implement.

When it comes to PHP frameworks there’s Zend, Symphony, CakePHP, Code Igniter and of course Yii framework.

You can also go with the mainstream and learn zend, but for me its too much for smaller quick projects. All these different frameworks has many plus points and their equal share of weak points. some are good for enterprise projects and some are good for small scale web based systems.

Here i have listed some points that i think are important when selecting a framework,

  • Ease of learning. How easy its to learn the basics? what is the underlying technology?
  • Project Scale – is it a project that should be implemented very quickly? a project that would have hundreds of users or one or two?
  • How easy it is to quickly define our logical design in the framework? how to match our database schema with framework elements? does the framework use MVC?
  • The learning materials available. There are some good frameworks but it doesnt have any E-books or any tutorials explaining how we can use the framework. for me this is a huge point, when i was trying to learn CakePHP I found that examples that details common day to day use of codings and methods are scarce compared to Yii framework. And if there is an easy follow documentation and tutorials plus an ebook then your life would be very easier when learning that framework.
  • because ultimately your aim is not learning a framework but using it to develop a useful system that matches the requirements.
  • How easy it is to host your finished product? does the framework require you to have some crazy obscure PHP options enabled on the server? It might be ok if your going to host the web site on your own but if your thinking about hosting it somewhere else this is a huge concern.
  • Tools available for automatic code and architecture generation. some tools are there that are hard to learn and needs lots of resources to run. so better clear this up in the beginning.
  • Additional functionality available? Yii’s collection of extentions has made my life a lot easier, so when your selecting a framework its good to check up on its extensibility using already pre-coded extensions. after all you don’t want to reinvent the wheel right?
these are some of the concerns i had when choosing a good PHP framework. let me know yours. for me the ultimate framework for small projects is Yii. And im trying to learn zend, will see how that works out. 🙂

iRaid and Yii Framework

What is iRaid?

iRaid is a project that I’m doing at e-services Lanka. Its a system that is being developed for Sri Lanka Consumer Affairs Authority to manage their Raiding operations.

As this system is web based any information that is being updated to the system by district offices are immediately available to the head office.

This helps to streamline their operations and also to track and update all raid related information efficiently and productively. By implementation of this system the paper trail used by the current manual system can be cut upto 80%.

The Technology behind iRaid.

Yii Framework – mainly the system was developed by me using the Yii framework. I was trying to develop using cakephp but found that Yii framework is much more easier to learn and develop. I managed to learn the basics of the framework and complete upto about 90% of the system in less than a month. (Project was scheduled for 3 months). And also the numerous extensions available for Yii is also helping to minimize the development headaches that are normally there.

with its inbuilt CRUD operations and caching mechanisms development and implementation speed has increased.

Yii is fully based on MVC architecture and by using Gii tool provided we can easily create Models, Controllers and Views.

PHP5 – the coding is fully object oriented and used PHP5.

MySQL – MySql was used to create the database and has been really easy to do changes and implement in multiple server environments.

Some Screen shots of the system.

Let me know if you want to know about how to use Yii Framework in your projects. Its easy to learn and fun to code. 🙂