Location: PHPKode > scripts > MageSource's CardDeck > magesources-carddeck/example.php
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
	<head>
		<meta http-equiv="Content-Language" content="en-us" />
		<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
		<title>MageSource's Example</title>
	</head>
	<body>
		<?php
			require_once('includes/cardDeck.php'); //include the class file
			//I would view source of the HTML document in your browser just to see it all in HTML form.
			session_start(); //start the session (only using sessions becuase I did not want to write a stateless method. This was just to show you the class. I do not recomend you ever loading an object into a session.
			if(!(isset($_SESSION['ourDeck']))) //if our session is not set, create one.. make a deck, shuffle it, and create a 'discard' pile to pull drawn cards from.
			{
				$_SESSION['ourDeck'] = new CardDeck(false); //create a deck with jokers and store it in a session so on page load the deck remains.
				$_SESSION['ourDeck']->shuffleDeck(); //shuffles the deck
				$_SESSION['drawnCards'] = array(); //a place to store drawn cards.
			}
			else
			{
				$_SESSION['drawnCards'][] = $_SESSION['ourDeck']->drawCard(); //on page load, if we have a cardDeck draw a card and store it in the discard pile.
			}
			
		?>
		<p>Click me to draw a card</p>
		<?php
			if($_SESSION['ourDeck']->getSelectedIndex() == -1){ //the class returns a -1 when you run out of cards.
				echo "\n\t\t\t<img alt='No Cards' src='images/nocards.png' width='72' height='96' />\n"; //there are no cards left, so don't make the image a link to refresh.
			}
			else{
				echo "\n\t\t\t<a href='index.php'><img alt='Draw Card' src='images/facedown.jpg' width='72' height='96' /></a>\n"; //the deck has cards in it, keep clicking.
			}
		?>
		<hr />
		
			<p>
				Our deck has: <?php echo $_SESSION['ourDeck']->getSelectedIndex() + 1;//zero index solution (+1) ?> cards left!
			</p>
			<p>
				The last drawn card has these properties:<br />
				Card Name:  <?php echo $_SESSION['ourDeck']->getCardName(); //all these functions can include a string index (see documentation) if you want information about a specific card ?><br /> 
				Card Suit: <?php echo $_SESSION['ourDeck']->getCardSuit(); ?><br />
				Card Color: <?php echo $_SESSION['ourDeck']->getCardColor(); ?><br />
				Card Value: <?php echo $_SESSION['ourDeck']->getCardValue(); ?><br />
			</p>
			<hr />
		<div style='padding-top: 30px'>
			<?php
				echo "\n"; //I like formatting my HTML when source is viewed in a readable manner. this is simply my display whats been drawn loop. look at how I am feeding a string index (contained in $_SESSION['drawnCards'] to get the graphic and card name.
				$rows = -1; //just so that everything displays right, I only wanted 8 cards in the column.
				for($i = 0; $i < sizeof($_SESSION['drawnCards']); $i++) //loops through the drawn cards.
				{
					if($rows == 7) {echo "\t\t\t<br />\n"; $rows = -1;} //if our row has eight cards add a break so it "seems" to draw only 8 per row.
					//for new programmers this may seem like alot. but what im doing is taking a card stored in drawncards and using the object method get card name to put that name in the "alt" attrib. Same for the graphic, just feeding it the value in the discard array.
					echo "\t\t\t<img alt='" . $_SESSION['ourDeck']->getCardName($_SESSION['drawnCards'][$i]) . "' src='" . $_SESSION['ourDeck']->getCardGraphic($_SESSION['drawnCards'][$i]) . "' width='72' height='96' />\n";
					$rows++; //increment rows.
				}
			?>
		</div>
</body>
</html>
			
Return current item: MageSource's CardDeck