School Assignment? Project Due Tomorrow? Chat LIVE With A Programming Expert!

Welcome to Dream.In.Code
Become an Expert!

Join 307,038 Programmers for FREE! Get instant access to thousands of experts, tutorials, code snippets, and more! There are 2,219 people online right now. Registration is fast and FREE... Join Now!




Scratch Card Game using php

 
Reply to this topicStart new topic

> Scratch Card Game using php

Denis1
Group Icon



post 1 Aug, 2009 - 03:39 AM
Post #1


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.
CODE
--
-- 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
CODE
<!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
CODE
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

CODE
<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
CODE
$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
CODE
<!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 icon_up.gif
Go to the top of the page
+Quote Post


Register to Make This Ad Go Away!

Joshdw
**



post 24 Sep, 2009 - 08:39 AM
Post #2
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 Sep, 2009 - 01:44 PM
Go to the top of the page
+Quote Post


Reply to this topicStart new topic
2 User(s) are reading this topic (2 Guests and 0 Anonymous Users)
0 Members:

 


Lo-Fi Version Time is now: 11/21/09 08:49AM

Live Help!

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter Fan Us On Facebook

Tutorials

Programming

Web Development

Reference Sheets

Code Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month