Posts Tagged ‘php’

Iterating through class constants in PHP

Friday, June 18th, 2010

I was breaking my head trying to figure out the best way to reference a particular set of constants in one of my objects so that I didnt’t unncessarily need to create a new database table. Fortunately, I found this thread about looping through class constants using reflectors.

As a summary, it can be done in a very small amount of code:
class test{
const CONSTANT = 'foo';
}
$t = new test;
$r = new ReflectionObject($t);
print_r($r->getConstants());

With the appropriate naming convention – everything else is easy!

Thanks to jpadie.

Share on Facebook

Creating Line Graphs in JPEG Format through PHP

Tuesday, May 25th, 2010

A while ago I wrote about making use of information collected by websites – I find graphs a great way to do this. And while there are lots of great graphing objects, or tools available today, it can be a hectic scene to try and find a free one that you want for basic use. So quick modifications to existing code can sometimes be exactly what you need! Below is the code that was used to create the graphs seen in that previous post.
(more…)

Share on Facebook

Useful Currency Information by Accident

Wednesday, May 12th, 2010

Pennies and Pounds If you run a website, have you ever noticed how much data it accumulates? Usually, quite by accident as well… I was poking through some old parts of the VenueMirror website today and stumbled across a feature that I had totally forgotten we had implemented. It is of course one of those features that the code makes use of everyday, but it has become so inherent that it gets taken for granted!

There is a need for our system to perform currency conversions as transactions can take place across multiple currencies, and so currency updater was created to provide live exchange rates. Our currency updater is quietly used by our currency and transaction objects, and so once built there’s very little need for us to even think about how it works! Essentially it collects the latest currency conversion rates and builds a currency table for the whole system to use. In the process though, it has collected historical currency data.

So I thought it would be interesting to render this data into charts that show how exchange rates fluctuate. I built a quick bit of PHP to take daily spot rates out of the data at 11.30am and see how they compared to the previous days! The images below are dynamic PHP images, and should show you exchange rates for ten days started from 25 days before yesterday!




The script takes parameters that allows it to be customised on how many data points it shows, which currencies it compares, and how many days into the past it should end. Tomorrow, I’ll post the source to the graphing code, and the tutorials that are available online for you to get more information from.

Cheers,
K
P.S: Thanks to Mukumbura for the opening photo!

Share on Facebook

Sending HTML composite email in PHP using objects!

Tuesday, May 4th, 2010

Photo of an American MailboxHave you ever tried to send HTML email using PHP? Sending plaintext email is quite straight forward and easy, and if you have the energy to write your own functions, sending HTML email can be pretty simple too. Half the problem is writing functions that can be ported around to different projects quickly and easily.

PHP Objects made this much easier to do, and since then I’ve been swapping objects in and out of various different parts of the code, and across different applications. I realised the other day that the one I make the most use of is my mail message object. So I thought I would open it up in the event that you also found it useful! (more…)

Share on Facebook