Category: "PHP"

Wicked Good Gallery - Lightweight LAMP, Easy to Administer

The Wicked Good Gallery grew from an idea mentioned at http://w3schools.com/forum and discussions with the people at http://lilyclaireart.com.

The challenge was to build a gallery application that would allow site visitors to view and purchase artwork and be very easy to administer.

The gallery offers two viewing modes, navigation through the thumbnails at the top of the page, and a slide show. The thumbnails are limited by the width of the page, so not all thumbnails may be displayed. Arrows are used to indicate additional images earlier or later in the list. Clicking on a thumbnail displays a larger image, along with the associated HTML. The slide show hides the HTML and centers the image, then loops through the images to display all the images of the gallery. It’s as if the site visitor is walking through the gallery, looking at artwork.

Administration is through SFTP/FTP/SCP. The person running the site creates a directory, named for the image, and places the original image .jpg in it, as well as free form HTML. When the site loads the next time, it will find the new directory, check for a thumbnail and detail image, and create them if necessary. When site visitors click on the thumbnail, the HTML will be displayed with the image.

With respect to SEO, each image is represented with a dedicated URL. The artwork’s name is used as the title, and the more text in the HTML for that image, the better.

All requests into the application are routed through a single file. If the requests map to an image, the appropriate artwork is displayed, otherwise, the home page, or contact page is shown.

Debugging AJAX

  • Use the transport mechanism to send debug information. For example, if you’re sending back an array of data, add a new element (or several), and put in the SQL query so you can see exactly what’s being executed.
  • Store the returned data in global variables on the client side and use Firebug to examine them. console.debug and alert are helpful, too.
  • Use var_dump and echo if you get frustrated. They’ll probably throw an error on the client side, but you’ll get to see what you want to.
  • Use var_export($variable,true) if you want to do a var_dump to a string or to a file. file_put_contents(’/tmp/file’,$data,FILE_APPEND); is a great way to monitor script execution. Open a new SSH window and use tail -f /tmp/file to see it.
  • Cut and paste output into Notepad so you can easily verify it. Often you need several chunks of text and data to truly understand what is happening and why.
  • Develop complex pieces in a standalone environment. This is good for assembling arrays of data out of complex SQL queries. Simplifying the task so you can focus on the difficult parts will speed development.
  • Watch out for extra commas in object assignments. IE will crash.
  • If you have Visual Studio, use it. It can save you a tremendous amount of time when debugging under IE.
  • Test for the presence of variables, objects, and attributes before using them. It isn’t really right, but you can use if (variable_exists) to see if the variable has been set up. A better solution is to initialize everything, or to use if (typeof variable != ‘undefined’).
  • To remove something from a page, use style="display:none". It works, it’s simple, and it’s very low risk. If anyone is playing with your code, they’ll find it, but it shouldn’t be an issue unless it had security issues, or restricted content.
  • Add a debug setting to your code that allows you to receive more information when errors occur. You can disable it for the production release.

Upgrading stock CentOS5 PHP to 5.2.12

To run eZ publish 4.2, I needed to upgrade CentOS’s PHP 5.1.6 to PHP 5.2.12.

I started at http://php.net, and ended at http://wiki.iuscommunity.org/Doc/ClientUsageGuide#Installing_Packages_from_IUS_Testing The site has a nice collection of RPMs for PHP, so I removed the 5.1.6 RPMS and installed 5.2.12, using RPM.

Phone Number RegExp - PHP

Matching a phone number, with a regular expression, and disregarding any extraneous characters.

Useful for searches, not validation.


<?php
$aPhoneNumbers=array(
'7085556232',
'1(708)555-6232',
'(708)555-6232',
'708.555.6232',
'1.708.555.6232',
'1.708.555.6232 ext. 123',
'17085556232',
'7215556232',
'1(721)555-6232',
'(721)555-6232',
'721.555.6232',
'1.721.555.6232',
'17215556232'
);

$sPhone='7085556232';
$rPattern='/(\d)/';
$rReplace='\D*${1}';
$sRegExp='/'.preg_replace($rPattern,$rReplace,$sPhone).'/';

echo 'Regular Expression: '.$sRegExp."\n";

foreach ($aPhoneNumbers as $k => $v)
        check($sRegExp,$v);


function check($sRegExp,$sPhone)
{
echo '$sPhone: '.$sPhone.' ? '.preg_match($sRegExp,$sPhone)."\n";
}
?>

Output

Regular Expression: /\D*7\D*0\D*8\D*5\D*5\D*5\D*6\D*2\D*3\D*2/
$sPhone: 7085556232 ? 1
$sPhone: 1(708)555-6232 ? 1
$sPhone: (708)555-6232 ? 1
$sPhone: 708.555.6232 ? 1
$sPhone: 1.708.555.6232 ? 1
$sPhone: 1.708.555.6232 ext. 123 ? 1
$sPhone: 17085556232 ? 1
$sPhone: 7215556232 ? 0
$sPhone: 1(721)555-6232 ? 0
$sPhone: (721)555-6232 ? 0
$sPhone: 721.555.6232 ? 0
$sPhone: 1.721.555.6232 ? 0
$sPhone: 17215556232 ? 0

Rebuilding PHP Under CentOS5

  1. Configure firewall to allow httpd requests: system-config-securitylevel

  2. Get PHP development RPM: http://rpm.pbone.net/index.php3/stat/4/idpl/8077900/com/php-devel-5.1.6-20.el5.i386.rpm.html

  3. Get PHP source RPM: ftp://ftp.pbone.net/mirror/ftp.centos.org/5.3/os/SRPMS/php-5.1.6-23.el5.src.rpm

  4. Use yum to handle RPM dependencies. yum install php-5.1.6-23.el5.src.rpm
  5. Update SPECS/php.spec and enable/disable options as necessary.
  6. rpmbuild -bb –target=i386 SPECS/php.spec - It’s good to specify the target, since otherwise, the RPM build may take a very long time.
  7. Force the installation of the updated RPMs. I did an ls of the RPMS directory and forced them all.
    rpm -i –force php-5.1.6-23.i386.rpm php-bcmath-5.1.6-23.i386.rpm php-cli-5.1.6-23.i386.rpm php-common-5.1.6-23.i386.rpm php-dba-5.1.6-23.i38
    6.rpm php-debuginfo-5.1.6-23.i386.rpm php-devel-5.1.6-23.i386.rpm php-gd-5.1.6-23.i386.rpm php-imap-5.1.6-23.i386.rpm php-ldap-5.1.6-23.i386.
    rpm php-mbstring-5.1.6-23.i386.rpm php-mysql-5.1.6-23.i386.rpm php-ncurses-5.1.6-23.i386.rpm php-odbc-5.1.6-23.i386.rpm php-pdo-5.1.6-23.i386
    .rpm php-pgsql-5.1.6-23.i386.rpm php-snmp-5.1.6-23.i386.rpm php-soap-5.1.6-23.i386.rpm php-xml-5.1.6-23.i386.rpm php-xmlrpc-5.1.6-23.i386.rpm