Hello everyone, Rum here. I've been developing a way for people to "like" forum topics and I'm looking for some testers, all you need to do is click the button below and 'like' this topic. I will also post the source code and how you can add a like button to your own topic.
Click here for Sparks' self updating images and links in posts!!
NEW TOPIC HERE: http://scratch.mit.edu/forums/viewtopic.php?id=70214 DO NOT POST ON THIS ONE
How the liking system works
The image
This checks a mysql database and shows how many people have clicked the like button
The link
This does something a bit more complicated, first of all it checks to see whether people have liked the topic at all before, if not then it adds a whole new entry into the database this entry has your ip address and the topic id. If people have liked the topic before, it checks whether your ip is on the list of ips for that topic, if not then it adds your ip to the list of ips for that topic. This means that you can only like things once.
Please like this topic to show your support:
and the source code:
<?php
function writetextimage($string, $substring) {
// Credit to LS97 for parts of this function
if ($_REQUEST['style'] == 1 || isset($_REQUEST['style']) == false || $_REQUEST['style'] != 2) {
if ($_REQUEST['type'] == 'post') {
$img = imagecreatefrompng ('bgpost.png');
}
else {
$img = imagecreatefrompng ('bg.png');
}
$text_colour = imagecolorallocate($img, 1, 1, 1);
$font = realpath('font.ttf');
imagettftext($img, 7, 0, 10, imagefontheight(7), $text_colour, $font, $string);
imagettftext($img, 7, 0, 10, 28, $text_colour, $font, $substring);
$whit = imagecolorallocate($img, 0, 0, 0);
imagecolortransparent($img, $whit);
}
if ($_REQUEST['style'] == 2) {
$img = imagecreatefrompng ('bg2.png');
if ($_REQUEST['type'] == 'post') {
$text_colour = imagecolorallocate($img, 0, 102, 185);
}
else {
$text_colour = imagecolorallocate($img, 255, 0, 0);
}
$text_colour2 = imagecolorallocate($img, 1, 1, 1);
$font = realpath('font2.ttf');
imagettftext($img, 10, 0, 4, 14, $text_colour, $font, $string);
imagettftext($img, 8, 0, 15, 28, $text_colour2, $font, $substring);
$whit = imagecolorallocate($img, 255, 255, 255);
imagecolortransparent($img, $whit);
}
header('Content-type: image/png');
imagepng($img);
imagecolordeallocate($text_color);
if(isset($text_colour2)) {
imagecolordeallocate($text_color2);
}
imagedestroy($img);
}
include 'mysql.php';
$topicid = $_REQUEST['id'];
$userip = $_SERVER['REMOTE_ADDR'];
if ($_REQUEST['forum'] == 'tbg') {
$url = 'http://scratch.mit.edu/tbgforums/viewtopic.php?id=' . $topicid;
$database = 'tbg_topicrate';
if ($_REQUEST['type'] == 'post') {
$url = 'http://scratch.mit.edu/tbgforums/viewtopic.php?pid=' . $topicid . '#p' . $topicid;
$database = 'tbg_postrate';
}
}
else {
$url = 'http://scratch.mit.edu/forums/viewtopic.php?id=' . $topicid;
$database = 'topicrate';
if ($_REQUEST['type'] == 'post') {
$url = 'http://scratch.mit.edu/forums/viewtopic.php?pid=' . $topicid . '#p' . $topicid;
$database = 'postrate';
}
}
if ($_REQUEST['ret'] == 'image') {
if (is_numeric($topicid) === false) {
writetextimage('Error: id is not valid', '');
exit();
}
$urlcontents = trim(file_get_contents($url));
if (strpos($urlcontents, $bdrq) != false) {
writetextimage('Error:', 'id not found');
exit();
}
mysql_connect($mysql_host, $mysql_user, $mysql_password) or die(mysql_error());
mysql_select_db($mysql_database) or die(mysql_error());
$checkquery = mysql_query("SELECT * FROM $database WHERE postid='$topicid'");
if (mysql_num_rows($checkquery) == 0) {
$likes = 0;
}
else {
$topicratings = mysql_fetch_row($checkquery) or die(mysql_error());
$ratedips = explode(',', $topicratings[1]);
$likes = count($ratedips);
if ($ratedips[0] == '') {
$likes = 0;
}
}
if ($likes == 1) {
$likes = 1 . ' Like';
if($_REQUEST['verb'] == 'love') {
$likes = 1 . ' Love-it';
}
}
else {
if($_REQUEST['verb'] == 'love') {
$likes = $likes . ' Love-its';
}
else {
$likes = $likes . ' Likes';
}
}
$checkquery = mysql_query("SELECT * FROM $database WHERE postid='$topicid'");
if (mysql_num_rows($checkquery) == 0) {
$already_liked = 'Like This';
if ($_REQUEST['verb'] == 'love') {
$already_liked = 'Love it?';
}
}
else {
$topicratings = mysql_fetch_row($checkquery) or die(mysql_error());
$ratedips = explode(',', $topicratings[1]);
$count = count($ratedips);
if ($_REQUEST['verb'] == 'love') {
$already_liked = 'Love it?';
}
else {
$already_liked = 'Like This';
}
for($i=0; $i<$count; $i++) {
if($ratedips[$i] == $userip) {
$already_liked = 'Unlike This';
if ($_REQUEST['verb'] == 'love') {
$already_liked = 'I love it!';
}
}
}
}
writetextimage($likes, $already_liked);
}
elseif ($_REQUEST['ret'] == 'add') {
if (is_numeric($topicid) === false) {
writetextimage('Error: id is not valid', '');
exit();
}
$urlcontents = trim(file_get_contents($url));
if (strpos($urlcontents, $bdrq) != false) {
writetextimage('Error: id not found', '');
exit();
}
$headerurl = 'Location:' . $url;
mysql_connect($mysql_host, $mysql_user, $mysql_password) or die(mysql_error());
mysql_select_db($mysql_database) or die(mysql_error());
$checkquery = mysql_query("SELECT * FROM $database WHERE postid='$topicid'");
if (mysql_num_rows($checkquery) == 0) {
$addtopic_query = mysql_query("INSERT INTO $database (postid, ips) VALUES ('$topicid', '$userip')");
header($headerurl);
exit();
}
else {
$topicratings = mysql_fetch_row($checkquery) or die(mysql_error());
$ratedips = explode(',', $topicratings[1]);
$count = count($ratedips);
for($i=0; $i<$count; $i++) {
if($ratedips[$i] == $userip) {
unset($ratedips[$i]);
$ratedips = implode(',', $ratedips);
$updateips_query = mysql_query("UPDATE $database SET ips='$ratedips' WHERE postid='$topicid'") or die(mysql_error());
header($headerurl);
exit();
}
}
$ratedips[$count] = $userip;
$ratedips2 = implode(',', $ratedips);
if ($ratedips[0] == '') {
$ratedips2 = $userip;
}
$updateips_query = mysql_query("UPDATE $database SET ips='$ratedips2' WHERE postid='$topicid'") or die(mysql_error());
header($headerurl);
exit();
}
}
else {
writetextimage('Error: incorrect URL vars used', 'Better luck next time!');
}
?>This took a long time to write so please support me!
I'd like to thank LS97 for the writetextimage() code and Sparks for his inspirational API thread.
Right, now for how you can add it to your posts.
First off you need to make a url tag with the url http://scratchloveit.netne.net/topicrate.php?id=TOPIC ID HERE&ret=add to find the topic id, you must first make the post, go out to the forums section your topic is in, click on your topic and you'll see in the url bar something like scratch.mit.edu/forums/viewtopic.php?id=# where # is a number, you need to copy this number and put it in the TOPIC ID HERE.
So far we have
[url=http://scratchloveit.netne.net/topicrate.php?id=TOPIC ID HERE&ret=add][/url]
Now for the image part
Basically you use the same id but you use an image tag and the url is slightly different so
[url=http://scratchloveit.netne.net/topicrate.php?id=TOPIC ID HERE&ret=add][img]http://scratchloveit.netne.net/topicrate.php?id=TOPIC ID HERE&ret=image[/img][/url]
notice ret=image now rather than ret=add.
To have a like button that likes a tbg forum topic, just add &forum=tbg to the url for both image and link. To make it use love-its instead of likes (changes the text) then add &verb=love onto the end of the image url only. this is what it looks like:
Make sure to check out Sidharth's Javascript code that automatically generates the code for you based on the url of your post! Here it is and you can like it using the new likeable posts feature (see below on how to implement yourself)!
Which brings us nicely onto the new:
Likeable Posts!
So, you've made a post in a topic that you think everyone will like, and want to show there thanks, but the like button only supports topics. Well not anymore!
You can now have likeable posts, all you have to do is use the same code as topics, except change the id to the id of your post. To find this out, when you make a post, check (once you've posted it) what your url bar says, it will have on the end something like #p123456 you need to copy the number bit of it (in this case 123456) and put that as the id in my code
[url=http://scratchloveit.netne.net/topicrate.php?id=POST ID HERE&ret=add&type=post][img]http://scratchloveit.netne.net/topicrate.php?id=POST ID HERE&ret=image&type=post[/img][/url]
then, as you can see here you put on the end of each of the urls (image and link) &type=post this is to tell my script that you're talking about a post and not a topic. If you leave this blank then it will think it is a topic, and either it won't work, or you will be liking some random topic.
Well, we're done! I hope you like this new feature!
Custom like button styles
So, you like the idea of 'like buttons' but don't like the style they're in?
Introducing:
Custom made like buttons
Basically, you tell me the style of button you want, font, background shape, colours etc. and I make it for you, adding it as an option in the URL of the image eg. stl=1 meaning style = style no.1. I might add the option to change the font colour & size from the URL, it all depends on how many custom requests I get.
Current Styles:
Style 1 - Style 1 is default, no extra code needed
Style 2 - add &style=2 to the end of the image url to enable this style. Requested by IHeartGaming.
Thanks,
Rum
Changelog
V1.0 First Release, allows you to add like buttons to forum topics and like them. V1.1 Changed text size & Added redirect to topic V1.5 Added unlike feature so that if you liked something in the past you can change your mind and take away the like also added small text saying either like or unlike to show you what would happen if you clicked the link. V1.5.1 Bug Fixes, such as now displays image of Error messages and I had a couple of variables set the wrong way round. V1.6 Prettier like button, thanks LS97! V1.6.1 Fixed a bug where people could like topics even if they didn't exist also put the mysql details in a seperate file along with a variable that holds the string 'Bad request' something or other. V1.7 TBG support! add &forum=tbg to url for it to work. V1.7.1 Have it use Love-its instead of Likes put &verb=love on the end of the img url to make it work. V1.8 Added first custom style requested by IHeartGaming. V2.0 Finally added Likeable posts! as requested by LS97. V2.1 Added blue colouring for posts, and white colouring for topics
Todo list
· Top liked list (a bit liked the top loved section but for forum topics) · Star Rating System (Seperate project) · Customisable Button-added requested one, need to make fully user customisable · Add Supporters list support using scratch login api. · Custom Button text (different to Like and Love)
Last edited by RUMCHEERYPOOPOO (2011-07-30 14:40:28)
Offline
just checking, can anyone else not see this: ?
Offline
Can a mod please change the title of this post to 'Forum topic like Buttons!'
Offline
I'm working on a redirect to the last visited page
Last edited by RUMCHEERYPOOPOO (2011-07-26 09:38:39)
Offline
the only problem with this is i was the 7th like and when i tried to like it again it took away my like
Offline
rojasscratches wrote:
the only problem with this is i was the 7th like and when i tried to like it again it took away my like
![]()
It doesn't take away your like, you have to reload the page for the change to take effect.
I'm working on this.
Edit: should redirect now
Last edited by RUMCHEERYPOOPOO (2011-07-26 10:26:23)
Offline
So you can type in any topic id and it'll work? Good job!
Offline
Yep
thx although not tbg forum topics but I might implement
Offline
I'm thinking in the next version a top liked topics list and support for the tbg forums. Any other suggestions people have got?
Edit: also I'll make it so that if you have already liked it, you can unlike it.
Last edited by RUMCHEERYPOOPOO (2011-07-26 18:54:02)
Offline
Right! I have made it so you can unlike it if you've already liked it, but in the process I reset the like counter for this thread
.
Also I made it display some text telling you whether you will like it or unlike it when you click it
Offline
Cool!
You should ask the Scratch team to maybe add a place for the most liked or something.
Offline
Mcsugarface wrote:
Cool!
You should ask the Scratch team to maybe add a place for the most liked or something.
Maybe someday the scratch team will take pity on me
, I quite like it being unofficial it keeps the pressure off somewhat. But if they're offering, I'd happily let them use it.
Offline
RUMCHEERYPOOPOO wrote:
Mcsugarface wrote:
Cool!
You should ask the Scratch team to maybe add a place for the most liked or something.
Maybe someday the scratch team will take pity on me
, I quite like it being unofficial it keeps the pressure off somewhat. But if they're offering, I'd happily let them use it.
You should try to make a way to list the most liked posts, and ask the Scratch team if they could put up a page or something for the list. XD
Offline
whizzer wrote:
Experiment:
http://scratchloveit.netne.net/topicrat … ;ret=image
Maybe you could make it a better image, like a Google +1 or a Facebook Like?
I see you have tried doing it for posts, unfortunately that doesn't work :\ but yes I suppose I could do that, but I'd rather not use the same one as google or facebook. I could make it prettier though
also the # kinda breaks it
Edit: I made it a picture error message
Last edited by RUMCHEERYPOOPOO (2011-07-27 04:52:13)
Offline
ssss wrote:
Could you make a star rating system? xD
I think a star rating system would be quite hard to do as you would have to have a seperate page that came up after you clicked on the stars to vote on. I may work on this, but alongside the like system, switching them over if I can perfect it. Thanks for the suggestion!
Offline
RUMCHEERYPOOPOO wrote:
ssss wrote:
Could you make a star rating system? xD
I think a star rating system would be quite hard to do as you would have to have a seperate page that came up after you clicked on the stars to vote on. I may work on this, but alongside the like system, switching them over if I can perfect it. Thanks for the suggestion!
Welcome.
Offline