Forgot Password / Register
Site Statistics
Total Members: 520
Total Tutorials: 242
Newsest User: 8884244477
Todays Unique Hits: 759
0 Users 2 Guests Online

Badge System

Welcome to the Badge System tutorial. This was a request. So here it its!

FIRST. Run these SQL queries

Code
CREATE TABLE `badges` (
`id` int(11) NOT NULL auto_increment,
`name` varchar(255) NOT NULL default '',
`url` varchar(255) NOT NULL default '',
`desc` text NOT NULL default '',
PRIMARY KEY (`id`)
) TYPE=MyISAM;

CREATE TABLE `ubadges` (
`id` int(11) NOT NULL auto_increment,
`username` varchar(30) NOT NULL default '',
`bname` varchar(255) NOT NULL default '',
PRIMARY KEY (`id`)
) TYPE=MyISAM;


Then let's call this: badge_admin.php

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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
<?php 
session_start
(); //allow sessions.
include("config.php"); //get config
if($logged[username] && $logged[userlevel] ==6// allows level 5 to view this page
if (!$_POST[update])

# If User Is Logged In

switch($_GET[badge])
{
# Lets Us Use One Page For Everything

default:
# The Default Page

echo("<b><u>Current Badges</u></b><br><br>");
$getbadges mysql_query("SELECT * FROM badges");
while(
$badge mysql_fetch_array($getbadges))
{
echo(
"Badge Name: $badge[name]<br>
Badge Description: 
$badge[desc]<br>
Badge: <img src=\"
$badge[url]\"><br><br>
<a href=\"?badge=delete&id=
$badge[id]\">Delete The $badge[name] Badge</a> - <a href=\"?badge=edit&id=$badge[id]\">Edit The $badge[name] Badge</a><br><br>");
}
echo(
"<br><b><u>More Options</u></b><br>");
echo(
"<a href=\"?badge=create\">Create A Badge</a><br>
<a href=\"?badge=give\">Give User A Badge</a><br>
<a href=\"?badge=deletebadge\">Take Badge away from member</a><br>"
);
# Options

break;
# End Default Page

case 'edit':
# Create A Badge Page
if ($_GET[id]) {
if(!
$_POST[ed]) {
$getbadgeid mysql_query("SELECT * FROM badges WHERE id = '$_GET[id]'");
$b mysql_fetch_array($getbadgeid);
echo(
"<form method=\"POST\">
Badge Name: <input type=\"text\" value=\"
$b[name]\" name=\"bname\"><br>
Image URL: <input type=\"text\" name=\"burl\" value=\"
$b[url]\"><br>
Badge Description: <input type=\"text\" name=\"bdesc\" value=\"
$b[desc]\"><br><br>
<input type=\"submit\" name=\"ed\" value=\"Update Badge\">
</form>"
);
# The Create A Badge Form

}else{
# The Form Has Been Submitted
$ub2 mysql_query ("UPDATE `badges` SET `name` = '$_POST[bname]', `url` = '$_POST[burl]', `desc` = '$_POST[bdesc]' WHERE `badges`.`id` =$_GET[id]");
$ub3 mysql_query ("UPDATE `ubadges` SET `bname` = '$_POST[bname]' WHERE `ubadges`.`bid` = $_GET[id]");
echo(
"Badge $_POST[bname] updated");
# Inserts New Badge
}
}
break;
# End Create Page

case 'create':
# Create A Badge Page

if(!$_POST[create]) {
echo(
"<form method=\"POST\">
Badge Name: <input type=\"text\" name=\"bname\"><br>
Image URL: <input type=\"text\" name=\"burl\" value=\"http://www.\"><br>
Badge Description: <input type=\"text\" name=\"bdesc\"><br><br>
<input type=\"submit\" name=\"create\" value=\"Create Badge\">
</form>"
);
# The Create A Badge Form

}else{
# The Form Has Been Submitted

$updatebadges mysql_query("INSERT INTO badges (`name`, `url`, `desc`, ) VALUES ('$_POST[bname]','$_POST[burl]','$_POST[bdesc]')");
echo(
"A new badge has been set up!");
# Inserts New Badge

}
break;

case 
'delete':
# Delete Page

$getbadgeid mysql_query("SELECT * FROM badges WHERE id = '$_GET[id]'");
$c mysql_fetch_array($getbadgeid);
# Get Badge ID

$deletebadge mysql_query("DELETE FROM badges WHERE id = '$_GET[id]'");
echo(
"The $c[name] badge has been deleted!");
# Delete Badge

break;
# End Delete Page

case 'give':
# Give Page

$getbadges mysql_query("SELECT * FROM badges");
$g mysql_fetch_array($getbadges);
# Checks Table

if(!$_POST[apply]) {
echo(
"<form method=\"POST\">
Badge Name: <select name=\"bname\">"
);
$badges mysql_query("SELECT * FROM badges");
while(
$b mysql_fetch_array($badges))
{
echo(
"<option value=\"$b[name]\">$b[name]</option>");
}
echo(
"</select><br>Give To: <select name=\"give\">");
$users mysql_query("SELECT * FROM members");
while(
$mem mysql_fetch_array($users))
{
echo(
"<option value=\"$mem[username]\">$mem[username]</option>");
}
echo(
"</select><br><br><input type=\"submit\" name=\"apply\" value=\"Give Badge\">
</form>"
);
# The Give A User A Badge Form

}else
# The Form Has Been Submitted

{
$checkbadge mysql_query("SELECT * FROM ubadges WHERE username = '$_POST[give]'");
$c mysql_fetch_array($checkbadge);
# Checks Table

if($c[bname] == $_POST[bname]) {
echo(
"$_POST[give] already has the $_POST[bname] badge!");
# Checks To See If User
# Already Has The Badge
}else{
$badgeinfo mysql_query("SELECT * FROM badges WHERE name = '$_POST[bname]'");
$d mysql_fetch_array($badgeinfo);
$givebadge mysql_query("INSERT INTO ubadges (`bname`, `username`,`bid`) VALUES ('$_POST[bname]', '$_POST[give]', '$d[id]')");
echo(
"$_POST[give] has received the $_POST[bname] badge!");
# Insert Badge

}
}
break;
# End Apply Badge Page

case 'deletebadge'//delete BADGE HELP
# Give Page

$getbadgeid2 mysql_query("SELECT * FROM badges WHERE id = '$_GET[id]'");
$cb mysql_fetch_array($getbadgeid2);
$name$cb[bname];
# Checks Table

if(!$_POST[apply]) {
echo(
"<form method=\"POST\">
Badge Name: <select name=\"bname\">"
);
$badges mysql_query("SELECT * FROM badges");
while(
$q mysql_fetch_array($badges))
{
echo(
"<option value=\"$q[name]\">$q[name]</option>");
}
echo(
"</select><br>Take From:

<input type='text' name='useraway'>
<input type=\"submit\" name=\"apply\" value=\"Take Away Badge!\">
</form>"
);
# the badge form


}else{
$deletebadge mysql_query("DELETE FROM ubadges WHERE username = '$_POST[useraway]' AND  bname = '$_POST[bname]'");
echo(
"$_POST[useraway] no longer has the $_POST[bname] badge!");
# Insert Badge

}
break;


}
# End Apply Badge Page
?>


Ok, we have the badge admincp sey up. Now onto the the next one. Let's call this badges.php.

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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
<?php

switch($_GET[view]){ 
    
    default:
echo 
"<style>
table {
background:transparent;
}
#myb {
background: #DCEEF8;
width:490px;
padding: 3px;
border: 1px solid #86C0DF;
color: #459AC8;
}
td#re {
border-bottom: 2px solid #A4C8DA;
padding-bottom: 5px;
border: 
}
</style><center>
<table id='myb'><tr style='font-weight:bold;' align='center'><td>Badge Name</td><td>Image</td><td>Description</td>"
;
$badges mysql_query("SELECT * FROM badges");
while(
$q mysql_fetch_array($badges))
{
echo(
"<tr align='center'>
<td id='re'>
<a href='?view=b3&id=
$q[id]' name='bname'>$q[name]</a></td><td id='re'>
<img src='
$q[url]' alt='$q[desc]' border='0'></td><td id='re'>$q[desc]<br></td>

</tr>"
);
}
echo 
"</table></center>";
break;
    
case 
'b3':

if (
$_GET[id]) { //get username
$badges2 mysql_query("SELECT * FROM badges WHERE id = '$_GET[id]' ");
$v mysql_fetch_array($badges2);
$get_admins mysql_query("SELECT * FROM `ubadges` WHERE `bid` = '$_GET[id]'");
while(
$admins mysql_fetch_array($get_admins)){ //make a loop
$getinfo2 mysql_query("SELECT * FROM members WHERE username = '$admins[username]' ");
$u mysql_fetch_array($getinfo2);
echo 
"<a href='members.php?user=$u[id]'>- $admins[username]</a><br>"//echo the admin with am link to profile.
//end our loop for admins

echo "<br>people who have earned this badge.";

}
break;

case 
'badges':

if (
$_GET[id]) {
$badges mysql_query("SELECT * FROM `badges`") or die(mysql_error()); //get all the messages to the loged in user
$meminfo mysql_query("SELECT * FROM `members` WHERE `id` = '$_GET[id]'");
$a mysql_fetch_array($meminfo);
echo 
"<div id=title>$a[username]'s  Badges</div><br><br><center>
<style>
table {
background:transparent;
}
#myb {
background: #DCEEF8;
width:490px;
padding: 3px;
border: 1px solid #86C0DF;
color: #459AC8;
}
td#re {
border-bottom: 2px solid #A4C8DA;
padding-bottom: 5px;
border: 
}
</style>
<table id='myb'><tr style='font-weight:bold;' align='center'><td>Badge Name</td><td>Image</td><td>Description</td>"
//echo the start5 table and create badge links
$mybadges mysql_query("SELECT * FROM `ubadges` WHERE `username` = '$a[username]'") or die(mysql_error());
if(
mysql_num_rows($mybadges) == 0){ //check if there areany badges
echo "<tr><td colspan='3' align='center' valign='middle'>They have not earned any badges!</td></tr>"//no new messages
}else{ //or if there are messages
while($r mysql_fetch_array($mybadges)){ //repeat for all the messages
$badges2 mysql_query("SELECT * FROM `badges` WHERE `name` = '$r[bname]'") or die(mysql_error());
$b mysql_fetch_array($badges2);
echo 
"<tr align='center'>
<td id='re'>
$r[bname]</td><td id='re'>
<img src='
$b[url]' alt='$b[desc]' border='0'></td><td id='re'>$b[desc]<br></td>

</tr>"
//echo the messages
//end while
//end message amount check
echo "</table></center>"//end table
}
break;



}
?>


Now this lets people view who has witch badge and which member has witch badge.

Who has witch badge: badges.php?view=b3&id=5
Show member's badges: badges.php?view=badges&id=1

The 1 in beside badges?id& is the member ID.

Last part is going to show you which badges you have earned. Let's call this: my_badges.php.

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
<?php
include ("config.php");
if(!
$logged[id]){ //Check if user is logged in
echo "<b>Error</b>: You Are Not Logged In!"//Not logged in
}else{ //Their loggedin
switch($_GET[page]){ //make some links ?page=case
default: //set up the default page upon going to pms.php
$badges mysql_query("SELECT * FROM `badges` ORDER BY `id` DESC") or die(mysql_error());
$mybadges mysql_query("SELECT * FROM `ubadges` WHERE `username` = '" $logged[username] . "' ORDER BY `id` DESC") or die(mysql_error()); //get all the messages to the loged in user
echo "<div id=title>My Badges</div> <br><br><center>
<style>
table {
background:transparent;
}
#myb {
background: #DCEEF8;
width:490px;
padding: 3px;
border: 1px solid #86C0DF;
color: #459AC8;
}
td#re {
border-bottom: 2px solid #A4C8DA;
padding-bottom: 5px;
border: 
}
</style>
<table id='myb'><tr style='font-weight:bold;' align='center'><td>Badge Name</td><td>Image</td><td>Description</td>"
//echo the start5 table and create badge links
if(mysql_num_rows($mybadges) == 0){ //check if there are any badges
echo "<tr><td colspan='3' align='center' valign='middle'>You have not earned any badges!</td></tr>"//no new messages
}else{ //or if there are messages
while($r mysql_fetch_array($mybadges)){ //repeat for all the badges
$badges2 mysql_query("SELECT * FROM `badges` WHERE `name` = '$r[bname]'") or die(mysql_error());
$b mysql_fetch_array($badges2);
echo 
"<tr align='center'>
<td id='re'>
$r[bname]</td><td id='re'>
<img src='
$b[url]' alt='$b[desc]' border='0'></td><td id='re'>$b[desc]<br></td>

</tr>"
;
//end while
//end badge amount check
echo "</table></center>"//end table
break; //end the default page

//end switch/get
//end login check
?>


So yeah, that's basically it.

Then finally to show a user's badges.
Code
http://SITENAME.com/badges.php?view=badges&id=$logged[id]


Enjoy!!
UrbanTwitch
Views:
2927
Rating:
Posted on Thursday 14th August 2008 at 11:37 PM
UrbanTwitch
UrbanTwitch
Yeah. Update it with the posts in the comments.
Posted on Thursday 14th August 2008 at 08:55 PM
DanielXP
DanielXP
Does this tutorial need editing. (i see you alreayd submitted it to P2L)
Posted on Thursday 14th August 2008 at 12:28 PM
Dalez
Dalez
Also, to just show the badges (on a users profile) use this code:

Code

$findbadges = mysql_query("SELECT * FROM ubadges WHERE username = '$_GET[user]'");
while($l = mysql_fetch_array($findbadges))
{
# Checks Table For Badges

$findinfo = mysql_query("SELECT * FROM badges");
while($p = mysql_fetch_array($findinfo))
{
# Checks Badges

if($l[bname] == $p[name]) {
echo("<img alt="$p[desc]" border="0" src="$p[url]"> ");
}

}
}


I don't think it needs any other code ^^
Posted on Tuesday 12th August 2008 at 05:13 PM
UrbanTwitch
UrbanTwitch
Also in sql

Code

CREATE TABLE `ubadges` (
`id` int(11) NOT NULL auto_increment,
`username` varchar(30) NOT NULL default '',
`bname` varchar(255) NOT NULL default '',
PRIMARY KEY (`id`)
) TYPE=MyISAM;

add below bname

Code
`bid` varchar(10) NOT NULL default '',
Posted on Tuesday 12th August 2008 at 04:40 PM
UrbanTwitch
UrbanTwitch
Everyone!!! ON case 'create' . get:

PHP Code
1
2
3
4
5
6
}else{
# The Form Has Been Submitted

$updatebadges = mysql_query("INSERT INTO badges (`name`, `url`, `desc`, ) VALUES ('$_POST[bname]','$_POST[burl]','$_POST[bdesc]')");
echo("A new badge has been set up!");
# Inserts New Badge


and replace it with:

PHP Code
1
2
3
4
5
6
}else{
# The Form Has Been Submitted

$updatebadges = mysql_query("INSERT INTO badges (`name`, `url`, `desc` ) VALUES ('$_POST[bname]','$_POST[burl]','$_POST[bdesc]')");
echo("A new badge has been set up!");
# Inserts New Badge
Posted on Tuesday 12th August 2008 at 04:15 PM
Dalez
Dalez
Nope, i have the same tables, and it gives me no errors at all. Also, i have done the show badges.
Posted on Tuesday 12th August 2008 at 02:30 PM
UrbanTwitch
UrbanTwitch
As for showing badges on profile pagee.. I have not added that in. Also the give/makee/take badges should work. I don't know whats wrong with you. Maybe you have different tables?
Posted on Tuesday 12th August 2008 at 08:49 AM
Dalez
Dalez
Well, i've tested this out, and i can't add bagdes, or give badges. I have had to do it manually.

Also, how do i just show the badges on my profile page?
Posted on Tuesday 12th August 2008 at 08:20 AM
Dalez
Dalez
Its pretty cool. I really like it ^^

But is there a way to show the images, instead of the the link?
Posted on Tuesday 12th August 2008 at 07:51 AM
UrbanTwitch
UrbanTwitch
So, what do you all think of this badge thinga-ma-jig? :P