Posts in the “php” category

CakePHP naming conventions

CakePHP naming conventions FAQ: Can you share some examples of the CakePHP naming conventions, specifically the CakePHP model, view, and controller naming conventions?

As I get back into the CakePHP development world, I wanted to make a little CakePHP naming conventions reference page, showing examples of the standard CakePHP naming conventions for CakePHP model, view, and controller elements.

A PHP array to CSV string example

PHP array to CSV FAQ: How do your convert a PHP array to a CSV string?

Converting a PHP array to a PHP CSV string is so easy, it actually felt like I trick question when I just found the answer in the PHP docs.

In my case, I just had a PHP array named $tags, and I wanted to convert it to a PHP CSV string. To do this, all I had to do was use the PHP implode function, like this:

A PHP "select from where in" query example

If you ever need the source code for a PHP "SELECT FROM foo WHERE id in (1,2,3)" example, I hope the following PHP function will be a decent example to work from. I turned a given list of database table id values into a CSV list, then use that string in a "SELECT FROM ... WHERE ... IN ... (list)" SQL query.

I'm not going to explain it today, but I will share the PHP source code for your reading enjoyment (and problem solving).

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.

A Smarty templates object array example

Smarty templates object array FAQ: How can I display an array of objects with Smarty templates?

I just wasted about thirty minutes trying to figure out how to display an array of objects with Smarty templates, so I thought I'd share the solution here. I'll include my PHP class, some of the PHP logic that builds the Smarty variables (my Smarty object array), and then a portion of the Smarty template that displays my PHP object array.

PHP array - How to loop through a PHP array

PHP array FAQ: How do I iterate (loop) through a PHP array?

Answer: The easiest way to loop through a PHP array is to use the PHP foreach operator. If you have a simple one-dimensional array, like the $argv array which lets you access command line arguments, you can loop through it like this:

Use the PHP addslashes function to fix SQL INSERT problems

PHP MySQL FAQ: Can you share an example of how to use the PHP addslashes function when creating a SQL INSERT statement?

When you're inserting records into a database table with plain PHP (as opposed to using a PHP framework), some of the fields you're inserting may have characters in them that will cause problems during the SQL INSERT process. Characters that will cause problems on database INSERT statements include the single quote, double quote, backslash, and NUL characters.

A CakePHP debug strategy

I just started working with CakePHP in the last two weeks, and as a newbie, I've found a simple debug strategy that I like. I've used this CakePHP debug technique several times when trying to formulate a CakePHP find query.

CakePHP find - a SQL limit query example (SQL select with limit clause)

When working with CakePHP, one of the hardest things for me has been getting used to formulating SQL queries with the CakePHP find method syntax. Today I needed to run a SQL query where I limited the amount of records returned from the database, and after a little trial and error, I found the correct CakePHP SQL LIMIT syntax, and thought I'd share that here today.

The CakePHP error log (CakePHP error logging)

CakePHP error log FAQ: Where is the CakePHP error log, and how do I write to it?

CakePHP error log file location

The CakePHP error log file is named error.log, and it is located in the $app/tmp/logs directory of your CakePHP application:

$app/tmp/logs/error.log

(Where $app represents the name of your CakePHP application.)

Solved problem trying to configure PHP with GD support

Problem

I was just trying to install PHP from source code on a CentOS Linux box, specifically trying to configure PHP with GD support with a configure command like this:

./configure --with-apxs2=/usr/local/apache2/bin/apxs \
            --with-mysql \
            --with-gd

Unfortunately this configure command fails, ending with the following two lines of error output:

Drupal and CakePHP - Why so many arrays?

As a relative newbie to both Drupal and CakePHP, I see the same programming style in both applications, and I'll call this style "programming in arrays of arrays". That is, anywhere I would normally use an object in Java, the Drupal and CakePHP developers have used an "arrays of arrays" to store data.

Let's look at some source code examples so I can compare their "arrays of arrays" approach to an OOP approach.

CakePHP find and the SELECT BETWEEN query syntax

In an effort to get more CakePHP find query examples out into the world, here are two examples of how to perform SQL "SELECT BETWEEN" queries using the CakePHP find method.

In my first find query example, I'm trying to find all the records from the logfile_records database table where the date column is between the two dates specified:

Setting the CakePHP page title

CakePHP page title FAQ: I'm still a CakePHP newbie, but I just figured out how to change the page title on my CakePHP view pages (i.e., the HTML TITLE tag on my view pages), so I thought I'd share that process here.

Setting the CakePHP page title

The first step in setting a CakePHP view title is to set the pageTitle variable in your CakePHP controller method. Here's an example where I set the page title in a typical CakePHP controller index method:

CakePHP SQL debug - Configure the CakePHP debug output setting

CakePHP SQL debug tip: If you're a CakePHP newbie, one of the surprising things about seeing your first CakePHP view pages is seeing the CakePHP debug output (the SQL debug output) at the bottom of your view pages. It's definitely a nice feature for when you're learning CakePHP, and also for any time you're trying to understand what queries CakePHP is running for you, but it's a surprise at first.