Strange Arrays PHP Puzzler: Dynamic typing, strings, and array indexes

May 20, 2009

Take a look at this snippet of code:

<?php
$a = array('first');
if (isset($a[0]['some string'])) {
  print "What?" . PHP_EOL;
}
?>

What does it do? By all appearances, it declares an array ($a) with one value in it (first). Then it checks to see if $a's first entry has an index some string.

If it...

Packt: Creating Our First Module using Drupal 6 (Part2)

May 19, 2009

Today Packt has published the second part in my series on creating a first Drupal 6 module. In this installment, I walk though the process of implementing hook_block(). Here's Packt's summary:

In the first part of this 2-part article series we had created a basic module that uses hook_block() to...

TweetyPants: How smart are your Twitter friends?

May 18, 2009

Are your Twitter friends smarty-pantses? Today I am releasing TweetyPants, a tool that uses QueryPath, OpenAmplify, and Twitter to evaluate a user's posts for three things:

  • Style: The more verbose and flowery one's prose is, the higher the score.
  • Shizzle: The more slang a post has, the higher the...

Drupal Quiz 3.0 Alpha 2 released

May 15, 2009

Drupal Quiz 3.0 Alpha 2 is now released.

The major news for this release is that it has gotten three big new features:

  • A new Matching question type has been added.
  • A new Short answer question type has been added (and it offers fill-in-the-blank-like functionality)
  • QTI (Question & Test Interoperability...

Packt: Creating Our First Module using Drupal 6 (Part1)

May 15, 2009

Packt has published an article of mine on beginning Drupal 6 module development. In the article, I cover the process of writing a first module (it's based on Chapter 2 of Learning Drupal 6 Module Development). Here's the intro from the article:

Creating Our First Module using Drupal 6 (Part1)

In...

QueryPath: Replacing text in one document with text from another

May 14, 2009

I received a good question in the QueryPath Drupal module's issue queue. Since the answer is not at all specific to Drupal's QueryPath module, and since the question is one that involves a few QueryPath nuances, I thought it would make a good post here.

To frame the question, here's some sample data:

<?php 
$a = '<div>
  <label>1</label>
  <label>2</label>
  <label>3</label>
</div>';

$b = '<div>
  <p>Second set.</p>
  <label>a</label>
  <label>b</label>
  <label>c</label>
</div>';
?>

The task is to take the labels from $a and write then into $b.

Here's the solution:

<?php 
require 'QueryPath/QueryPath.php';

$a = '<div>
  <label>1</label>
  <label>2</label>
  <label>3</label>
</div>';

$b = '<div>
  <p>Second set.</p>
  <label>a</label>
  <label>b</label>
  <label>c</label>
</div>';

$qpa = qp($a, 'label');
$qpb = qp($b, 'label');

//Might want to check to make sure they are the same length:
if ($qpa->size() != $qpb->size()) {
  // Do something...
  print "Warning...";
}

$i = 0;
foreach ($qpb as $label) {
  $label->text($qpa->branch()->eq($i++)->text());
}

$qpb->writeHTML();
?>

To begin, we create two QueryPath objects -- one pointing to $a, and one pointing to $b. And in both, we search for just the labels, since that is what we are going to replace.

We make sure that there are the same number of labels in each. We should be able to drop this section without causing QueryPath to err, but we do want to do this check.

Most of the "work" happens on these three lines:

foreach ($qpb as $label) {
  $label->text($qpa->branch()->eq($i++)->text());
}

This loops through all of the labels in $qpb and sets the label's text to whatever is in the corresponding indexed position in $qpa.

The latter part is done with $qpa->branch()->eq($i++)->text(). Why do we branch() here? Because we don't want to modify the $qpa QueryPath, and eq() is a "destructive" function. Branching clones the $qpa QueryPath object. When we run eq(), only the branched version is modified. This prevents us from having to do anything fancy with queries. The eq() function selects just the label at the given index. Then we fetch that label's text with text().

The output of this code will look something like this:

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" "http://www.w3.org/TR/REC-html40/loose.dtd">
<html><body><div>
  <p>Second set.</p>
  <label>1</label>
  <label>2</label>
  <label>3</label>
</div></body></html>

Using QueryPath to interact with a SQL database, Part 3

May 13, 2009

This is the third part in a series on how to use QueryPath to work with databases. In this part, we will see how the QPDB extension to QueryPath can be used to extract data from an XML or HTML document and store it in a database.

In the first part of this series, we looked at the basics of QueryPath...

Favorite Programmer Cartoons

May 12, 2009

The "Ballmer Peak" was explained to me at DrupalCon DC earlier this year... moments after I checked in Kitten.module.

A friend of mine sent me a link to a hilarious collection of programmer-oriented comics and cartoons, including the Ballmer Peak, Bobby Tables, the Dilbert Token Ring, and a few...

Microformats and RDFa are used by Google

May 12, 2009

I've seen a couple of unimpressive RDFa demonstrations. They tend to involve either a beta search server from Yahoo! or a custom tool with ugly regular expressions. In spite of the quality of the presentations, though, I was sold on the value of using RDFa to embed metadata into HTML. But what good...

Using QueryPath to interact with a SQL database, Part 2

May 11, 2009

This is the second part in a series on how to use QueryPath to work with databases. In this article we will work with SELECT statements. We will see how to work with returned results.

In the first part of this series, we looked at the process of connecting to a database and executing basic SQL statements...