Thursday 28 June 2012

OOP PHP POST handler in 2 parts

Actually, I'm wrong before I've even started. The class I give you below will also handle GET requests too. I've described it in 2 parts. The first is some HTML and jQuery which can be used to test the class. Then there is the class itself.

The HTML delivers a form with an input field and submit button. It also contains a jQuery call to the POST handler. Here, I pass the method within the class which will be called to handle the data. Then I pass the data. In this case, the contents of the input field. Finally, An alert catches any data coming back from the class.

The POST handler accepts the POST request as and converts it into an object. It checks to see if a method has been called and that the method exists. If so, the method is invoked. If not, an error message is returned.

Enjoy!

The HTML


<!DOCTYPE html>
<html lang="en">
<head>
<title>Post test</title>
<style>
body
{
font-family:Sans-serif;
line-height:1.5em;
}
</style>
<script src="http://www.google.com/jsapi"></script>
<script>
google.load("jquery", "1");
google.load("jqueryui", "1");
</script>
</head>
<body>
<form>
<label for="myname">Type your name</label>
<input type="text" name="myname" id="myname" />
<button type="submit">Submit</button>
</form>
<script>
(function()
{
$('form').submit(function()
{
$.post('posthandler.php',
{
method:'getContent',
myname:$(this).find('#myname').val()
}, function(data)
{
alert(data)
});
return false;
});
})();
</script>
</body>
</html>

The Class


<?php
class posthandler
{
private $postObject;
function __construct($p)
{
$this->postObject = (object) $p;
if($this->postObject->method && (method_exists($this, $this->postObject->method)))
{
$evalStr = '$this->'.$this->postObject->method.'();';
eval($evalStr);
}
else
{
echo 'Invalid method supplied';
}


}
function getcontent()
{
echo $this->postObject->myname;
}
}
new posthandler($_POST);
?>

Monday 28 May 2012

Tabs using Twitter Bootstrap


I had been trying to search out a real world example of how tabs worked in Twitter Bootstrap and found that, by far, most examples missed some crucial elements. Below, I have supplied some code to paste into your Twitter Bootstrap site which you can play with.

<div class="row">
<div class="span16">
 <div class="tabbable" id="usernav">
 <ul class="nav nav-tabs">
<li class="active"><a href="#home" data-toggle="tab">Home</a></li>
<li><a href="#jobs" data-toggle="tab">Jobs</a></li>
 </ul>
 <div class="tab-content">
<div class="tab-pane active" id="home">
This is my home content.
</div>
<div class="tab-pane" id="jobs">
This is the jobs section.
</div>
 </div>
</div>
</div>
</div>

Friday 9 March 2012

A bit off the wall but who cares

This post won't appeal the the majority of people who follow my blogs, but here goes just the same.

If you type organic culture into the Google search bar, you get results which include fabrics from bamboo, pro-biotic foods, crops and gardening etc. Or to put it another way, semi-structures derived and evolved from existing natural materials.

I was having a conversation with a colleague this morning. We were talking about a system which requires someone to produce something (new) and then receive approval and feedback. The people who submit the products are also the people who will be approving/disapproving other people's products.

Let's just image for a moment that it's you who have been working long and hard on a product with all the best intentions. You are hoping to gain some credibility for your efforts. You send it out for approval. The feedback is negative, in places, missing the point that you have taken time and care to convey.

How likely are you to provide positive feedback to the next product you review?
How likely are you to provide positive feedback to the next product from the person who gave your work negative feedback?

Most organisations have a culture. An organisation I once worked for have a very positive, mutually supportive culture. As time went by, this culture was organically shifted to one which treated new ideas and efforts as an opportunity to pour scorn and score short term points.

Once this type of culture takes hold, it's a bit like fabrics from bamboo, pro-biotic foods, crops and gardening etc. Very difficult to untangle or remove, but for us who wish better for our working lives and for our organisations, we must. And we must replace it with a culture which gets behind something good, works with the originator and builds upon it to create excellence. Only then do we deserve and pat on the back.

Monday 5 March 2012

Multiple inheritance through PHP using traits

I actually took this code from http://php.net. Multiple inheritance has always been a bit dodgy for me in PHP. This is an excellent example of how to use traits to achieve it.

<?php
trait Hello {
    public function sayHello() {
        echo 'Hello ';
    }
}

trait World {
    public function sayWorld() {
        echo ' World';
    }
}

class MyHelloWorld {
    use Hello, World;
    public function sayExclamationMark() {
        echo '!';
    }
}

$o = new MyHelloWorld();
$o->sayHello();
$o->sayWorld();
$o->sayExclamationMark();>
?>

Friday 2 March 2012

Removing cache manifest in Chrome

In an earlier post, I explained how to use cache-manifest for localstorage. Once you've achieved this, you'll find that in order to continue editing the site that the cache manifest will need to be removed. Otherwise, you won't see your changes. To do this in Chrome, you need to put this command in the address bar:
chrome://appcache-internals/
This will list all the sites in your application cache with a 'remove' link.
A full list of such commands, enter chrome://chrome-urls/

Tuesday 13 December 2011

Invalid field count in csv input on line 1

I came across this annoying phpmyadmin 'feature' when trying to import some data to a newly created MySQL table. I had a CSV file full of data. The columns matched the number of fields but I kept getting the error 'Invalid field count in csv input on line 1'.

The problem was, that the 'Fields terminated by' field in the import screen was set to ';' instead of a ','. By resetting this field, everything worked.

Monday 5 December 2011

Reset the Index of a MySQL table

Sometimes, I will be playing about with a MySQL database, often at the start of an application. I'll fire a load of dummy data in and test. When I've finally done with this phase I'm ready to start from the beginning. I might have an ID field which I'd like to auto increment but this time starting from 0. To reset this field, first empty all the records in the table, then you can apply a line like this:
ALTER TABLE `mytable` AUTO_INCREMENT=0