Posts in the “php” category

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:

CakePHP web testing - deleting database test data

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.

CakePHP automated web testing with SimpleTest

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 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:

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:

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.

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 simple, printable calendar

If you've ever wanted a simple, printable calendar you can print and then put up on your refrigerator, I just created one over on the Valley Programming website. Here's a link to my simple printable calendar blog post over there. And fwiw, here's an image to give you a little idea of what the calendar looks like:

An Apache 301 redirect for a deleted directory

I recently reorganized the devdaily.com website, and as part of that, I deleted an entire directory at the root level that was named "/Dir". This was the "directory", where I had a Yahoo-like directory of links to applets, tutorials, CGI scripts, and so on, so I had thousands of web pages with URLs like these:

Free PHP charts and graphs

As I embark on two new PHP web applications that have a lot of charting and graphing needs, I've started to dig into the PHP charts and graphs world. I'm pleased to say there are several nice, free PHP charts and graphs tools available, and several commercial PHP charting/graphing tools as well. Here's my quick review of the current PHP charts and graphs tools.

A PHP char_at function

PHP FAQ: Is there a PHP char_at or charAt function, like there is in other languages?

No, there isn't, but you can easily mimic a PHP charAt function like this:

$char = $my_string{4};

That's equivalent to having a charAt function like this:

$char = charAt($my_string, 4);

or this:

A PHP last character function

PHP string FAQ: Can you share some source code for a PHP last character function (string function)?

Sure, I just developed a PHP last character function for an app I'm working on, and here's the source code for that function, aptly named last_char:

# a php last character function
function last_char($str)
{
  $len = strlen($str);
  return char_at($str, $len-1);
}

As you can tell from the code, it returns the last character from a PHP string.

PHP random number examples (rand function)

PHP random number FAQ: Can you show an example of the PHP random function?

Sure. To create a random number in PHP, just use a line of code like this:

$r = rand(1,10);

That will generate a random number from 1 to 10.

Real-world PHP random number example

Next, here's a snippet of PHP code that shows how to create a random number in PHP in some real-world code:

PHP include syntax examples

PHP FAQ: Can you show an example of how to use the PHP include syntax? Also, can you show an example of how to include HTTP content into a PHP script?

PHP include syntax to include a file

Sure, I just used the PHP include statement in developing a Drupal theme, and a simple form of the PHP include syntax to include a file looks like this:

<? include 'google-analytics.inc' ?>

This includes the contents of the file named "google-analytics.inc" into my PHP page output.

A MySQL PHP update query example

Nothing major here, just an example MySQL PHP UPDATE query (really, a PHP SQL UPDATE query example that happens to use a MySQL database). For all the things I've done lately with PHP, I think this is the first PHP update query I've written, and I wanted to put an example out here so I can find it easily later.

So, with no further ado, here is a simple MySQL PHP update query example:

PHP printing - How to print variables in a PHP string

Nothing major here, just a quick example of how to print variables in PHP, particularly how to print variables in the middle of a PHP string. When you first get started programming in PHP, how to print variables in a PHP string is a common question, so I thought I'd share some examples here.

There are many ways to print variables in a PHP string, but these are some of the most common approaches:

A PHP cheat sheet (reference page)

PHP cheat sheet FAQ: Can you share a PHP cheat sheet?

This page is a PHP cheat sheet, but a different kind of cheat sheet. As I go deeper and deeper down the PHP rabbit hole, I found that instead of an over-simplified PHP cheat sheet, I wanted a "starter" page that will point me in the right direction when I'm looking for a PHP function for a task. For instance, if I can't remember how to add an element to a PHP array, I want to be able to search this page and find that easily.

A simple PHP form example

PHP form FAQ: Can you share an example of a very simple PHP form (a self-processing PHP form)?

Still trying to catch up on a lot of PHP examples I've written but never published, here's the source code for a simple PHP form example. This form posts back to itself, and while I'm generally not a fan of that coding approach, I have used it from time to time for simple tasks.

A simple PHP form example

Here's the source code for this simple PHP form example:

PHP Zend starter applications

I'm going to be starting a new PHP project later this week, and as I don't like to reinvent the wheel, I've been looking around for some decent PHP starter applications, i.e., PHP skeleton applications.

As I've looked around for PHP starter projects, this starter application from Grover Ponders looks the best. It seems well-organized, and you can just download a tar.gz file and get to work.

PHP preg_match regex examples

PHP regex FAQ: Can you share some PHP regex examples using the PHP preg_match function?

For a recent PHP program I was writing, I experimented with the PHP preg_match function and some regular expressions, trying to get the behavior I was looking for. Now that they're working properly, I thought I'd share two simple examples of my "PHP regex" preg_match examples/experiments.