Lottery System
Hey there!
This is just a tutorial to show you how to make a lottery system to go along with your Point system/shop.
First off, you will want to open up your phpMyAdmin and enter the following query:
Code
CREATE TABLE `lottery` (
`id` INT( 11 ) NOT NULL auto_increment,
`owner` VARCHAR( 255 ) NOT NULL,
PRIMARY KEY(`id`)
);
Next, open your config file and add:
Change the 5 to however many points you want the lotto tickets to cost.
Next make a new file in your main DIR named lottery.php and in it code something like..
PHP Code
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
<?php
session_start(); //allow sessuibs
include("config.php"); //get database
switch($_GET[act]){ //make links ?act=casename
default: //make default page
$total_tickets = mysql_num_rows(mysql_query("SELECT * FROM `lottery` ORDER BY `id` DESC")); //get the total tickets in the database
$total_points = $total_tickets * $lottopts; //turn the number into real points :)
$total_user_owns = mysql_num_rows(mysql_query("SELECT * FROM `lottery` WHERE `owner` = '$logged[username]';")); //get how many the logged in user has
echo "<form method=\"post\" action=\"lottery.php?act=buy\">
<table width=\"350\" border=\"1\" style=\"border: 1px solid #000000;\">
<tr>
<td width=\"80\"><b>Name</b>:</td>
<td width=\"254\">Lottery Ticket</td>
</tr>
<tr>
<td><b>Description</b>:</td>
<td>Buy a ticket see how much you can win or lose :)</td>
</tr>
<tr>
<td><b>Price</b>:</td>
<td>$lottopts Point(s)</td>
</tr>
<tr>
<td><b>You Own</b>:</td>
<td>$total_user_owns Ticket(s)</td>
</tr>
<tr>
<td><b>Prize</b>:</td>
<td>$total_points Points (So Far!)</td>
</tr>
<tr>
<td><input type=\"text\" maxlength=\"2\" size=\"14\" name=\"amount\"> <input type=\"submit\" name=\"buy\" value=\"Buy!\"></td>
</tr>
</table>"; //echo the form for buying, some general information
break; //End the default page for buying tickets
case 'buy': //the buy page
if($_POST[buy]){ //if they submitted the form
$amount_bought = (int) $_POST[amount]; //get the amount bought
if($amount_bought > '10'){ //if the user bought more than 10
echo "You Can Not Buy More Then 10 Tickets"; //tell them they can not get more than 10
}else{ //or if they bought exactly or less than 10
$math = $amount_bought * $lottopts; //times the amount bought by how much the lotto ticket costs
if($logged[points] < $math){ //if they do not have enough points
echo "You Do Not Have Enough Points."; //tell them
}else{ //or if they do have enough
$rmv_pts = $logged[points] - $math; //get rid of points from user hand.....
if($rmv_pts < '0'){ //check points just in case
echo "You Do Not Have Enough Points!"; //if they don't have enough points
}else{ //or if they do have enough
$update_table = mysql_query("UPDATE `members` SET `points` = '$rmv_pts' WHERE `username` = '$logged[username]';"); //update users points :S
for($i = 1; $i <= $amount_bought; $i++){ //$i is 1, but then i is less than or equal to the amount bought then $i is increased by one UNTILL it is the same number as the amount bought :)
$insert = mysql_query("INSERT INTO `lottery` (`owner`) VALUES ('$logged[username]');") or die(mysql_error()); //insert or die >:O
} //end for function
echo "You Have Bought $amount_bought Ticket(s)"; //tell them how many they bought.
} //end second points check
} //end points check
} //end amount check
}else{ //or if they didn't
echo "You Can Not Visit This Page Directly"; //give them what they deserve!
} //end form check
break; //end the buy page
}
?>
Note: put this next file in the /home/YOURUSERNAME/ directory so no body can get to it! And i used different things for the Cron jobs filename and username thingo.
name this file LOTTERYcron.php
PHP Code
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
<?php
include("public_html/config.php"); //get config from regular site area
$admin = "your_name"; //put your username there
$date = date("d-m-y"); //get todays date
$totaltickets = mysql_num_rows(mysql_query("SELECT * FROM `lottery`")); //get total tickets
$query_lotto = mysql_query("SELECT * FROM `lottery` ORDER BY RAND() ASC LIMIT 1");
$prize = $totaltickets * $lottopts;
$gw = mysql_fetch_array($query_lotto); //get the winner :)
$winner = $gw[owner]; //winner's username
$updateuser = mysql_query("UPDATE `members` SET `points` = `points` + $prize WHERE `username` = '" . $winner . "'"); //update the users points
$msg = "Hey There $winner,
<b>Congradulations! You've Won The Lottery!</b>
Total Prize: $prize.";
$pmmem = mysql_query("INSERT INTO `privates` (`to`,`from`,`date`,`subject`,`content`) VALUES ('" . $winnder . "','" . $admin . "','" . $date . "','Lottery Notice','" .$msg . "')") or die(mysql_error()); //insert pm into table for user
$reset = mysql_query("TRUNCATE `lottery`"); //empty the table
?>
Next open up Cpanel and go to Cron Jobs,
Cpanel 11:
Cpanel 10:
After that select Advanced (Unix Style) and enter the following:
Cpanel 11:
Cpanel 10:
be sure to change
YOURUSERNAME and
YOURNAME to your Cpanel username.
That should be it for that...