Vancouver Canada Server Colocation, Managed Services, Dedicated Servers, Binary Environments Ltd.
SITE NAVIGATION
HOME                
ABOUT                
CONTACT                
SERVICES                
SUPPORT                
REGISTER A DOMAIN

OTHER BEL SITES
XODIGITALCOURIER
DOT SOLUTIONS
WIGWAGS
SEARCH THIS SITE




HOME    ABOUT    CONTACT    SERVICES    SUPPORT    

Pick A Card PHP class

Here is the code we use for Tarot Card of the Day and Rune of the Day, though what we run is adapted a bit to 'plug in' to our site code. What's presented here is seriously minimal. It will run, but it won't be kosher html, so if you're using it, you may with to adapt it to your particular needs.

The class requires a configuration file which defines the deck (card names and descriptions), as well as the address of the application and the location of the images directory. If using a graphic type other than jpg, you can define the extension used as well. We'll include it in the example below for purposes of illustration, though it defaults to jpg so it's not really necessary for this example.

Images should be named by two digit number, 00.jpg, 01.jpg, 02.jpg, etc. Yes, we start counting with 0 as the first card.

Here is what should be in the config file.

<?php
define('CARDIMGDIR','./images/tarotcards');		
define('OURURL',$_SERVER["REQUEST_URI"]);
define('GRAPHICEXT','jpg');

$cards["0 The Fool"] = "Fools rush in where angels fear to tread.
They may accomplish things angelic wisdom cannot.  Or they may 
fail spectacularly, causing great woe to themselves or others.";
		
$cards["I The Magician"] = "Focus the will and concentrate awareness
to achieve fulfillment of your vision.";
		
$cards["II The High Priestess"] = "Listen to your intuition.";
		
$cards["III The Empress"] = "Abundance awaits.";
		
$cards["IV The Emperor"] ="Determine the structure, framework, system
of constraints, within which the objective will
be most easily achieved.";

// [ . . . . and so on until all the cards are defined ]

?>

And here is the class.

<?php
/*
pickacard

copyright 2007 Eric Pettifor 

This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.
http://www.gnu.org/licenses/gpl.html
or write to the Free Software Foundation, Inc.,
59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
*/

class Pickacard {
	private $cards,$numCards,$keys,$numKeys,$ext;
	public $cardImgDir,$cardNum,$cardName,$cardDescription,$cardImg;
	public $cardImgFullPath,$ourURL;

	function __construct($confFile) {
		require_once($confFile);
		$this->cards = $cards;
		$this->keys = array_keys($cards);
		$this->numKeys = count($this->keys);
		if ( defined("CARDIMGDIR") ) $this->cardImgDir = CARDIMGDIR;
		if ( defined("GRAPHICEXT") ) $this->ext = GRAPHICEXT;
		else $this->ext = 'jpg';
		if ( defined("OURURL") ) $this->ourURL = OURURL;
	}

	public function setCard($num="") {
		if ( is_null($num) || ! is_int($num) ) $num = $this->getRandom();
		$this->cardNum = $num;
		$this->cardName = $this->keys[$num];
		$this->cardDescription = $this->cards[$this->cardName];
		$cardDisplayNum = sprintf("%02d", $num);
		$this->cardImg = $cardDisplayNum.'.'.$this->ext;
		$this->cardImgFullPath = CARDIMGDIR.'/'.$this->cardImg;
	}

	private function getRandom() {
		mt_srand((double)microtime()*1000000);	
		// rand((double)microtime()*1000000);	
		$n = mt_rand(0,$this->numKeys);
		return $n;
	}

} // end pickacard class
?>

And here is some code that uses the class

<?php
$CONFIGFILE = './tarotcards.conf';

$deck = new Pickacard($CONFIGFILE);
$deck->setCard();

$table = '<p align="center">'."\n";
$table .= '<table width="550"><tr><td width="152">'."\n";
$table .= '<img src="'.$deck->cardImgFullPath.'" alt="'.$deck->cardName;
$table .= '"/></td><td>'."\n";
$table .= '<p>'.$deck->cardDescription."</td></tr>\n";
$table .= '<tr><td align="center"><b>'.$deck->cardName;
$table .= "</b></td></tr>\n";
$table .= '<tr><td colspan="2" align="center">';
$table .= '<a href="'.$deck->ourURL.'">pick again</a></td></tr>';
$table .= "</table>\n";

print $table;
?>

This class can be used for any deck you define, though it would have to be tweaked for decks of more than 100 cards. With regard to the Waite-Colman Smith deck, there is some debate concerning copyright, so we're going to be chickenshits and not offer you a tarball of the graphics. You probably know how to get them from this site, and you can also find them conveniently at the above wikipedia link (or that was the case at time of writing).



Return to BEL's Things of Interest


©Copyright 1998-2006 Binary Environments Ltd.