Archive for the ‘PHP Code Snippets’ Category

Australian Post Code Geo Database

Tuesday, June 24th, 2008

Here is some code and the SQL required to do searching based on Post Code it will give you distances between post codes, Suburbs close by, All suburbs with that post code, Distance between 2 post codes / long & lat, Give you the post code for the specified suburb. The PHP code can be found: here The SQL for MySQL can be found: here This code requires PHP 5 with PDO Support.

Update :)

I have now made 2 versions 1 for PHP4 and another for PHP5 neither require PDO they use the native MySQL library for PHP See link below

PHP5 : Native MySQL version

PHP4 : Native MySQL version

Note the above 2 have not been tested if you have any issues let me know.

Update:

Do not use tools what are browser based as this is a massive SQL Dump it will most likely time out uploading. Use a MySQL console by mysql -u USERNAME -p DATABASE < import.sql or a 3rd party app such as SqlYog

Namespacing much similar to Perl’s in PHP

Wednesday, April 23rd, 2008

Namepsacing in PHP is very handy at times why use require/include :) When you can have simply.

<?php

use foo::bar;

$blah = new bar();

?>

See below for an example any questions leave a comment

?Download template.txt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
<?php
/**
Example:
index.php
<?php
$template = new Template("test.php");
$template->ParseTemplate();
?>
test.php should be in modules/ and contain the following
<?php
use some::blah;
$blah = new blah();
?>
 
now create some/blah.php containing the following
<?php
class blah {
public function __construct() {
echo "namespacing in the works!";
}
}
?>
Namepspacing in pure php..
@author Michael Baker
*/
class Template {
 
public $_incpath = "/home";
public $tempdir = "/tmp";
 
/**
* Initate the Templating class ready for our name spacing
* @param $template string The module name eg. module.php
* @throws IO Errors
* @access public
*/
public function __construct($template) {
$this->contents = "";
if(file_exists("modules/$template")) {
$this->contents = file("modules/$template");
}
else {
throw new Exception('Fatal Error: <b>Template not found</b>');
exit;
}
}
/**
* ParseTemplate - Parse the template and do perl style namespacing on the script
* This will do a require_once once complete with the new code :)
* @throws IO Errors
* @access public
*/
public function ParseTemplate() {
foreach($this->contents as $line) {
if(preg_match("/use (?<dir>\w+)::(?<module>\w+)/", $line, $matches)) {
if(isset($matches['dir']) && isset($matches['module'])) {
if(file_exists($this->_incpath.'/'.$matches['dir'].'/'.$matches['module'].".php")) {
$line = "require_once('".$this->_incpath.'/'.$matches['dir'].'/'.$matches['module'].".php);";
}
else {
throw new Exception("Fatal Error: <b>Module {$matches['dir']}/{$matches['module']}.php not found</b>");
exit;
 
}
}
}
$this->_output .= $line;
}
$file = md5(time());
$handle = @fopen("/tmp/".$file.".php", "a");
if(!$handle) {
throw new Exception("Fatal Error: <b>Unable to write temp file</b>");
exit;
}
fwrite($handle, $this->_output);
fclose($handle);
require_once("/tmp/".$file);
unlink("/tmp/".$file);
}
}
?>

Templated HTML to PDF using DOMPdf

Monday, April 7th, 2008

The code posted below will convert the specified HTML file to PDF using DOMPdf (http://www.digitaljunkies.ca/dompdf/) code is documented with examples below.


<?php
/**
* HTMLPDF This will use the DOMPdf class to convert a HTML to PDF with templating
* @author Michael Baker
*/
class HTMLPDF extends DOMPdf {

proctected $input = null;
protected
$pdf = null;

public function __construct() {
$this->pdf = new DOMPDF();
}

/**
* inputFile - The input file name for the html file to be parsed
*
* @param string $filename The filename to parse for the template
* @return bool False if we hit an error but an exception will be thrown
* @throws I/O Errors
*/
public function inputFile($filename) {
if(!
file_exists($filename)) {
throw new
Exception(“Input file not found!”);
return
false;
}

if(!is_readable($filename)) {
throw new
Exception(“Input file found but not readable!”);
return
false;
}

$file = fopen($filename, “r”);
while(!
feof($file)) {
$this->input .= fread($file,8192);
}
fclose($file);
return
true;
}

/**
* parseTemplate - This will parse a set of key => value array pairs into the PDF
*
* @param array $template
* @return boll False on I/O error
* @throws I/O exception is output is not writable
*/
public function parseTemplate($template) {
if(!
is_array($template)) {
throw new
Exception(“Variable passed is not an array k => v array required”);
return
false;
}
foreach (
$template as $k => $v) {
$this->input = str_replace(“[$k]“, $v, $this->input);
}
$this->pdf->load_html($this->input);
return
true;
}

/**
* documentSize - This will set the document size for the output of the PDF see the PDF lib config for sizes
*
* @param string $paper Paper size
* @param string $orientation Paper orientation
* @return bool If the paper size or orientation is null it will return false
* @throws Null exception errors
*/
public function documentSize($paper,$orientation) {
if(
is_null($paper) || is_null($orientation)) {
throw new
Exception(“Paper and orientation cannot be null!”);
return
false;
}
$this->pdf->set_paper($paper, $orientation);
}

/**
* renderPDF - This will render the PDF to a specifed outfile
*
* @param string $output Outpu file name
* @return bool False on I/O error
* @throws I/O Exceptions
*/
public function renderPDF($output) {
if(!
is_writeable($output)) {
throw new
Exception(“Output file is not writeable exiting”);
return
false;
}
$this->pdf->render();
$this->pdf>stream($output);
return
true;
}

public function __destruct() {
unset(
$this->input);
}
}
$pdf = new HTMLPDF();
$pdf->inputFile(“Test.html”);
$template['NAME'] = “Michael”;
$pdf->parseTemplate($template);
$pdf->documentSize(“a4″, “portrait”);
$pdf->renderPDF(“Test.pdf”);
$pdf->renderPDF(“/tmp/out.pdf”);
?>