Badge System


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

FIRST. Run these SQL queries

PHP Code
  1. CREATE TABLE `badges` (
  2. `id` int(11) NOT NULL auto_increment,
  3. `name` varchar(255) NOT NULL default '',
  4. `url` varchar(255) NOT NULL default '',
  5. `desc` text NOT NULL default '',
  6. PRIMARY KEY (`id`)
  7. ) TYPE=MyISAM;
  8.  
  9. CREATE TABLE `ubadges` (
  10. `id` int(11) NOT NULL auto_increment,
  11. `username` varchar(30) NOT NULL default '',
  12. `bname` varchar(255) NOT NULL default '',
  13. PRIMARY KEY (`id`)
  14. ) TYPE=MyISAM;


Then let's call this: badge_admin.php

PHP Code
  1. <?php
  2. session_start(); //allow sessions.
  3. include("config.php"); //get config
  4. if($logged[username] && $logged[userlevel] ==6) // allows level 5 to view this page
  5. if (!$_POST[update])
  6.  
  7. # If User Is Logged In
  8.  
  9. switch($_GET[badge])
  10. {
  11. # Lets Us Use One Page For Everything
  12.  
  13. default:
  14. # The Default Page
  15.  
  16. echo("<b><u>Current Badges</u></b><br><br>");
  17. $getbadges = mysql_query("SELECT * FROM badges");
  18. while($badge = mysql_fetch_array($getbadges))
  19. {
  20. echo("Badge Name: $badge[name]<br>
  21. Badge Description: $badge[desc]<br>
  22. Badge: <img src=\"$badge[url]\"><br><br>
  23. <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>");
  24. }
  25. echo("<br><b><u>More Options</u></b><br>");
  26. echo("<a href=\"?badge=create\">Create A Badge</a><br>
  27. <a href=\"?badge=give\">Give User A Badge</a><br>
  28. <a href=\"?badge=deletebadge\">Take Badge away from member</a><br>");
  29. # Options
  30.  
  31. break;
  32. # End Default Page
  33.  
  34. case 'edit':
  35. # Create A Badge Page
  36. if ($_GET[id]) {
  37. if(!$_POST[ed]) {
  38. $getbadgeid = mysql_query("SELECT * FROM badges WHERE id = '$_GET[id]'");
  39. $b = mysql_fetch_array($getbadgeid);
  40. echo("<form method=\"POST\">
  41. Badge Name: <input type=\"text\" value=\"$b[name]\" name=\"bname\"><br>
  42. Image URL: <input type=\"text\" name=\"burl\" value=\"$b[url]\"><br>
  43. Badge Description: <input type=\"text\" name=\"bdesc\" value=\"$b[desc]\"><br><br>
  44. <input type=\"submit\" name=\"ed\" value=\"Update Badge\">
  45. </form>");
  46. # The Create A Badge Form
  47.  
  48. }else{
  49. # The Form Has Been Submitted
  50. $ub2 = mysql_query ("UPDATE `badges` SET `name` = '$_POST[bname]', `url` = '$_POST[burl]', `desc` = '$_POST[bdesc]' WHERE `badges`.`id` =$_GET[id]");
  51. $ub3 = mysql_query ("UPDATE `ubadges` SET `bname` = '$_POST[bname]' WHERE `ubadges`.`bid` = $_GET[id]");
  52. echo("Badge $_POST[bname] updated");
  53. # Inserts New Badge
  54. }
  55. }
  56. break;
  57. # End Create Page
  58.  
  59. case 'create':
  60. # Create A Badge Page
  61.  
  62. if(!$_POST[create]) {
  63. echo("<form method=\"POST\">
  64. Badge Name: <input type=\"text\" name=\"bname\"><br>
  65. Image URL: <input type=\"text\" name=\"burl\" value=\"https://www.\"><br>
  66. Badge Description: <input type=\"text\" name=\"bdesc\"><br><br>
  67. <input type=\"submit\" name=\"create\" value=\"Create Badge\">
  68. </form>");
  69. # The Create A Badge Form
  70.  
  71. }else{
  72. # The Form Has Been Submitted
  73.  
  74. $updatebadges = mysql_query("INSERT INTO badges (`name`, `url`, `desc`, ) VALUES ('$_POST[bname]','$_POST[burl]','$_POST[bdesc]')");
  75. echo("A new badge has been set up!");
  76. # Inserts New Badge
  77.  
  78. }
  79. break;
  80.  
  81. case 'delete':
  82. # Delete Page
  83.  
  84. $getbadgeid = mysql_query("SELECT * FROM badges WHERE id = '$_GET[id]'");
  85. $c = mysql_fetch_array($getbadgeid);
  86. # Get Badge ID
  87.  
  88. $deletebadge = mysql_query("DELETE FROM badges WHERE id = '$_GET[id]'");
  89. echo("The $c[name] badge has been deleted!");
  90. # Delete Badge
  91.  
  92. break;
  93. # End Delete Page
  94.  
  95. case 'give':
  96. # Give Page
  97.  
  98. $getbadges = mysql_query("SELECT * FROM badges");
  99. $g = mysql_fetch_array($getbadges);
  100. # Checks Table
  101.  
  102. if(!$_POST[apply]) {
  103. echo("<form method=\"POST\">
  104. Badge Name: <select name=\"bname\">");
  105. $badges = mysql_query("SELECT * FROM badges");
  106. while($b = mysql_fetch_array($badges))
  107. {
  108. echo("<option value=\"$b[name]\">$b[name]</option>");
  109. }
  110. echo("</select><br>Give To: <select name=\"give\">");
  111. $users = mysql_query("SELECT * FROM members");
  112. while($mem = mysql_fetch_array($users))
  113. {
  114. echo("<option value=\"$mem[username]\">$mem[username]</option>");
  115. }
  116. echo("</select><br><br><input type=\"submit\" name=\"apply\" value=\"Give Badge\">
  117. </form>");
  118. # The Give A User A Badge Form
  119.  
  120. }else
  121. # The Form Has Been Submitted
  122.  
  123. {
  124. $checkbadge = mysql_query("SELECT * FROM ubadges WHERE username = '$_POST[give]'");
  125. $c = mysql_fetch_array($checkbadge);
  126. # Checks Table
  127.  
  128. if($c[bname] == $_POST[bname]) {
  129. echo("$_POST[give] already has the $_POST[bname] badge!");
  130. # Checks To See If User
  131. # Already Has The Badge
  132. }else{
  133. $badgeinfo = mysql_query("SELECT * FROM badges WHERE name = '$_POST[bname]'");
  134. $d = mysql_fetch_array($badgeinfo);
  135. $givebadge = mysql_query("INSERT INTO ubadges (`bname`, `username`,`bid`) VALUES ('$_POST[bname]', '$_POST[give]', '$d[id]')");
  136. echo("$_POST[give] has received the $_POST[bname] badge!");
  137. # Insert Badge
  138.  
  139. }
  140. }
  141. break;
  142. # End Apply Badge Page
  143.  
  144. case 'deletebadge': //delete BADGE HELP
  145. # Give Page
  146.  
  147. $getbadgeid2 = mysql_query("SELECT * FROM badges WHERE id = '$_GET[id]'");
  148. $cb = mysql_fetch_array($getbadgeid2);
  149. $name= $cb[bname];
  150. # Checks Table
  151.  
  152. if(!$_POST[apply]) {
  153. echo("<form method=\"POST\">
  154. Badge Name: <select name=\"bname\">");
  155. $badges = mysql_query("SELECT * FROM badges");
  156. while($q = mysql_fetch_array($badges))
  157. {
  158. echo("<option value=\"$q[name]\">$q[name]</option>");
  159. }
  160. echo("</select><br>Take From:
  161.  
  162. <input type='text' name='useraway'>
  163. <input type=\"submit\" name=\"apply\" value=\"Take Away Badge!\">
  164. </form>");
  165. # the badge form
  166.  
  167.  
  168. }else{
  169. $deletebadge = mysql_query("DELETE FROM ubadges WHERE username = '$_POST[useraway]' AND bname = '$_POST[bname]'");
  170. echo("$_POST[useraway] no longer has the $_POST[bname] badge!");
  171. # Insert Badge
  172.  
  173. }
  174. break;
  175.  
  176.  
  177. }
  178. # End Apply Badge Page
  179. ?>


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

PHP Code
  1. <?php
  2.  
  3. switch($_GET[view]){
  4.  
  5. default:
  6. echo "<style>
  7. table {
  8. background:transparent;
  9. }
  10. #myb {
  11. background: #DCEEF8;
  12. width:490px;
  13. padding: 3px;
  14. border: 1px solid #86C0DF;
  15. color: #459AC8;
  16. }
  17. td#re {
  18. border-bottom: 2px solid #A4C8DA;
  19. padding-bottom: 5px;
  20. border:
  21. }
  22. </style><center>
  23. <table id='myb'><tr style='font-weight:bold;' align='center'><td>Badge Name</td><td>Image</td><td>Description</td>";
  24. $badges = mysql_query("SELECT * FROM badges");
  25. while($q = mysql_fetch_array($badges))
  26. {
  27. echo("<tr align='center'>
  28. <td id='re'>
  29. <a href='?view=b3&id=$q[id]' name='bname'>$q[name]</a></td><td id='re'>
  30. <img src='$q[url]' alt='$q[desc]' border='0'></td><td id='re'>$q[desc]<br></td>
  31.  
  32. </tr>");
  33. }
  34. echo "</table></center>";
  35. break;
  36.  
  37. case 'b3':
  38.  
  39. if ($_GET[id]) { //get username
  40. $badges2 = mysql_query("SELECT * FROM badges WHERE id = '$_GET[id]' ");
  41. $v = mysql_fetch_array($badges2);
  42. $get_admins = mysql_query("SELECT * FROM `ubadges` WHERE `bid` = '$_GET[id]'");
  43. while($admins = mysql_fetch_array($get_admins)){ //make a loop
  44. $getinfo2 = mysql_query("SELECT * FROM members WHERE username = '$admins[username]' ");
  45. $u = mysql_fetch_array($getinfo2);
  46. echo "<a href='members.php?user=$u[id]'>- $admins[username]</a><br>"; //echo the admin with am link to profile.
  47. } //end our loop for admins
  48.  
  49. echo "<br>people who have earned this badge.";
  50.  
  51. }
  52. break;
  53.  
  54. case 'badges':
  55.  
  56. if ($_GET[id]) {
  57. $badges = mysql_query("SELECT * FROM `badges`") or die(mysql_error()); //get all the messages to the loged in user
  58. $meminfo = mysql_query("SELECT * FROM `members` WHERE `id` = '$_GET[id]'");
  59. $a = mysql_fetch_array($meminfo);
  60. echo "<div id=title>$a[username]'s Badges</div><br><br><center>
  61. <style>
  62. table {
  63. background:transparent;
  64. }
  65. #myb {
  66. background: #DCEEF8;
  67. width:490px;
  68. padding: 3px;
  69. border: 1px solid #86C0DF;
  70. color: #459AC8;
  71. }
  72. td#re {
  73. border-bottom: 2px solid #A4C8DA;
  74. padding-bottom: 5px;
  75. border:
  76. }
  77. </style>
  78. <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
  79. $mybadges = mysql_query("SELECT * FROM `ubadges` WHERE `username` = '$a[username]'") or die(mysql_error());
  80. if(mysql_num_rows($mybadges) == 0){ //check if there areany badges
  81. echo "<tr><td colspan='3' align='center' valign='middle'>They have not earned any badges!</td></tr>"; //no new messages
  82. }else{ //or if there are messages
  83. while($r = mysql_fetch_array($mybadges)){ //repeat for all the messages
  84. $badges2 = mysql_query("SELECT * FROM `badges` WHERE `name` = '$r[bname]'") or die(mysql_error());
  85. $b = mysql_fetch_array($badges2);
  86. echo "<tr align='center'>
  87. <td id='re'>
  88. $r[bname]</td><td id='re'>
  89. <img src='$b[url]' alt='$b[desc]' border='0'></td><td id='re'>$b[desc]<br></td>
  90.  
  91. </tr>"; //echo the messages
  92. } //end while
  93. } //end message amount check
  94. echo "</table></center>"; //end table
  95. }
  96. break;
  97.  
  98.  
  99.  
  100. }
  101. ?>


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. <?php
  2. include ("config.php");
  3. if(!$logged[id]){ //Check if user is logged in
  4. echo "<b>Error</b>: You Are Not Logged In!"; //Not logged in
  5. }else{ //Their loggedin
  6. switch($_GET[page]){ //make some links ?page=case
  7. default: //set up the default page upon going to pms.php
  8. $badges = mysql_query("SELECT * FROM `badges` ORDER BY `id` DESC") or die(mysql_error());
  9. $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
  10. echo "<div id=title>My Badges</div> <br><br><center>
  11. <style>
  12. table {
  13. background:transparent;
  14. }
  15. #myb {
  16. background: #DCEEF8;
  17. width:490px;
  18. padding: 3px;
  19. border: 1px solid #86C0DF;
  20. color: #459AC8;
  21. }
  22. td#re {
  23. border-bottom: 2px solid #A4C8DA;
  24. padding-bottom: 5px;
  25. border:
  26. }
  27. </style>
  28. <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
  29. if(mysql_num_rows($mybadges) == 0){ //check if there are any badges
  30. echo "<tr><td colspan='3' align='center' valign='middle'>You have not earned any badges!</td></tr>"; //no new messages
  31. }else{ //or if there are messages
  32. while($r = mysql_fetch_array($mybadges)){ //repeat for all the badges
  33. $badges2 = mysql_query("SELECT * FROM `badges` WHERE `name` = '$r[bname]'") or die(mysql_error());
  34. $b = mysql_fetch_array($badges2);
  35. echo "<tr align='center'>
  36. <td id='re'>
  37. $r[bname]</td><td id='re'>
  38. <img src='$b[url]' alt='$b[desc]' border='0'></td><td id='re'>$b[desc]<br></td>
  39.  
  40. </tr>";
  41. } //end while
  42. } //end badge amount check
  43. echo "</table></center>"; //end table
  44. break; //end the default page
  45.  
  46. } //end switch/get
  47. } //end login check
  48. ?>


So yeah, that's basically it.

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


Enjoy!!
UrbanTwitch's Avatar
Views:
4,440
Rating:
Posted on Thursday 14th August 2008 at 11:37 PM
UrbanTwitch
UrbanTwitch's Avatar
Yeah. Update it with the posts in the comments.
Posted on Thursday 14th August 2008 at 08:55 PM
DanielXP
DanielXP's Avatar
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's Avatar
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's Avatar
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's Avatar
Everyone!!! ON case 'create' . get:

PHP Code
  1. }else{
  2. # The Form Has Been Submitted
  3.  
  4. $updatebadges = mysql_query("INSERT INTO badges (`name`, `url`, `desc`, ) VALUES ('$_POST[bname]','$_POST[burl]','$_POST[bdesc]')");
  5. echo("A new badge has been set up!");
  6. # Inserts New Badge


and replace it with:

PHP Code
  1. }else{
  2. # The Form Has Been Submitted
  3.  
  4. $updatebadges = mysql_query("INSERT INTO badges (`name`, `url`, `desc` ) VALUES ('$_POST[bname]','$_POST[burl]','$_POST[bdesc]')");
  5. echo("A new badge has been set up!");
  6. # Inserts New Badge
Posted on Tuesday 12th August 2008 at 04:15 PM
Dalez
Dalez's Avatar
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's Avatar
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's Avatar
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's Avatar
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's Avatar
So, what do you all think of this badge thinga-ma-jig? :P