PHP Array to Text Tables

October 31st, 2008 § 0

I needed this for a little project so I coded it up. I haven’t done a lot of tests but it works just fine for formatting the associative arrays I have run through it.

The class supports multi-line rows, limiting the width of the column, and automatically creating a heading based on the keys from the associative array.

Usage and Output Example

Text Table Formatted Output of above example:

+----------+----+---------------------+
| COMPANY  | ID |       BALANCE       |
+----------+----+---------------------+
| AIG      | 1  | -$99,999,999,999.00 |
| Wachovia | 2  | -$10,000,000.00     |
| HP       | 3  | $555,000.000.00     |
| IBM      | 4  | $12,000.00          |
+----------+----+---------------------+

Full class source code after the jump…
» Read the rest of this entry «

Convert from ADOdb Library to PDO

May 30th, 2008 § 0

In the past I used the ADOdb Database Abstraction Library for PHP for a lot of my PHP projects. However, PDO (PHP Data Objects) is now a better fit as it performs better for me and doesn’t require the inclusion of any libraries.

I wanted to make the switch not only for future projects, but also to go back into some of my existing projects that I maintain and covert from ADOdb to PDO. However, I didn’t want to alter thousands of calls to ADOdb methods.

The solution I came up with was to do some more work on my PDO Singleton Class. By adding in the ADOdb methods Execute(), qstr(), GetOne(), and an iterator for the adodb recordsets returned by Execute(), I was able to easily transition to PDO in projects with thousands of lines of code by including this class and creating a new instance of sdb() instead of NewADOConnection() and Connect().

$DB =& new sdb(”mysql:host=localhost;dbname=testdb”, ‘username’, ‘password’);

This is the equivalent of the following ADOdb code: (except it is a singleton)

$DB = NewADOConnection(’mysql’);
$DB->Connect(”localhost”, “username”, “password”, “testdb”);

You can now treat that $DB object just as you would with ADOdb to create a connection to the mysql database ‘testdb’. Note: I have not refactored the error or transaction functions as they were not used in the specific projects I needed this conversion in… but they would be very simple to add.

» Read the rest of this entry «

PHP5 PDO Singleton Class

May 23rd, 2007 § 2

I use Zend Studio for Eclipse as my PHP IDE. The framework I am developing uses PHP PDO for the database abstraction, and rather than passing around a reference to my PDO instance I wanted to create a singleton.

At the same time, I wanted to take advantage of the helpful code completion and suggestion features of the Zend IDE. So I created a class that creates a PDO singleton object in a way that Zend Studio for Eclipse can still do code assistance.

PHP5 PDO Singleton Usage Example

$dbObj =& new sdb("mysql:host=localhost;dbname=testdb", 'username', 'password');

This create a connection to the mysql database ‘testdb’ and a new PDO instance. $dbOjb can be passed around by reference but using the sdb:: singleton is recommended as Zend will do code assist on this.

SOAP Plugin for Smarty

April 21st, 2007 § 0

This plugin for Smarty enables in-template access to SOAP services.

It requires the NuSOAP PHP class in your PHP include folder (ie: includes_path/nusoap/nusoap.php) or edit the path in the soap smarty plugin.

You may need to download the NuSOAP php class and place it in your PHP include path.

The smarty soap plugin function is shown after the fold.

Example SOAP Plugin Usage in Smarty Templates:

{soap assign=soapResponse
 endpoint=http://site.com/resource.soap
 call=resource..search
 var=(string)Test
 debug=true|false}

» Read the rest of this entry «

JSON Plugin for Smarty

April 20th, 2007 § 4

JSON Plugin for Smarty

This is a simple plugin for Smarty to retrieve and parse JSON data into an object in Smarty. Requires the PECL JSON.php class or PHP 5.2.0.

Instructions:

  1. If your PHP version is < 5.2.0 and save it to your include path if it is not already there. If your PHP version is >= 5.2.0, this class is not needed, we will use the native function.
  2. Download the JSON Smarty Plugin PHP Function File (shown after the fold) as function.json.php in your smarty\plugins\ directory.

JSON Plugin Usage in Smarty Templates Files:

{json
 url=http://site.com/data.json
 resource=result}

The $result variable now contains the parsed JSON data.

» Read the rest of this entry «

Where Am I?

You are currently browsing the PHP category at Tony Landis.