Kevin Nelson Marshall
Other entries:
« You gotta know a lot...

I've got a lot of really exicting stuff in the works right now, and because I'm anxious to get back into building them (so I can then release them and introduce you to them), I'm going to take the easy way out tonight and just post a PHP version of the Perl key generation script I shared the other day.

It basically does all the same things as the script I mentioned the other (takes an integer and generates a base 36 key), it's just in PHP this time around. I did this version because the front end of the pu.ly site is actually in PHP (and the backend monitoring scripts are in Perl)...so I needed a version of the code in both to keep everything in sync. depending on where/when a record would be inserted into the long tweets table.

Anyway here's the code, enjoy.

<?
function base_conv($num) {
  $remainder = $num % 36;
  $value = round($num / 36);
  $row = array($value, $remainder);
  return $row;
}

function generate_key($id) {
  $continue = 1;
  $slot = 0;
  $key = "";
  $bits = array();
  $control = array();
  $remainder = 0;
  while ($continue) {
    $check = base_conv($id);
    $id = $check[0];
    $remainder = $check[1];
    array_push($bits, $remainder);
    if ($id == 0) { $continue = 0; }
  }
  foreach (range('A','Z') as $value) {
    $control[$slot] = "$value";
    $slot++;
  }
  foreach (range(0,9) as $value) {
    $control[$slot] = "$value";
    $slot++;
  }
  foreach ($bits as $rev) {
    $key .= $control[$rev];
  }
  return $key;
}

$id = 0;
if (isset($_REQUEST['id'])) { $id = $_REQUEST['id']; }
$key = generate_key($id);
// just show the generated key to the user
echo $key;
?>

posted by Kevin Marshall on 2009-08-03 00:00:00+00


Subscribe to my RSS feed »

BotFu feed with RSS reader

BotFu feed by Email


Search All Posts »


Kevin Marshall - Who's That?

I'm just your basic programmer. I can't spell to save my life, I'm not the greatest story teller, and I often ramble on about nothing. This blog showcases all of that!

If you're bored drop me an email at info at falicon.com or view my outdated resume.


Stalk Kevin on »

bit.ly
Delicious
Digg
Disqus
Facebook
Flickr
FriendFeed
Github
Last.fm
LinkedIn
StumbleUpon
Twitter (@falicon)

Archives by Category »

(28) Code »
(8) ColdFusion »
(15) Database »
(10) Factor »
(3) Falcons »
(321) General »
(13) JavaScript »
(18) Perl »
(17) PHP »
(20) Ruby »

Archives by Month »

(1) September 2010 »
(2) August 2010 »
(3) July 2010 »
(13) June 2010 »
(8) May 2010 »
(2) April 2010 »
(2) March 2010 »
(5) January 2010 »
(2) October 2009 »
(6) August 2009 »
(11) July 2009 »
(2) May 2009 »
(3) April 2009 »
(2) March 2009 »
(7) February 2009 »
(9) January 2009 »
(14) December 2008 »
(5) November 2008 »
(12) October 2008 »
(13) September 2008 »
(16) August 2008 »
(23) July 2008 »
(20) June 2008 »
(24) May 2008 »
(23) April 2008 »
(27) March 2008 »
(28) February 2008 »
(26) January 2008 »
(7) December 2007 »

Published Works »

Beginning Amazon's SimpleDB (Apress in dev.)
Pro Active Record (Apress 2007)
Web Services with Rails (O'Reilly 2006).

Contributed To »

Ruby Cookbook (O'Reilly 2006)
SQL Cookbook (O'Reilly 2005)
Various Reviews published in Computing Reviews

Free Code I've Created »

SimpleDB library in Python 3.0



This blog is powered by KickAssCode.