Feed on
Posts
Comments

I required to generate a HTML table from PHP data for a project that I was working on. Unfortunately after a bit of Googling I couldn’t really find anything that was suitable so I decided to create my own.

I realise that this may not be the best table generating class ever and there’s probably a few bugs still in it, but I’d prefer to share and if anyone wants a better version I’ll look into it I originally wanted to create a table built up of seperate objects e.g. cell object, row object (built up of cell objects), but I realised the overhead on this would be HUGE! So I’ve gone back to an array setup.

The class will calculate the maximum number of columns used in the header, footer, or body and write out that number of columns. Headers, Footers, and the Caption are optional. Attributes are made up of an array where the key is the attribute name and the value is the attribute value. Attributes are applied to table tag (on table creation – class initialisation), table rows (array[row][attribute]), and table cells (array[row][column][attribute]) this applies for headers, footers, and the table body.

Example

I always find the best way to look at something is to use an example so here we go:
$table = new HtmlTable(array('class' => 'sortable', 'style' => 'width: 550px;'));

$header[0][0]['content'] = ‘ID’;
$header[0][1]['content'] = ‘Title’;
$header[0][2]['content'] = ‘Date’;
$header[0][3]['content'] = ‘Site’;

$table->addHeader($header);
$i = 0;
foreach ($newsList AS $newsItem) {
$itemLink = “{$newsItem['title']}”;
$tableBody[$i] = array(0 => array(’content’ => $newsItem['id'],
‘attributes’ => array(’style’ => ‘font-weight: bold;’),
1 => array(’content’ => $itemLink),
2 => array(’content’ => $newsItem['date_posted']),
3 => array(’content’ => $newsItem['site']) );
$i++;
}
$table->addBody($tableBody);
echo $table->drawTable();

ID Title Date Site
9 News 1 2009-04-04 10:40:00 site name
10 News 2 2009-04-04 12:44:11 site name

And the HTML looks like this:

<table class = "sortable" style = "width: 550px;" >
<thead>
	<tr >
		<th >ID</th>
		<th >Title</th>
		<th >Date</th>
		<th >Site</th>
</tr>
</thead>
<tbody>
	<tr >
		<td style = "font-weight: bold;" >9</td>
		<td >News 1</td>
		<td >2009-04-04 10:40:00</td>
		<td >site name</td>
</tr>
	<tr >
		<td style = "font-weight: bold;" >10</td>
		<td >News 2</td>
		<td >2009-04-04 12:40:00</td>
		<td >site name</td>
</tr>
</tbody>
</table>

Code

HtmlTable.php
If you have any comments please make them below, it would be great to hear some. If you end up using it also let me know and I’ll extend it in the future

I’ve managed to get a site up and running with the Zend Framework, everything is logging nicely to FireBug/FirePHP so next step was to log to the DB. I also wanted to log some additional information using the framework such as user agent, date and time, get and post variables. So to extend the manual a little here’s what I did:

// Set up logging to DB
$db = Zend_Registry::get('dbAdapters');
$db = $db['general'];

$columnMapping = array( 'priority' => 'priority',
'message' => 'message',
'datetime' => 'timestamp',
'user_agent'=> 'user_agent',
'get_vars' => 'get_vars',
'post_vars' => 'post_vars',
'site' => 'site'
);

$writerDb = new Zend_Log_Writer_Db($db, 'error_logging', $columnMapping);
$logger = new Zend_Log($writerDb);

$logger->setEventItem('datetime',date('Y-m-d H:i:s'));
$logger->setEventItem('user_agent',$_SERVER['HTTP_USER_AGENT']);
$logger->setEventItem('get_vars',print_r($_GET,true));
$logger->setEventItem('post_vars',print_r($_POST,true));
$logger->setEventItem('site',SITE);

$logger->info('Informational message');

Where the array keys in $columnMapping are my column names. ‘Priority’ and ‘message’ are understood by Zend_Log_Writers but the additional fields were added to give me some additional information.

Obviously this assumes that you have logging working using one of the other writers first

One of the fun things I’ve been involved in whilst working for Tracesmart is the names directory. Basically we took our huge information database and pulled out a big pile of stats about various names.

The process is all one big SEO effort in order to draw more traffic to the site, but it also provides some interesting information and name statistics.

Here’s an example for my name Lloyd Watkin, or I suppose more correctly Steven Watkin. Take a minute to look up your name on the Tracesmart site, might be interesting

We’ve also got names statistics data on the names directory page which tells you what names have been searched and how recently. Its quite interesting to see famous people’s names fly up the stats when they appear in the news. A recent example is that of Liam Neeson who’s wife died from a skiing accident recently, although obviously people aren’t quite right on the spelling (hence the huge numbers of hits on this name) — Liam Nilson.

Facebook Application

Once the names directory was created we set about creating a facebook application which we duely named ‘My Name‘. The facebook application is an extension of the names directory and provides some additional statistics. In order to add the names directory to your facebook account please visit:

http://apps.facebook.com/my-name/

A few months ago I changed my Flickr password so that a friend could upload some shots to it. A couple of months later I find I’ve forgotten that password and trying to remember all my secret details from Yahoo! is just a nightmare. I had my Yahoo! email address for several years and I wasn’t sure when or where (I was living) when I signed up – oh just to make this clearer Flickr uses Yahoo! login system

Yahoo! were no help either, they wanted exact details and would reject anything that didn’t match. Plus I wasn’t allowed to provide a list of possible matching details it had to be right on every detail otherwise it would be rejected. I can understand the security but this was getting a pain in the arse, especially as I could retrieve my user name to my usual email address without any trouble (why couldn’t you send a reset there!?!?!). On top of all this they wanted the details sent to their offices in California either by mail or fax, boo!

Step back to Flickr who kindly send an email to my registered email address asking me to detach my Yahoo! account from my Flickr account. Hooray I could then sign up with a new Yahoo! account and reattach my Flickr account to this new login.

After waiting at least a week each time for Yahoo! to reply Flickr got this all sorted within 2-3 hours!

Well done Flickr, thankfully you’re out there looking after your customers

Now for everyone’s boredom here’s yet another link to my Flickr account

Went hiking with the guys from Pontardawe today. we got a taxi up to Ystradgynlais and started a hike there after a breakfast in Merlyns.  As a note a few doors down there’s a cafe called Mimosa which is named after the boat which took the first Welsh settlers to Patagonia. The cafe was opened many yeas later by descendants of one of those original families who had returned to Wales.

We headed over to the Neath to Brecon Railway, and then up the hill to see the site of an old powered tram system with powered by something called “the machine” or “the engine”, I forget. Anyway at the top of the hill is the ruins of the old machine house which you can see in my photos. From there we headed north towards the Cribarth (and the pub). The Sarn Helen Open Cast meant that we had to detour and head back down to the railway line.

We headed up the railway line for  a bit (its being changed to a cycle path by the open cast people), and cut off to cross the valley to head up the Cribarth. We came across a circular building which was used to feed cattle of a Scottish design (McTurk brothers?).  There was a dead sheep inside so obviously it wasn’t working so well any more

We followed the Tawe for awhile before headnig up the Cribarth and eventually towards the Ancient Briton for a few beers and watch the Italy vs Wales rugby match.

Heres some photos: Ystradgynlais to Abercraf hike

I’ve recently set up a blog for one of the guys in work – Matthew Hopkins – and seeing his first few posts has made me think that maybe I should add a few posts as its been a good while and lots has happened over 2008. Another one of the guys in work does himself a blog to, good old Stephen Griffiths so take a look there too.

So I’m going to plod on and add some retrospective posts about what 2008 held for me and how 2009 is shaping up

2008 WTF?!?

2008 was a strange old year, there’s several things I really enjoyed about it,  and there’s some things that have now been forgotten and moved on with….

Diving

I haven’t done much in the way of scuba diving I think I managed around about 10 dives all year. One of those dives was the scuba santas event at Vobster Quay which was more successful than my wildest dreams. In the end we raised over £1000 for the RNLI, got coverage on BBC and ITV, and had around 80 divers in the water. Vobster was a sea of scuba diving santas for the day! I really hope I get to do mroe diving in 2009 (lots more!), getting back into it will involve lots of training to get comfortable again. I miss the days of getting on the boat, sun beaming down, grabbing a lobster, packing up   and heading home for a few beers.

Running

At the end of January 2008 I was involved in a car crash, hit from behind whilst stopped, nothing major or so I thought. Over several months my neck pain got progressively worse stopping me running. In the end I contacted an accident claims company who started getting me treatment with a chiropractor in Cardiff which I’m still attending (March 2009 – the claim still isn’t settled either). Running is another one of those things I want to get back to, I did start with Miles but between illnesses, injuries, and him eventually moving to Cambridge we only got out a handful of times. I really need a good kick up the bum to get going again, although my one consolation is that I have been cycling back and fore to work for months which is doing me some good.

Moved

In July 2008 I moved into a flat with Megan, we’re getting through the painting and decorating and getting it looking really nice  – although we’re sort of dragging our heels with the painting….still. The flat is in Adamsdown in Cardiff (I never seem to move far) and is suiting us just great.

Work

Work was all over the place in 2008, I started off drifting a little before taking up work at a letting agent as manager. This helped no end with the estate and letting agency software that I was developing. My plan for the estate and letting agency software is to release it as a low cost product when I have more time to clear it up. From what I’m told about competing products on the market it is easier to use and performs many or more of the same functions in a better way

During April 2008 coming up to my 22nd birthday both Megan and my mother started pushing for me to ‘get a career’, which a hard idea to swallow was really what I needed. Running my own company was fun but didn’t provide the stability needed and earning a pittance at the letting agency wasn’t a long term career goal. It was always a case of wait until I get my software finished and I’ll start selling it, but time was always against me.

What followed was several interviews with some great companies in some great areas, full of promise of moving to a new area, fresh start seemed great. The best job offer was with a large hosting provider in the Worcester area and seemed ideal. So what happened? Well I had developed an online SMS solution back around christmas that had been sitting around not doing much which investors were found for. Excellent I thought and off we went. All didn’t end well and after several months of broken promises and rising stress levels I made a stand and walked out. Working 60 hours a week, coming home and doing another 3-4 hours plus working all weekend and for all the stress it was one of the best decisions I’ve ever made. (Here I’d like to thank Megan, my parents, and my friends that stood by me, I was a stress-headed dick at times, and I’m sorry)

Four weeks later I landed myself a job with a company called Tracesmart in Cardiff Bay as a PHP Developer. After passing the tests and the interviews I started with a whole pile of enthusiasm (as you can imagine things had been a bit low for sometime). Tracesmart perform people searching, people tracing, electoral roll data handling, involving huge quantities of data. I’ve been there now for about 5 months (given I’m actually writing this in March 2009) and I love every minute of it. I’m pushing myself every day, have a great working environment (my desk overlooks the bay!), good colleagues! I’ve been involved in some good projects (other posts to follow) and I’m really proud of the work I’m doing there.

2009….

Despite starting 2009 in Germany suffering from the Novo virus 2009 is great so far. Megan and I spent  New Years time in Germany and Amsterdam. Amsterdam is beautiful and the Anne Frank house had a real profound effect on me, somewhere we’ll definately be returning to. Work is going really well. My neck is getting better and better so in a couple of weeks time I’ll head out for those first few awful runs once again. The only thing I really need now is a good few dives (or a dive holiday) to get back into the swing of things!

Given the growing size of this years scuba santas event to be held at Vobster Quay on the 20th December 2008 I thought I should create a website to collect all the information in one place instead of having it spread between Yorkshire Divers (YD) and facebook.

So here’s the site Scuba Santas – http://www.scuba-santas.co.uk

Links are appreciated as this is a fun event that I organise to raise money for the RNLI each year.

Scuba Santas 2008 is now up and running. Planning is underway for the 2008 dives organised through YD for the 20th December 2008.  The event is being controlled through facebook events.

There’s also a first individual post on YD here about what date to run the event and to get some provisional names for diving santas: Scuba Diving Santas 2008 – on Yorkshire Divers.

The event will once again be held at Vobster Quay, we’ve applied for another Guiness World Record, and hopefully get more visits from news crews such as those we had last year from the BBC.

More news will be posted once we’ve decided on a charity and things are moving along

Here’s more about last years event: Scuba Santas 2007.

Ian Dennis Technical Website Ian Dennis Technical has now been launched. Simply a shift of domains for Advanced Diver Training was a quick inexpensive website set up for a scuba diving instructor. So for all your uk trimix training you know where to go! Ian Dennis Technical – IANTD training in nitrox and trimix

Older Posts »