Thursday 19 July 2012

Simple PDO Class

For some reason, maybe it was my first (negative) introduction to OOP through C++, I've always hated those (::) double colons. So, when I started to look at PHP Data Objects (PDO), although it seemed like  a good idea, I couldn't get over the fact that I was opening myself up to using static class and methods with all the syntax which goes with it.

So I started to create a simple database class which made use of PDO, which would be a little friendlier to call. Humble beginnings these. First I created an ini file for all the settings, which looks something like this:

DB_TYPE = mysql
DB_HOST = localhost
DB_USERNAME = jimmy
DB_PASSWORD = password
DB_NAME = testdb

Then, I created the database class which called the ini file thus:

<?php
class database
{
private $config;
private $connection;
private $pdoString;
  function __construct()
{
$this->config = (object) parse_ini_file('config.ini', true);
$this->pdoString = $this->config->DB_TYPE;
$this->pdoString .= ':dbname='.$this->config->DB_NAME;
$this->pdoString .= ';host='.$this->config->DB_HOST;
$this->connection = new PDO($this->pdoString, $this->config->DB_USERNAME, $this->config->DB_PASSWORD);
}


public function query($q)
{
    return $this->connection->query($q);
}


function __destruct()
{
$this->connection = NULL;
}
}
?>
Finally, I created a calling page to see how it ran:

<?php
require_once 'database.class.php';
$db = new database;
$arr = $db->query('SELECT * FROM users');
foreach($arr as $row)
{
echo $row['username'].'<br />';
}
?>
It's a start.


Friday 13 July 2012

OOP PHP RSS Reader

This is a simple RSS Reader which allows you to add multiple feeds. It also sorts the results before returning them to the calling page.
The calling page would look something like this:


<?php
require_once 'rss.class.php';
$addr1 = 'http://feeds.bbci.co.uk/sport/0/football/rss.xml';
$addr2 = 'http://feeds.howtogeek.com/HowToGeek';
$addr3 = 'http://feeds.feedburner.com/TheEdTechie';
$rss = new rss;
$rss->addFeed($addr1);
$rss->addFeed($addr2);
$rss->addFeed($addr3);
$arr = $rss->getFeed();
foreach($arr as $row)
{
echo $row->title.'-'.$row->pubDate.'<br />';
}
?>

So, here's the RSS Reader class:


<?php
class rss
{
public $addr = NULL;
private $outArr = Array();
function __construct($addr = NULL)
{
$this->addr = $addr != NULL ? $addr : $this->addr;
if($this->addr)
{
return $this->addFeed();
}
}


function addFeed($addr = NULL)
{
$this->addr = $addr != NULL ? $addr : $this->addr;
$rss = simplexml_load_file($this->addr);
$this->outArr = array_merge($this->outArr, $rss->xpath('/rss//item'));    
}


function getFeed()
{
usort($this->outArr, function ($x, $y)
{
if (strtotime($x->pubDate) == strtotime($y->pubDate)) return 0;
    return (strtotime($x->pubDate) > strtotime($y->pubDate)) ? -1 : 1;
});
return $this->outArr;
}
}
new rss;
?>

Wednesday 11 July 2012

HTML5 Audio Slideshow

Here, I've created a slideshow which syncs with a piece of audio. You can add different types of image and you can set the position in the audio when the image/slide appears. All free for you to play with.


<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
<title>HTML5 Audio Slideshow</title>
<!--[if IE]>
<script src="http://html5shim.googlecode.com/svn/trunk/html5.js"></script>
<![endif]-->
<link rel="stylesheet" href="http://meyerweb.com/eric/tools/css/reset/reset.css" />
<style>
audio img
{
display:none;
}
#display_meta
{
width:300px;
min-height:225px;
}
</style>
<script src="http://www.google.com/jsapi"></script>
<script>
google.load("jquery", "1");
google.load("jqueryui", "1");
</script>
</head>
<body>
<br />
<img id="display_meta" src="1.png" /><br />
<audio id="greeting" controls="controls">
<source src="http://dl.dropbox.com/u/17154625/greeting.ogg" type="audio/ogg" />
<img src="jimmy.png" data-seconds="2" />
<img src="johnnie.png" data-seconds="4" />
<img src="julie.png" data-seconds="6" />
</audio>
<script>
var allImages = new Array(3);
var inc = 0, foundie = 0;
$('audio img').each(function()
{
allImages[inc] = new Array(3);
allImages[inc][0] = $(this).attr('src');
allImages[inc++][1] = $(this).data('seconds');
});
(function()
{
var displayTime = function()
{
var rVer = Math.round(greeting.currentTime);
foundie = giveBackImageName(rVer);
if(foundie != 0)
{
document.getElementById('display_meta').src = foundie;
}
}

var giveBackImageName = function(vr)
{
for (var i = 0; i < allImages.length; i++)
{
    if(allImages[i][1] == vr)
    {
    return allImages[i][0];
    }
}
return 0;
}
$('#greeting').bind('timeupdate', displayTime);
})();
</script>
</body>
</html>