php

recent posts related to the php programming language

PHP date validation

PHP date validation FAQ: How do I validate a date in PHP?

This is a little bit of a tough question because you'll probably want more than the basic PHP date validation technique. For instance, to check a person's age you'll probably want to include a limited range of years, and to check something like a credit card expiration date, you'll want to make sure the date is in the future.

That being said, the basic PHP date validation technique is to use the checkdate function, like this:

A MAMP new website setup tutorial

MAMP website setup FAQ: How do I configure MAMP and my Mac so I can develop a new PHP website? (Or, Can you share a Mac MAMP setup tutorial?)

Assuming you already have MAMP installed, creating a new MAMP website is pretty easy. I've gotten to the point where I can do it in about two minutes. You just have to configure your /etc/hosts file and your MAMP Apache configuration file. Here's how.

How to run a Drupal 7 query outside of Drupal

Drupal query FAQ: How can I run a Drupal query outside of Drupal, i.e., from the command line? (Drupal 6 or Drupal 7)

Last night I was reading the excellent book Pro Drupal 7, and I stumbled on most of the solution for running a Drupal query from the command line. The book was exactly correct, except for one missing step, which may have been something that changed after publication.

The PHP PDOException SQLSTATE MySQL can't connect socket error

PHP PDOException MySQL socket error FAQ: What is the PHP PDOException SQLSTATE MySQL socket error, and how do I fix the problem?

Every time I work with a new web hosting company, I keep running into one variation or another of this PHP MySQL socket error. It's pretty darned annoying, but at least I know how to fix it now.

In short, if you try to access a MySQL database using the PHP PDO library, such as with Drupal 7, which is what I just did, you may get an ugly MySQL socket error that looks like this:

CakePHP HTML image links

CakePHP HTML image links FAQ: How do I create HTML image links in CakePHP?

When I first started using CakePHP I had no idea how to create a CakePHP image link, so I thought I'd share what I've learned here, specifically the CakePHP HTML image and HTML link syntax.

Rather than go into any great detail, I'll just share the source code from one of my CakePHP view pages. This is from a typical CakePHP index view page, but I wanted to use images for the Edit and Delete actions instead of plain links. Here's the CakePHP view code I created to get this done:

CakePHP automated web testing with SimpleTest alvin January 30, 2011 - 12:54pm

CakePHP/SimpleTest automated web testing: Can you share an example of some automated CakePHP SimpleTest web tests?

Sure, here's a quick look at how I've created a suite of CakePHP automated web tests using SimpleTest to perform integration testing on a new CakePHP web application.

The recipe for creating automated web tests in CakePHP is fairly simple:

CakePHP web testing - deleting database test data alvin January 30, 2011 - 12:47pm

CakePHP delete database test data FAQ: How can I delete CakePHP test data from my database in automated CakePHP unit and web tests (integration tests)?

I recently created a large set of CakePHP unit tests and CakePHP web tests (essentially integration tests), and as part of thorough testing, I needed to be able to create some user accounts and insert some test data into the database.

A CakePHP SQL UPDATE example

CakePHP SQL UPDATE FAQ: How do I perform a SQL UPDATE with CakePHP?

As I was writing a user registration process for a CakePHP application, I just found myself in a situation where I needed to write a CakePHP SQL UPDATE query. While you don't pay attention to it, you often run a SQL UPDATE query in CakePHP in a typical controller edit() function, but this was the first time I really had to do this manually.

The short answer is that you execute a SQL UPDATE query in CakePHP using the usual model save() method. The way the save method work is like this:

A CakePHP jQuery fade out flash success message

CakePHP flash fade out success messages: If you'd like to show fade out success messages in your CakePHP applications using jQuery, like the ones currently used by applications such as Twitter and Facebook, I thought I'd share the formula I came up with this weekend. While it includes touching a few different files, it's pretty easy, and once you've done the basic setup work you can display CakePHP fade out success messages in all of your web pages, and web applications.

The CakePHP form save, validate, and debug problem

CakePHP form save/validate/debug FAQ: How do I debug problems in the CakePHP form save/validate process?

One problem I've run into with CakePHP is that while the form validation approach works very well 95% of the time, when it fails it can be very hard to debug. Because the form validation process happens when you invoke the save() method, much of this debugging process seems to be out of your control.