Scratch Card Game using php

Page 1 of 1

4 Replies - 12798 Views - Last Post: 27 January 2011 - 01:10 PM

#1 Denis1  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 32
  • View blog
  • Posts: 103
  • Joined: 29-July 09

Scratch Card Game using php

Posted 01 August 2009 - 04:39 AM

This tutorial uses the deprecated mysql extension (as of PHP 5.5.0) which should not be used for writing new code as it will be removed in the future. The mysqli (MySQL Improved) or PDO extension should be used instead.

Nevertheless, it has been decided to keep this tutorial available until such time as it has been either updated or re-written. In the meantime, if you wish to take the tutorial, then it is recommended that you change the mysql-extension code to use mysqli or PDO. (If you do this successfully then perhaps you might consider submitting a revised tutorial, contributing to dream.in.code and earning some Kudos!)




Hello
i am going to show how easy it is to make a scratch card using php this is more like a chance game so you get a chance to win prizes and a chance to lose.
I am going to be using money to buy the scratch card but you can use gold or what ever you want.

lets begin, as usual make sure you are connected to your database (mysql).

Step one // adding database fields in mysql.
-- 
-- Table structure for table `card`
-- 

CREATE TABLE `card` (
  `id` int(100) NOT NULL auto_increment,
  `name` varchar(200) NOT NULL default '',
  `money` bigint(255) NOT NULL default '0',
  PRIMARY KEY  (`id`)
) ENGINE=MyISAM AUTO_INCREMENT=6 DEFAULT CHARSET=latin1 AUTO_INCREMENT=6;

-- --------------------------------------------------------


Step two // make a page call new_game.php

on this page the user will start a new game and enter a name in to the name field that is on the database this way you can keep a track off all the players
<!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-Type" content="text/html; charset=utf-8" />
<title>Scratch Card</title>
</head>
<body>
<?

if(isset($_POST['new'])){

if (ereg('[^A-Za-z0-9]', $_POST['name'])) {
echo "Invalid Name only A-Z,a-z and 0-9 is allowed.";
}else{

$sql = "INSERT INTO card SET id = '', money = '100000', name = '" .mysql_real_escape_string($_POST['name'])."'";
$res = mysql_query($sql);

echo "<meta http-equiv=\"refresh\" content=\"0;URL=scratch_card.php\">";
?>
<table width="411" border="1" align="center">
<tr>
	  <td colspan="2" align="center">Start a New Game Of Scratch Card</td>
	</tr>
	<tr>
	  <td width="102">Choose a name</td>
	  <td width="293"><input name="name" type="text" class="entryfield" id="name" style='width: 98%;; border:1px solid #000000; font-size: 10px; font-family: verdana; width: 98%;' maxlength="20" /></td>
	</tr>
	<tr>
	  <td colspan="2" align="right"><input name="card" type="submit" class="button" id="card" onfocus="if(this.blur)this.blur()" value="Start" /></td>
	</tr>
  </table>
</body>
</html>


Step three // make a new page call scratch_card.php because when they click start on new_game.php it will refresh the page and take them to a game call scratch_card.php.

if you dont want to name this page scratch_card.php name it to what ever you want but be sure on the page new_game.php change
echo "<meta http-equiv=\"refresh\" content=\"0;URL=scratch_card.php\">";



"URL=scratch_card.php" to your choose name. unless you know php very well i suggest to just name your page scratch_php prevent errors.

Step four // on the new page scratch_card.php add the code below on top of your html code and your page

<form method="post">
<?php 
$buy=$_POST['buy'];

$sql=mysql_query("SELECT * FROM card WHERE name='$name'");
$fetch=mysql_fetch_object($sql);

$card1 = rand(1,3);
$card2 = rand(1,3);


$disp1="default";
$disp2="default";


if($go){
///// check if they have enought to play
if($fetch->money >= "10000"){
$new=$fetch->money-10000;
mysql_query("UPDATE card SET money='$new' WHERE name='$name'");
}else{
print "You dont have enough money to buy a scratch card.";
exit();
}
//// end money check
//// start card display

if($card1 == "1"){
$disp1="money";
}

if($card2 == "1"){
$disp2="money";
}


/// end settin dispaly

//// begin checkin if they have won a prize and if so give it out


$message="Shame you won nothing."; ///DEFAULT MSG
/// check money win ////////
if($card1 == "1" && $card2 == "1" ){
$message="You won £100,000.";
$win_money=$fetch->money+100000;
mysql_query("UPDATE card SET money='$win_money' WHERE name='$name'");
}


}/// for the first if




?>


to change the randomness of the game
you have to change
$card1 = rand(1,3);
$card2 = rand(1,3);


the higher you make the 3 on both once the less chance the player have of winning

Step five. // on the same page add the code below this is your design part of the whole game
<!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-Type" content="text/html; charset=iso-8859-1" />
<title>Scratch Card </title>
</head>

<body>
  <table width="400" border="0" align="center" cellpadding="0" cellspacing="2" class="table">
	<tr>
	  <td  class="head"><div align="center">Scratch Card </div></td>
	</tr>
	<tr>
	  <td><div align="center"></div>		<div align="center"></div>		
		<div align="center" class="cell">Try you odds and try a Scratch Card </div></td>
	</tr>
	
	<tr>
	  <td height="131" class="cell"><div align="center">
		<p>You Can Add An Image Here If You Want</p>
		<p class="cell" >Prizes :</p>
		<ul>
		  <li class="cell">£100,000</li>
		</ul>
		<p><? print"$message"; ?></p>
		<p>
<form method="post" action="">
<input type="hidden" value="go" name="go">
<input type="submit" class="submit" value="Buy Scratch Card For £7500">
</form>
		  </p>
</div></td>
	</tr>
	
</table>
</body>
</html>


And thats it all should work make sure every thing is in the same and make sure that the fields are added to the database
HOPE YOU LIKE IT :^:/>

This post has been edited by andrewsw: 01 February 2015 - 04:58 AM


Is This A Good Question/Topic? 0
  • +

Replies To: Scratch Card Game using php

#2 Joshdw  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 225
  • Joined: 23-March 09

Re: Scratch Card Game using php

Posted 24 September 2009 - 09:39 AM

Parse error: syntax error, unexpected $end in /home/****/public_html/new_game.php on line 34


what could this be

This post has been edited by Joshdw: 24 September 2009 - 02:44 PM

Was This Post Helpful? 0
  • +
  • -

#3 brandonc  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 12-September 10

Re: Scratch Card Game using php

Posted 12 September 2010 - 12:36 PM

change the code to this wan
<!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-Type" content="text/html; charset=utf-8" />
<title>Scratch Card</title>
</head>
<body>
<?

if(isset($_POST['new'])){

if (ereg('[^A-Za-z0-9]', $_POST['name'])) {
echo "Invalid Name only A-Z,a-z and 0-9 is allowed.";
}else{

$sql = "INSERT INTO card SET id = '', money = '100000', name = '" .mysql_real_escape_string($_POST['name'])."'";
$res = mysql_query($sql);

echo "<meta http-equiv=\"refresh\" content=\"0;URL=scratch_card.php\">";
}
?>
<table width="411" border="1" align="center">
<tr>
	  <td colspan="2" align="center">Start a New Game Of Scratch Card</td>
	</tr>
	<tr>
	  <td width="102">Choose a name</td>
	  <td width="293"><input name="name" type="text" class="entryfield" id="name" style='width: 98%;; border:1px solid #000000; font-size: 10px; font-family: verdana; width: 98%;' maxlength="20" /></td>
	</tr>
	<tr>
	  <td colspan="2" align="right"><input name="card" type="submit" class="button" id="card" onfocus="if(this.blur)this.blur()" value="Start" /></td>
	</tr>
  </table>
</body>
</html>


i dint test this so say if it works

This post has been edited by andrewsw: 01 February 2015 - 04:58 AM

Was This Post Helpful? 0
  • +
  • -

#4 Guest_Ralph Macho*


Reputation:

Re: Scratch Card Game using php

Posted 22 January 2011 - 01:28 PM

this works. Just missing a closing bracket.

<!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-Type" content="text/html; charset=utf-8" />
<title>Scratch Card</title>
</head>
<body>
<?

if(isset($_POST['new'])){

    if (ereg('[^A-Za-z0-9]', $_POST['name'])) {
        echo "Invalid Name only A-Z,a-z and 0-9 is allowed.";
    }
    else    {
    
            $sql = "INSERT INTO card SET id = '', credits = '100000', name = '" .mysql_real_escape_string($_POST['name'])."'";
            $res = mysql_query($sql);
            
            echo "<meta http-equiv=\"refresh\" content=\"0;URL=scratch_card.php\">";
            }
}
?>
<table width="411" border="1" align="center">
<tr>
	  <td colspan="2" align="center">Start a New Game Of Scratch Card</td>
	</tr>
	<tr>
	  <td width="102">Choose a name</td>
	  <td width="293"><input name="name" type="text" class="entryfield" id="name" style='width: 98%;; border:1px solid #000000; font-size: 10px; font-family: verdana; width: 98%;' maxlength="20" /></td>
	</tr>
	<tr>
	  <td colspan="2" align="right"><input name="card" type="submit" class="button" id="card" onfocus="if(this.blur)this.blur()" value="Start" /></td>
	</tr>
  </table>
</body>
</html>


Was This Post Helpful? 0

#5 Denis1  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 32
  • View blog
  • Posts: 103
  • Joined: 29-July 09

Re: Scratch Card Game using php

Posted 27 January 2011 - 01:10 PM

Yes thanks. Will be revising this script and making some improvements.

This post has been edited by andrewsw: 01 February 2015 - 04:57 AM
Reason for edit:: Removed previous quote, just press REPLY

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1