Friday, 29 July 2011

PDO mysql_num_rows equivalent

So I'm re-doing a web app using PDO rather than the built-in MySQL stuff as there seems to be a possibility that the MySQL extension will be gone before too long.

Thing is I do a lot, and I mean a hell of a lot, of checking to see how many rows are returned after a SELECT... and PDO doesn't support mysql_num_rows.

So after looking around I discovered that if I alter my SQL I can check on one line thus:

$dbconn->query("SELECT COUNT(*) FROM `something` WHERE `condition` = 'something'")->fetchColumn()

Which'll give me the number of matching records. I can then check that against a condition and carry on... or not as the case may be.

Monday, 25 July 2011

Throwback

Neanderthal child

My Aunt Joan has always said I was something of a throwback, she said I was like a Viking or some other sort of big, gruff warrior type of geezer - though she always said I was a gentle giant type-thing as well. Looks like all us non-africans are to some extent according to this article I found on disinformation which discusses this article on io9 by Alasdair Wilkins.

Except that I'm a throwback to a later period in history, which can only be good I guess... unless it means that everyone else has evolved further... ahh well, at least I'm not dragging my knucles along the ground!

Rule 34

So I got my present job on the back of an interview. It was an interview that was more like a conversation and I found myself interrupting Lars and him interrupting me as we discussed various technologies or approaches to programming. I got off my bike at The Judge School after listening to WHOURKR (which I'd discovered via Transmission Zero) and I was hyped and sweating buckets thanks to cycling to the beat and being really quite excited. We got to talking and it was like I was entering a whole new world, a world where people didn't actively listen to me when I was rattling on about this IT stuff which got me so excited.

Reading Charles Stross is ever-so-much like this. I find myself having to put down my iPad (running Kindle) and just laughing aloud at the way he's inserted some techno-speak into my head, and inserted it without having to go through the loops of having to explain it to me as I already grok it.

My Ma loves his books too, particularly the Laundry series, and I guess he must do the same there as she's an avid reader of thriller and crime novels so I guess she gets the references to spy novels on an instinctive level as well - I too have read my fair share of John Le Carre and I get it too!

Rule 34 though, the follow up to Halting State, is superb! I guess I shouldn't really be reviewing it as I've not quite finished, but there was at least two moments last night where the iPad went down and I just had to sit back and appreciate how he'd got into my head.

Well done Sir!

Wednesday, 20 July 2011

PHP float to money and back again

So I've a need of converting a float from a MySQL query into a currency value so it'll print nicely. THis works a treat:

$dosh = number_format($float, 2);

But bringing it back into the table means I searched and found this way:

$float = (float) str_replace(',', '', $dosh);

Seemed to work a treat TBH ;-)

Sunday, 17 July 2011

Progress Bar written in PHP using GD and TTF fonts

So I had a need of a progress bar to show the user their progress through a series of pages filling in data. I'm sure I'd seen something similar before but I wanted to do it myself - what's more I wanted to use the type-face that I'd used for the rest of the site.

The following script can be called whatever you want but does require a few parameters in it's calling. The parameters are width, height and percentage. The width and height are the dimension of the required image in px.

It does require that a typeface be discoverable - it might be best to have the typeface in the same directory as the script, at least that works for me. It's a little down and dirty and doesn't do any checking - which is should really, especially for the existance of the correct GD library and the correct parameters etc...

<?php
  $percentage = $_GET['percentage'];
  $width = $_GET['width'];
  $height = $_GET['height'];
  $progress = imagecreate($width, $height);
  imageantialias($my_img, true);
  $black = imagecolorallocate($progress, 0, 0, 0);
  $white = imagecolorallocate($progress, 255, 255, 255);
  $grey = imagecolorallocate($progress, 204, 204, 204);
  $lGrey = imagecolorallocate($progress, 221, 221, 221);
  imagefilledrectangle($progress, 0, 0, $width, $height, $black);
  imagefilledrectangle($progress, 1, 1, ($width - 2), ($height - 2), $white);
  imagefilledrectangle($progress, 1, 1, (($percentage/100) * ($width - 2)), ($height - 2), $grey);
  $text = $percentage." %";
  $font = 'Gothicb.TTF';
  $font_size = 12;
  $text_box = imagettfbbox($font_size, 0, $font, $text);
  $text_width = $text_box[2]-$text_box[0];
  $text_height = $text_box[7]-$text_box[1];
  $x = ($width/2) - ($text_width/2);
  $y = ($height/2) - ($text_height/2);
  imagettftext($progress, 12, 0, ($x + 1), ($y + 1), $lGrey, $font, $text);
  imagettftext($progress, 12, 0, $x, $y, $black, $font, $text);
  header("Content-type: image/png");
  imagepng($progress);
  imagecolordeallocate($black);
  imagecolordeallocate($white);
  imagecolordeallocate($grey);
  imagecolordeallocate($lGrey);
  imagedestroy($progress);
?>

Sunday, 10 July 2011

R.I.P News of the Screws

So this week the news has been full of the News of the Screws phone hacking thing. I'm not overly sure that such misuse of the term "hacking" is appropriate — from my somewhat limited understanding it was merely a question of investigators using the default passcode to listen to a mobile phone users voice mail…?

Whether that deserves the application of the term "hacking" rather than simply being "sneaky" I'm not too sure, neither am I sure that the victims of the alleged hacking aren't overly sensible — surely if everyone was given the same pin when issued a debit/credit card they would take the first opportunity to change that there pin wouldn't they? Not 100% analagous but I think it's similar enough to bear consideration.

Anyway… so News International decided to sweeten things by closing down the News of the Screws early (what's the betting that the Sun on Sunday was going to be launched soon anyway — and what's the betting that it'll have a similar roll to the News of the World as well?), not least because they're trying to take over BSkyB and want to avoid any hint of scandal as well as show proper contrition.

That does leave us with something of a quandary about privacy though… not too long ago the news was full of super-injunctions — the method by which the rich and/or famous were able to keep their actions out of the press — and the furore that that evoked when made public. Now we've got allegations of investigators using underhand/sneaky methods of obtaining information which anyone else could obtain if they had a total dis-regard for the privacy of others and a morality bypass.

We've also got the promise that the coalition will launch an enquiry — does anyone else see the link here?

Admittedly I'm something of a conspiracy-theory aficionado but wouldn't it be interesting if we threw the baby out with the bath-water when the enquiry returns suggestions that privacy be upped a notch, thus doing away with the requirement for super-injunctions?

What if, by our own carelessness, we ushered in stricter privacy laws in order to protect those of us who're too trusting or not technically savvy enough to change their pin?

I don't know about the legality of the phone "hacking", though I suspect it isn't particularly legal, but I do know that the next time our Dear Leader has a meeting with — just as a for-instance — Rupert Murdoch or — another for-instance — Rebekah Brooks then I'd like to be told! I dare say though, that in the future such reporting might be considered illegal and we'd all be the poorer for it being so.

I'm not defending the News of the Screws (though I did sort of enjoy reading it on a night-shift) but I am wary about what this might lead to — if anything I really, really don't want legislation to be created as a result, rather I'd like less legislation. I'd like people to be a little more aware of their responsibilities in regards to their own security. I'd really, really like News International to lose all investment and market share — I for one am thinking of boycotting anything which advertises in those outlets, I'd suggest that others do as well, I just need a register of who advertises with them now…? (Just hope it's not Tesco or I'll starve!)

Buying anything from News International is something I'll also avoid now! Thankfully I don't buy a paper but what else do they do?