Riding on Rails

I’ve decided to spend less of my free time on blogging and more on learning new things. I had planned to change this site to run on WordPress, but have decided instead to learn Ruby on Rails.

From what I can see it is (in many ways) similar to CakePHP (which I love) but with more of a command line/linux approach. Also it’s a new and interesting language to learn, so I’m sure there will be some exciting challenges along the way.

I don’t know where this new road will take me, but if its anything like CakePHP, I’m sure it will be fun. (yes, only geeks find coding fun!)

In other (u)interesting news, the FCK Editor helper I wrote has been used as part of someone else’s cake-bake-enhanced project.

To be honest I think it’s kinda cool, my first CakePHP helper and people are actually finding it worthwhile…time to write some more…

Developing with CakePHP: Creating a simple admin control panel

If you are like me, you prefer to have a specific url mapped to your applications control panel/backend etc.
This is usually something along the lines of www.url.com/admin or www.url.com/cms.

Normally during the CakePHP “bake” process, the system will ask you what you want your admin route to be (default “admin”). Once this has been set you are merely 3 simple steps away from creating your own simple user control panel….

Step 1) Routing the admin url.
Add the following to your app/config/routes.php

Router::connect(‘/admin/’, array(‘controller’ => ‘admin’, ‘action’ => ‘index’));

This will route url.com/admin to the admin controller index function

Step 2) Creating the admin controller
Create the admin_controller.php file in /app/controllers/ and add the following code

class AdminController extends AppController
{
var $name = ‘Admin’;
var $uses = array(); //This controller does not need to use a model

function index(){

}
}

Step 3) Create your views
Create the app/views/admin/ directory and add an index.ctp view to it, with whaterver code you prefer (mine is merely a bunch of links to other controllers at this stage)

  • link(‘Posts’, ‘posts/’);
  • link(‘Comments’, ‘comments/’);

Create an array containing the days of the week

You could simply code:

$days_of_week = array(‘Monday’, ‘Tuesday’, ‘Wednesday’, ‘Thursday’, ‘Friday’, ‘Saturday’, ‘Sunday’);

but thats the “noob” way, a real coder creates a function…

function days_of_week(){
// declare days_of_week array
$days_of_week = array();

// get current date integer value (0 – 6)
$day = date(“w”);

// get current date values
$today = date(“j”);
$month = date(“n”);
$year = date(“Y”);

// get start day and end day of the week
$start_day = $today – $day;
$end_day = $start_day + 6;

// build days of the week array
for ($i = $start_day; $i <= $end_day; $i++) {
$days_of_week[] = date(“l”, mktime(0,0,1,$month,$i,$year));
}

// return array
return $days_of_week;
}

MySQL: filtering results from a list of id's

A short while ago I posted about testing if a variable exists in a list and I spoke about the MySQL “IN” keyword.

I realised this week that not everyone knows about the MySQL “IN” keyword, so I’ve decided to explain this very handy SQL keyword.

Lets say you have a user table with the following fields; user_id, username, password. You have a list of user ids (eg 1, 3, 5) and you want to draw the usernames and passwords for these users.

You could use the following SQL code:

SELECT `username`, `password` FROM `user`
WHERE `user_id` = 1 AND WHERE `user_id` = 3 AND WHERE `user_id` = 5;

Now while this is correct and will return the required results, it is quite a bit to code (especially if you have a larger list of user ids). Here comes the “IN” keyword to save us.

SELECT `username`, `password` FROM `user` WHERE `user_id` IN (1, 3, 5);

See how easy that is. Now if you have a much larger list (or the list grows) you simply add the user ids to the comma delimited list. Less time spent, and the same result. I’m all about the shortcuts….

Life of a developer

It has been a while since I have posted here, mainly due to the time consuming period that is the festive season and also because I am discovering that I am getting fairly annoyed by Drupal as a blog tool and am trying out the new WordPress as an alternative.

For now I have a small post on something that many people aren’t aware of but is VERY handy, the ternary operator.

Basically it is a shortcut to do a simple if-then-else statement in one line when you are performing fairly simple checks for a single value. I tend to use it a lot when checking for GET or POST variables.

So:

$getContents = (isset($_GET) ? $_GET : array());

would check if the $_GET variable array is set, if it is return its contents to $getContents and if not return and empty array to $getContents.

Much easier that coding:

$getContents = array();
if (isset($_GET)){
$getContents = $_GET;
}

P.S. Just on a side note, if you are new to PHP and want to use this code to check for and receive your $_GET contents, dont forget to sanatize your data as well (hmm I think I see a new post topic)

Delving deeper into the PHP lake…testing if a variable exists in a list

As a PHP developer of almost four years now, I often still come across little problems in coding that make me do the good ol Google search to find the answer. (someday I will have memorised the entire PHP manual, but today is not that day ;-).

Anyway today I was asked about how to do a IN LIST type comparison in PHP. (similar to the mysql IN statement).

A few google searches later revealed that there exists no such PHP command, however the in_array function can be used to perform the task:

$myArray = array(1, 2, 3, 4, 5);

if (in_array($var, $myArray)){
// do some code.
}

I guess one could also combine the two as follows:

if (in_array($var, array(1, 2, 3, 4, 5))){
// do some code.
}

I’ll have to test that one, but you get the idea…

Simple PHP increment function

Whenever I have to output a numbered list of data I usually run the following code to achieve the required result.

$counter = 0;
// for loop that does all the required code
$counter++;
// end for

but I have discovered a lovely little function that does all this for me.

function increment(&$counter){
$counter++;
return $counter
}

now I simply call

increment($whichever_counter)

and it automatically outputs the next numeric.

Great! Simple and easy to use…..that’s the way to code.

My first published article.

Something of a first for me. I recently started playing around with the CakePHP framework again. I have had some (little) experience with it in the past and with the launch of two CakePHP books I decided it was high time I learned how to use this awesome framework properly.

A few days later I was trying to find a way to integrate my favourite html editor into the framework itself and after a lot of googling, trial and error and a little luck I managed to find a simple, reusable way to integrate the editor into the framework. As this was very helpful to me I posted an article on the CakePHP bakery, and the chaps over there deemed it worthy enough to be published.

You can read it here . Please feel free to send me your comments.

I’m just kinda chuffed the published it. 😉