Posts Tagged ‘class’

29 April, 2014 — Development

Debitoor API Helper Class Version 2.0

Last year I published a PHP API Helper class for the ever-growing Debitoor accounting software. The API is a RESTful API meaning it responds to JSON and HTTPS requests.

As I have got to use this class in projects of mine, I have built upon this class adding improvements and a new Debitoor class that will process your requests for you returning a clean PHP array ready for you to use.

So what has changed?

You can find a list below of what I changed in this release and some newly added files.


//Debitoor API Class
// Code by Daniel Roizer -
// v2.0

define(req_code, "REQUEST_CODE");
define(clientID, "CLIENT_ID");
define(clientSecret, "SECRET_KEY");
define(token, "TOKEN_FROM_API");
define(apiURL, "");


$debitoor = new Debitoor();

// Get list of clients as an array()

// Get list of products as an array()

// Add a client
print_r($debitoor->AddClient($name, $address, $phone, $email, $homepage, $cinumber, $vatnumber, $countrycode));

Download Now

You can download version 2.0 of the helper class over at GitHub!
– OR –
View on GitHub

This is not part of Debitoor and is code written by myself. If you wish to have something added or have any questions please comment below or submit a ticket over a GitHub.

16 October, 2013 — Development

Debitoor API Helper Class

As you may know if you read my review of Debitoor a few weeks ago, I am a big fan of their service. When I first found Debitoor, they didn’t have an OpenAPI for people to use. I wanted to send data from my website right into Debitoor without the need to enter the information twice. This was when I went over to there feedback forum and requested the public API. They accepted my request and yesterday Debitoor released some exciting news.

There API is now ready to use. I jumped right into it and started developing my application. The API is a RESTful API meaning it responsed to JSON and HTTPS requests. I decided to write a quick PHP helper class to allow quick sending and receiving of data to the Debitoor API.

You can download version 1.0 of the helper class here!

This is not part of Debitoor and is code written by myself. If you wish to have something added or have any questions please comment below.

UPDATE: Please also find this on GitHub

6 August, 2013 — Development, Snippets

Simple PHP Template Class

Update: Now available on GitHub here. Please commit improvements.

For a recent project of mine (newsletter system) I needed a simple but effective PHP class that would allow me to send a string of data and find every word with the curly bracket around it and replace it with a word or string.

For example in my newsletter system, I needed the email’s to be customized to the person it would be sending it to such as Dear Daniel… Dear James…. but during the loop to send the email I would need to get the script to switch {name} to be Daniel. To do this I found this amazing little PHP class that does just the job.


Click here to download Simple PHP Template Class.

The Usage

This class is very simple. You pass 3 variables into the function Replace.

  1. An array of keys and variables – the key will be what the script will look for in your template such as {name}. The value will be what {name} will be replaced with.
  2. The template string where the data will be replaced.
  3. If the template is case sensitive. Eg {Name} wouldn’t work in the below example where as {name} would.
// Array of variables to change, template string, case sensitive true/false
echo CurlyVariables::Replace(array('name' => $name, 'email' => $email), $string, true);

Any trouble please let me know in the comments below.

Photo by: Travis Isaacs

24 July, 2013 — Development, Snippets

Simple PHP MySQL Database Class

Update: Now available on GitHub here. Please commit improvements.

A while back now I decided to dive into OOP (Object-oriented programming) in PHP. Until then I had not really had any experience with OOP as functional PHP did the job just fine for me at the time. Now I have had some time getting to know OOP I realised why so many people now use it!

The first thing I wanted to learn was the basics and like with 90% of my projects this would involve a mySQL database connection and queries. That was where I began. Reading about the PDO class (a database package included in PHP) I quickly understood that using the class command “extends” I could write my own class to simplify my database interaction process.


Click here to download my mySQL database class. (Note: this class can be adjusted for other database types)

The Usage

Like with any class we need to initialize the class and assign it to a variable.

$database = new Database();

Then we can do any of the following…

Simple Insert Query
Simply insert records to a table.

// Simple Insert Query
$database->query('INSERT INTO mytable (FName, LName, Age, Gender) VALUES (:fname, :lname, :age, :gender)');

// Bind the data
$database->bind(':fname', 'John');
$database->bind(':lname', 'Smith');
$database->bind(':age', '24');
$database->bind(':gender', 'male');


Insert Multiple Records using a Transaction
You can insert multiple records using the transaction function. This allows you to run 1 query and multiple binds to insert a lot of rows at once.

// Begin the Transaction.

// Query
$database->query('INSERT INTO mytable (FName, LName, Age, Gender) VALUES (:fname, :lname, :age, :gender)');

// Bind the Data
$database->bind(':fname', 'Jenny');
$database->bind(':lname', 'Smith');
$database->bind(':age', '23');
$database->bind(':gender', 'female');

// Execute that data

// Bind the next set of data
$database->bind(':fname', 'Jilly');
$database->bind(':lname', 'Smith');
$database->bind(':age', '25');
$database->bind(':gender', 'female');

// Execute that data

// End the transaction

Get Last Insert ID
Simple – Get the ID of the last record inserted.

echo $database->lastInsertId();

Select a Single Row
Select just a single row of data from a table.

// Query
$database->query('SELECT FName, LName, Age, Gender FROM mytable WHERE FName = :fname');

// Bind data
$database->bind(':fname', 'Jenny');

// Next we run the single method and save it into the variable $row.
$row = $database->single();

echo "
echo "

Select Multiple Rows
Get a selection of rows from a table and add them to an array ready to be processed using foreach().

// Query
$database->query('SELECT FName, LName, Age, Gender FROM mytable WHERE LName = :lname');

// Bind data
$database->bind(':lname', 'Smith');

// Run the resultSet method and save it into the $rows variable.
$rows = $database->resultset();

echo "
echo "

Count Rows

// Display the number of records returned
echo $database->rowCount();

I hope you find this script useful. Please comment if you have any questions or found this useful.

Photo by: Chris Hsia