Archive for the ‘PHP’ Category

New Tutorial on Complex Objects in PHP Sessions

April 6th, 2008

For those PHP programmers out there new to the language I have posted a new tutorial (just a little one :) ) on how to store and retrieve complex objects (classes) in PHP session ($_SESSION). You can find it under Software Development, or go straight to it here. Cheers!

Tags: , ,
Posted in Development, PHP | Comments (1)

My First Plugin

October 16th, 2007

Yes, ladies and gentlemen, I have started learning how to write WordPress plugins. My first plugin is one that removes the word “Private: ” from the beginning of blog and page titles (when they are saved with a status of private).

The way this works is by creating a function that takes one argument. I then used a regular expression to remove the offending words. The regex I used was /^(private:\s*)/i and then I simply replaced any match with a blank string, removing it from the source text. And in WordPress there are two types of plugins: Action hooks, and Filter hooks.

In this case I used a filter hook, since filter hooks alter text coming or going from the WordPress database. So to make my filter work you have to use the add_filter function to register my function to handle the filter hook “the_title“. This hook alters the title of posts and pages before they go out to the browser. You can find my plugin at http://blog.adampresley.com/wordpress-mods/hide-private-in-title/. Now, here’s the code.

  1.    /*
  2.       Plugin Name: Hide "Private" In Title
  3.       Plugin URI: http://blog.adampresley.com/?page_id=124
  4.       Description: Removes the word "private:" from the beginning of blog entry
  5.                    and page titles.
  6.       Version: 1.0
  7.       Author: Adam Presley
  8.       Author URI: http://www.adampresley.com
  9.  
  10.       Copyright 2007  Adam Presley  (email : psykoprogrammer@yahoo.com)
  11.  
  12.       This program is free software; you can redistribute it and/or modify
  13.       it under the terms of the GNU General Public License as published by
  14.       the Free Software Foundation; either version 2 of the License, or
  15.       (at your option) any later version.
  16.  
  17.       This program is distributed in the hope that it will be useful,
  18.       but WITHOUT ANY WARRANTY; without even the implied warranty of
  19.       MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  20.       GNU General Public License for more details.
  21.  
  22.       You should have received a copy of the GNU General Public License
  23.       along with this program; if not, write to the Free Software
  24.       Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
  25.    */
  26.  
  27.    //————————————————————————–
  28.    // Name: filter_hidePrivateInTitle
  29.    // Auth: Adam Presley
  30.    // Desc: Removes the word "private:" from blog and page titles.
  31.    //————————————————————————–
  32.    function filter_hidePrivateInTitle($content)
  33.    {
  34.       return preg_replace(‘/^(private:\s*)/i’, "", $content);
  35.    }
  36.  
  37.    //—————————
  38.    // Add the hook to WordPress.
  39.    //—————————
  40.    add_filter(‘the_title’, ‘filter_hidePrivateInTitle’);

Tags: , , ,
Posted in Development, PHP | Comments (0)

Caching in PHP: Cache_Lite

May 30th, 2007

For many web application developers the concept of caching is foreign. Most of us build our pages, query our databases, and spit out the results. This is usually sufficient as the content is entirely dynamic. There are times, however, when the data, although not static, doesn’t change very frequently, and can benefit from caching.

Caching can come in many complex flavors, but in this blog I will talk about a very simple caching mechanism accomplished by using the PEAR library’s Cache_Lite class. Cache_Lite allows you to cache some specific piece of data, or page content, or whatever, and retrieve it quickly for reuse.

As an example let us say you have a database table with all of your customers and information about them. This table is not likely to change TOO quickly, so perhaps a web page that allows you to search and manage these customers would benefit from caching. Let us also say that this table is named customers, and you have one on 2 different database servers, because you have several clusters. Lets also assume you have an XML file that is the master list of all customers across all clusters.

If you are building a web page that can search for customer by providing information in a search criteria, you would have to read the XML file, and then go to the customer table for each cluster to find the information you want. Well, perhaps there is a better way. In this example I am using an imaginary XML file which would look like this:

  1.  
  2.  
  3.       DB1000
  4.       1

With this information we can then go the customer table on the database server that each customer is on to retrieve additional information.

  1. require_once("Cache/Lite.php");
  2.  
  3. $cacheId = "UniqueCacheId";
  4. $cacheXmlGroup = "xmlGroup";
  5. $cacheCustomerGroup = "customerGroup";
  6.  
  7. $cacheOptions = array(
  8.    "lifeTime" => 3600,
  9.    "automaticSerialization" => true
  10. );
  11.  
  12. $cache = new Cache_Lite($cacheOptions);
  13.  
  14. //————————————————————–
  15. // Read the XML master customer file. If the data is already in the
  16. // cache use that.
  17. //————————————————————–
  18. if ($cacheData = $cache->get($cacheId, $cacheXmlGroup))
  19.    $xmlCustomerList = $cacheData;
  20. else
  21. {
  22.    $xmlCustomerList = simplexml_load_file("customerXmlFile.xml");
  23.    $cache->save($xmlCustomerList, $cacheId, $cacheXmlGroup);
  24. }
  25.  
  26. //—————————————————————————-
  27. // Get the detail information from the database server/cluster this customer is on.
  28. // Assume we are already connected to each cluster database, and get all the
  29. // data. We’ll pretend we have an array of mysql connections. If this data
  30. // is already cached, use that.
  31. //—————————————————————————-
  32. if ($cacheData = $cache->get($cacheId, $cacheCustomerGroup))
  33.    $customerArray = $cacheData;
  34. else
  35. {
  36.    foreach ($connectionArray as $connection)
  37.    {
  38.       $qry = mysql_query("SELECT * FROM customers", $connection);
  39.  
  40.       while ($row = mysql_fetch_object($qry))
  41.       {
  42.          $customerArray[$row->customerName]["additionalInfo1"] = $row->additionalInfo1;
  43.          $customerArray[$row->customerName]["additionalInfo2"] = $row->additionalInfo2;
  44.       }
  45.    }
  46.  
  47.    $cache->save($customerArray, $cacheId, $cacheCustomerGroup);
  48. }

In this example we are loading two pieces of data. The first is an XML file with a master customer list, containing all customers across multiple clusters. Each cluster has a database server with a customer table that has detailed information about customer on IT’S cluster. With this data retrieved you can then do just about anything with that data. Search on it… Manipulate it… Data mining… whatever.

The COOL thing about this is the caching. While the cache file is still valid the XML and customer information data is pulled from the cache file instead of reading the XML and querying the database EACH time. This is a significant speed boost.

Tags: ,
Posted in Development, PHP | Comments (0)