Main Content

Choosing a railway station fairly in PHP

Archive - Originally posted on "The Horse's Mouth" - 2009-04-04 22:11:48 - Graham Ellis



I've been transferring the West Wilts Rail Users Group web site into a wiki this evening ... and came up with the question "How do I fairly choose which station to put on their front page".

Of course, I favour Melksham. It's far and away the one that needs all the help and support it can get from such groups, with just 2 trains a day each way for a population of over 20,000 (and compare that to an estimated 100 departures a day from Westbury - a town of half the size).

But - seriously folks - it has to be a randomly chosen station.

I don't know which is shown at the top of this article at the moment as it's fed by a PHP script that chooses at random:

<?php
header("content-type: image/jpeg");
$opts = array("avo","boa","tro","mkm","wes","dmh","war");
$last = $opts[rand(0,6)];
$fh = fopen("wwrug_$last.jpg","r");
$blow = fread($fh,filesize("wwrug_$last.jpg"));
print $blow;
?>


Want to learn more? Try our PHP Techniques Workshop