pear

Convert HTML table to Excel XLS

I recently needed to update a page that dumped a HTML table into Excel – and found it dumped everything, including hyperlinks, buttons, images, etc. This didn’t sit well with Excel for Mac, and probably not a good idea.

I thought I would re-write it, but in a way that was re-usable (as this code was situated on every datagrid in the application)

So, using the PEAR extension Spreadsheet_Excel_Writer and a simple HTML DOM class, I came up with this. Its by no means finished, but as it stands, it works pretty well for what I need. Feel free to rob it and use the code.

The call is from a form which uses jQuery to copy the table as HTML into an input and then uses that in the output script ($_REQUEST[‘gridcontent’])

When its all complete, I may put it on an open source repo, such as sourceforge.

FYI, there is a section in here that takes cell text with a
in it as a split cell. So if your TD has a BR in it, it will make all the other rows merged so that the data looks nice. This is by design.
Continue reading

Install Xdebug on Ubuntu

I always forget the little bit about error reporting, so here is a step by step guide to getting Xdebug running on you Ubuntu PHP installation.

1) Install PECL (if you don’t have it already)

> sudo apt-get install php-pear

2) Install Pear Xdebug extension

> sudo pecl install xdebug

3) Create a new config file and have it point at the new extension. Pay attention to the location of the xdebug.so extension in the above image. That is the path for this step.

> sudo nano /etc/php5/apache/conf.d/xdebug.ini

Add a line (if its not already there)

zend_extension=

I.e.

zend_extension=/usr/lib/php5/20090626+lfs/xdebug.so

4) Now, edit the PHP ini file to change the following settings

display_errors = On

and,

html_errors = On

 

Done, just restart apache and you should have nice var_dumps now

How to install PEAR (Linux)

PEAR (PHP Extension and Application Repository) is a bunch of extensions you can install on your server to uitilise clever ‘extras’ that other people have spent their time writing.

To install it, sudo into a terminal console:

$> wget http://pear.php.net/go-pear.phar
$> php go-pear.phar

This should go away and get the files and install it. Now you have to find the extensions you want. They all have an identifier in the form of a string, i.e. Spreadsheet_Excel_Writer – you’re likely to find the name of it on the website that hosts the code/documentation.

Installing it is easy too:

$> pear install Spreadsheet_Writer_Excel

Done. Sometimes, a new version will come out, and the version number will be appended to the end of the name, so you might have to install it like:

$> pear install Spreadsheet_Writer_Excel-09.00.1

But, pear will tell you if this is the case, and it will tell you which version to install.

To list all the PEAR packages on your machine,

$> pear list

To remove one,

$> pear uninstall <packagename>

The place that keeps a list of the locations of the PEAR files is called a channel, and some packages are stored on a different channel to the standard, so you have to add the channel list to PEAR:

$> pear channel-discover <url>

But again, the documentation for the package will tell you if this is the case, and the URL if so.

You can read more about PEAR here – http://pear.php.net/ and there is a list of ‘supported’ packages here – http://pear.php.net/packages.php