Drupal Forms

Written by James Mansson on February 9, 2013 Categories: AJAX, Drupal, HTML

Because of Drupal’s structured nature it is not immediately obvious how to implement forms in it. In this post I am going to look at how I used Drupal’s various built-in functions to implement a form used in one of my Drupal modules. (more…)

No Comments on Drupal Forms

Rendering HTML Tables Dynamically in Drupal

Written by James Mansson on February 2, 2013 Categories: Drupal, HTML

One task that can be cumbersome is creating HTML tables dynamically. Fortunately, when working with Drupal, there is a built-in function to help with this called theme_table. There are two basic ways to invoke this function: either you can call theme_table directly, or use the general theme function, and pass ‘table’ as the first parameter, and the additional settingsĀ as an array in the second parameter. The follow code will feature the second method. (more…)

No Comments on Rendering HTML Tables Dynamically in Drupal

Using the Zend Framework Db_RecordExists and Db_NoRecordExists Validators

Written by James Mansson on January 29, 2013 Categories: Databases, Zend Framework 1

When validating data submitted via a form on a web page, a common task is to check whether certain fields are or are not in a particular database table. There are two basic scenarios:

  • The user is adding a new record. One of the fields entered will be the primary key of a new record in a table. Clearly that primary key should not already be in the table.
  • The user is selecting an existing record. One of the fields entered is the primary key of an existing record in a table. Clearly that primary key should already be in the table.

The Zend Framework provides two validators to handle these scenarios: Db_NoRecordExists and Db_RecordExists. (more…)

No Comments on Using the Zend Framework Db_RecordExists and Db_NoRecordExists Validators

Adding Stylesheets to a Drupal Module

Written by James Mansson on January 25, 2013 Categories: CSS, Drupal

Although I have found Drupal to be a powerful means of creating content-driven websites, it can be frustrating in that the way to do certain things is neither obvious nor well-documented. This, I think, a consequence of the desired to hide the details of the implementation, and to allow users to concentrate on creating site content. While this is certainly desirable for routine tasks, there are occasions when you need to control things at a lower level, and in those circumstances, it can be hard to see how you do what you want to do. (more…)

2 Comments on Adding Stylesheets to a Drupal Module

Alternating Row Colours in a WPF ListView

Written by James Mansson on January 23, 2013 Categories: WPF

One common technique for making tabular data easier to read is to alternate the colour of data rows. I recently needed to add this effect to ListViews in a Windows application I had written using WPF. (more…)

No Comments on Alternating Row Colours in a WPF ListView

Changing the Format of a Date in PHP

Written by James Mansson on January 18, 2013 Categories: PHP

Sometimes you need to change the format of a date that is in text format. For instance, I recently needed to take a text string containing the date and time of an event, and only display the date part. As the text string was originally generated using the PHP date function, it was easy to do this with a combination of strtotime and date.

The text string was originally generated like this:

$date = date("F j, Y, g:i a");

I stripped out the time part as follows:

$ts = strtotime($date);
$date = date('F j, Y', $ts);

This left me with just the date.

No Comments on Changing the Format of a Date in PHP

Disabling Link Detection on Android and iPhone

Written by James Mansson on January 14, 2013 Categories: HTML, Mobile

Android and iPhone devices automatically attempt to detect text in HTML that corresponds to phone numbers, and turn this text into a link that calls that number. While this can be a useful feature, there will be occasions when you want to disable this feature. This can be achieved simply in HTML by adding the following in the page head section:

<meta name="format-detection" content="telephone=no" />

You can still enable linking of specific parts of the markup on the page by explicitly placing a link around that element, and using the tel protocol for the href value:

<a href="tel://01234567890"><img src="phone.png" /></a>

This is especially useful for graphical elements (e.g. an image of a phone) that wouldn’t be automatically detected.

No Comments on Disabling Link Detection on Android and iPhone

Using CSS gradients

Written by James Mansson on January 11, 2013 Categories: CSS3

I’m currently working on a web page where the background colour of some sections is a gradient between two colours. I’ve found the Mozilla Developer Network article Using CSS gradients to be very helpful in achieving this effect.

No Comments on Using CSS gradients

Retrieving the Primary Key of a Newly Inserted Row using Zend_Db_Table

Written by James Mansson on January 5, 2013 Categories: Databases, Zend Framework 1

Database tables often have an auto-increment field that acts as primary key. Sometimes it is necessary to retrieve this field immediately, for instance when adding a related entry to another table in the database. Fortunately, Zend_Db_Table offers a simple way to access this field, as it is passed back as the return value from the insert function. For instance:

$id = $dbTable->insert($data);

The variable $data is an array containing the values of the record to insert as key-value pairs. It should not include the primary key if the field is auto-increment.

No Comments on Retrieving the Primary Key of a Newly Inserted Row using Zend_Db_Table

Performing SELECT COUNT using Zend_Db_Select

Written by James Mansson on January 3, 2013 Categories: Databases, Zend Framework 1

Following on from my previous post, another common variant on the SELECT statement is SELECT COUNT. For instance:

SELECT COUNT(*) FROM order WHERE status = 'ordered'

This statement returns the number of records in the order table where the status field is set to ordered.

To perform this query using Zend_Db_Select, we would use something like:

$select = $dbTable->select();
$select->from($dbTable, array('count(*) as c'));
$rows = $dbTable->fetchAll($select);
$count = $rows[0]->c;

Note that we alias the result of the account as c in order to make retrieval easier.

No Comments on Performing SELECT COUNT using Zend_Db_Select