PDA

View Full Version : Social Bookmarking!



CarlLawl
09-14-2010, 03:19 PM
Now you may notice that sometimes at the bottom of articles you see little social icon allowing you to share the current page on somewhere like digg or facebook.

I've made a little class that makes the process fairly easy and saves a lot of code.

It also has the options of using icons or text by setting the icons argument to either big, small or none.

USAGE:

require_once('cls/socialbookmarks.php');
$links = new Socialbookmarks();

$links->data('http://www.google.com','Google','small'); // sets url,title,icons

// facebook,digg,delicious,reddit,stumbleupon,gmail,linkedin,myspace,twitter
echo $links->get('twitter'); // fetches the link for said bookmark

ARGUMENTS:

icons can be big, small or none
title is what will be shown between the <a></a> tags and also part of some urls

if you want to style them just wrap the $links->get(); in <p> tags and style the p tags (or span etc)

if there are any features i could add let me know and ill add them in
or if i could have done it a better way, let me know!

heres the class


<?php

class Socialbookmarks {

//--------- USABLE BOOKMARK CALLS ARE facebook,digg,delicious,reddit,stumbleupon,gmail,linkedin,myspace,twitter ---------//

private $url; // your url must begin with http://www.
private $title; // goes between the <a> and </a> (also part of the url for some sites)
private $icons; // none, small or big

// file formats must be as BOOKMARKNAME then your small/big suffix then the extension eg. facebook16.png & facebook32.png
private $imgLoc = 'img/icons/'; // location of image files (must contain ending / )
private $extSmall = '16.png';
private $extBig = '32.png';

// this sets all the needed vars to make the links
public function data($url,$title,$icons) {
$this->url = $url;
$this->title = $title;
$this->icons = $icons;

return $this->url;
}

// this gets the link for what ever bookmark you want
public function get($bookmark) {

switch($bookmark) {

case facebook:
$link = 'http://www.facebook.com/sharer.php?u='.$this->url;
break;

case digg:
$link = 'http://digg.com/submit?url='.$this->url.'&title='.$this->title;
break;

case delicious:
$link = 'http://del.icio.us/post?url='.$this->url.'&title='.$this->title;
break;

case reddit:
$link = 'http://reddit.com/submit?url='.$this->url.'&title='.$this->title;
break;

case stumbleupon:
$link = 'http://www.stumbleupon.com/submit?url='.$this->url.'&title='.$this->title;
break;

case gmail:
$link = 'http://mail.google.com/mail/?view=cm&fs=1&su='.$this->title.'&body='.$this->url;
break;

case linkedin:
$link = 'http://www.linkedin.com/shareArticle?mini=true&url='.$this->url.'&title='.$this->title.'&ro=false';
break;

case myspace:
$link = 'http://www.myspace.com/Modules/PostTo/Pages/?u='.$this->url.'&t='.$this->title;
break;

case twitter:
$link = 'http://twitthis.com/twit?url='.$this->url.'&title='.$this->title;
break;

}

switch($this->icons) {

case none:
$image = $bookmark;
break;

case small:
$image = '<img src="'.$ImgLoc.$bookmark.$extSmall.'" alt="'.$bookmark.'" border="0" />';
break;

case big:
$image = '<img src="'.$imgLoc.$bookmark.$extBig.'" alt="'.$bookmark.'" border="0" />';
break;

}

$r = '<a href="'.$link.'">'.$image.'</a>';
return $r;
}

}

?>