CakePHP: Components, redirect fail (On my part…)

I’ve been working on a CakePHP project lately and created a small component which was only needed in one of my controllers:

class CounterComponent extends Component {
  var $components = array(
    'Session'
  );

  function i() {
    if ($this->Session->check('Counter.i')) {
      $i = ($this->Session->read('Counter.i') + 1);

    } else {
      $i = 0;
    }

    $this->Session->write('Counter.i', $i);

    return $i;
  }

  function clear() {
    $this->Session->delete('Counter.i');
  }
}

Continue reading “CakePHP: Components, redirect fail (On my part…)”

PHP: Object Oriented Image Manipulation

I’ve been working on a CMS lately and having to create thumbnails for uploaded images is always a pain, lots of maths working out the correct sizes and such, so I’ve created a fairly small script to manipulate images in an object-oriented style.

For example:

scale(400, 300);
$image->write('small-image.jpg');

$image2 = new Image('image2.jpg');
$image2->watermark = 'sample.png';
$image2->output();

I’ve only implemented a few of the GD library methods, but I think these are the most useful methods. I might have to work on something that rounds the corners too, but I don’t have any need for it just yet.

There’s a demo page as ever, and you can get the script here (It’s a bit big, because the sample image is included, here’s one without it).

String Variable Concatenation

I stumbled across something odd today in PHP:

$r = '';
$r .= $r .= $r .= 'a';

Now, personally, I’d have expected a syntax error from the above code, but the result was even more confusing at first…

print $r; // 'aaaa'

Not sure if this was the expected output or not I tested similar code in other languages:

Ruby:

r = ''
r += r += r += 'a'
puts r # 'a'

Python:

r = ''
r += r += r += 'a'
#   File "", line 1
#     r += r += r += 'a'
#             ^
# SyntaxError: invalid syntax

Javascript:

var r = '';
r += r += r += 'a';
alert(r); // 'a'

Perl:

my $r = '';
$r .= $r .= $r .= 'a';
print $r; // 'aaaa'

That explains it!

So the reason the string is ‘aaaa’ seems to be that the code is evaluated from right to left:

$r = '';
$r += $r += $r += 'a';

// How it works:
//
// $r += 'a'; // 'a'
// $r += $r += 'a'; // 'a' + 'a';
// $r += $r += $r += 'a'; // 'aa' + ('a' + 'a')

I don’t think it’s a bug, well, at least I assume not, but is there a name for this?

Update: I asked some clever people for help understanding it.

jQuery Ajaxify

Probably not the first person to implement this, but I wanted to make an automatic form AJAX-‘ifier’ that could capture which submit button had caused the submit event to be fired too.

It’s implemented fairly simply:

$('form').ajaxify();

There’s a demo page or you can just download it and have a look.

Update: As pointed out by Andrea in the comments below, this would return the value of radio and checkboxes even if they weren’t selected. I have released 0.1a and updated the link above to fix this issue. Thanks Andrea!

Update 2: I have updated this plugin, view this post for more information.

Update October 2010: I have updated this plugin for jQuery 1.4.3 as a ‘lite’ version.

jQuery addCaptions 0.2 – Minor update

I’ve been using the jQuery addCaptions plugin and have come across a couple of new features that I felt were required and have fixed a bug in the previous verison.

I have added an exclude option to the settings so you can prevent captions being added to elements not required (like a reCaptcha box…). It’s used like this:

$('div.demo').addCaptions({
  'exclude': {
    'alt': [
      /orange/
    ]
  }
});

This would exclude any images that have the word ‘orange’ in the the alt attribute.

I also fixed a minor bug in which the script would look for captions in elements in reverse order (whoops!).

The only other change is making the elements found within the container a selector instead of an element option and a class option.

There is an updated demo here, or you can just download the script here.