Archive for the ‘PHP’ Category

Beware of the Trailing Comma in JavaScript Prototypes

on Wednesday 3rd December, 2008 Gabe speculated thusly…

Came accross the following post at:
http://www.pluralsight.com/community/blogs/fritz/archive/2007/06/19/47771.aspx

“I spent more time than I care to admit tracking down this one, perhaps this post will save someone else the trouble…

When defining a number of functions in a prototype in JavaScript, do not include a trailing comma after the last function:

MyType.prototype = {
    foo : function() {
          // ...
    },

    bar : function() {
        //...
    }, //< - fails in IE!
 }

What was especially tricky about tracking this problem down was that FireFox works with or without the trailing comma, so it only fails in IE!”

Posted in Books, Development, JavaScript, Linux, Operating System, PHP, Programming

No Comments »

HTML Helper for generating a country drop-down list in CodeIgniter

on Friday 9th May, 2008 Gabe speculated thusly…

Frustrated at having to create my own drop-down country lists in CI I created a helper for it. It can even work with the CI Validation class, so if the form is submitted and fails validation when it is redisplayed the same country is selected as the form was submitted with.

Download the file MY_form_helper.php and put it in the folder: /system/application/helpers/.

Download the file countries.php and place that in /system/application/config/.

You will now be able to create drop-down lists of countries in your views like this:
< ?=form_countries( 'country', $this->validation->country, array( ’style’ => ‘width: 250px’ ) )?>

Ensure that you create a validation rule called “country” in your controller and that you are loading the form helper.

form_countries() takes three arguments. The first is the name you would like to give the select box, this is required. The second is the country to select by default - this could be something like ‘GB’, or $this->validation->country, otherwise you can leave it blank. The third argument is any additional properties to give the select box, you can leave this blank, or pass an array. In the example above we also set the width to 250x. The country chosen by the user will be returned to the script as a two-letter ISO country code, if you are validating this then you can set the min and max length values as 2, and accept alphabetical characters only. If using MySQL as your database you will get best performance by creating a column of the type char with a length of 2.

Posted in CodeIgniter, Development, Frameworks, PHP, Programming

4 Comments »

Profiling in CodeIgniter

on Tuesday 6th May, 2008 Gabe speculated thusly…

Profiling an application is a great way to see how it is performing. CodeIgniter comes with a profiling class, which you can make calls to from your controllers. However, during development I find profiling so helpful that I want it on all my pages, without having to specifically call it each time. I also want to be able to deploy my development site to a production server - making calls to the profiler throughout the controllers means I would need to edit each and every controller to make sure the profiler wasn’t active on the live server.

After much searching I discovered a better solution. This allows you to add profiling to the bottom of every page (along with its debug info and SQL query info). When you copy the dev site live you just exclude 1 file, and profiling will be removed totally. All you need to do is create a file called MY_Output.php in system/application/libraries that extends the Output core class, with the following contents:

< ?php
# /system/application/libraries/MY_Output.php
if (!defined('BASEPATH')) exit('No direct script access allowed');
class MY_Output extends CI_Output {
function __construct() {
parent::__construct();
$this->enable_profiler( TRUE );
}
}
?>

This will enable profiling output on all your pages for development and debug purposes. When you copy your site to a production server just make sure you don’t copy MY_Output.php file. I normally use rsync for copying live and just add: --exclude="MY_Output.php" to the rsync command.

The condition to using the name MY_Output.php is that you left the variable $config['subclass_prefix'] in config.php as default. This defaults to $config['subclass_prefix'] = ‘MY_’;, so if you change it you will need to alter the filename of your new class.

Posted in CodeIgniter, Development, Frameworks, HowTo, PHP, Programming

No Comments »

Protected: Learning PHP 5 - O’Reilly

on Monday 10th March, 2008 Gabe speculated thusly…

This post is password protected. To view it please enter your password below:


Posted in Books, Information, PHP, Programming

Enter your password to view comments

Protected: Pro PHP Security

on Thursday 6th March, 2008 Gabe speculated thusly…

This post is password protected. To view it please enter your password below:


Posted in Books, Information, PHP, Programming

Enter your password to view comments

Protected: CodeIgniter for Rapid PHP Application development

on Thursday 6th March, 2008 Gabe speculated thusly…

This post is password protected. To view it please enter your password below:


Posted in Books, CodeIgniter, Frameworks, Information, PHP, Programming

Enter your password to view comments

Linking images in Code Igniter

on Sunday 17th February, 2008 Gabe speculated thusly…

I’m a big fan of the PHP framework, CodeIgniter. However, until recently it has lacked an elegant way of nesting an image in an anchor tag. I had to resort to manually typing out HTML like this:
<a href="<?=site_url?>/controller/action"><img src="<?=base_url()?>/images/button.gif"></a>
CodeIgniter 1.6 has been released, quickly followed by 1.6.1. This includes an image helper. One is still unable to elegantly link images using CI’s helpers. If you want to be able to do something like this:
<?= anchor( 'controller/action', img( 'images/button.gif' )?>

You could just comment out line 117 in /system/helpers/url_helper.php but that means you are altering CI core, which is not a good idea. For a start it will be overwritten when you upgrade and it is an inelegant hack.

A better bet is to create your own helper file and use it to override CodeIgniter’s one. This is dead-simple, just create a file called MY_url_helper.php in the following location /system/application/helpers/. Note that the MY_ prefix is set in the config.php file under $config['subclass_prefix'] = ‘MY_’; so if you have changed that setting you will need to adjust your file-name accordingly.

Then just copy and paste the following in to it: (more…)

Posted in CodeIgniter, Development, Frameworks, PHP, Programming

7 Comments »

Andrew Norman: The Book Collection

on Thursday 5th July, 2007 Gabe speculated thusly…

Andrew commissioned me to give his books an on-line presence. He wanted only to list his books and nothing else such as shopping or contact details.This post summarises, and then specifies the project. Designs are presented, and a project plan is demonstrated. Finally, the project is completed and I can make the source code available on request. (more…)

Posted in Development, MySQL, PHP, Programming

No Comments »