Facebook "like" remake
Posted: Fri Apr 30, 2010 10:46 pm
I did not code the first function, I found it online and it turned out to be pretty useful. My code here is really sloppy, I had to use a TON of comments because of all of the "if" statements. Any suggestions are appreciated 
Also, the function "userLink" just takes the arg and returns <a href="blahblahpage=ARG">ARG</a>
So it's just a link to their name, displaying their name.
Also, the function "userLink" just takes the arg and returns <a href="blahblahpage=ARG">ARG</a>
So it's just a link to their name, displaying their name.
Code: Select all
function array_remove_value() {
$args = func_get_args();
$arr = $args[0];
$values = array_slice($args,1);
foreach($arr as $k=>$v) {
if(in_array($v, $values)) {
unset($arr[$k]);
}
}
return $arr;
}
function showLikes($type, $id) {
$a = mysql_query("SELECT * FROM `skittles_$type` WHERE `id`='$id'") or die(mysql_error());
$b = mysql_fetch_array($a);
if ($b['likes'] != NULL) {
$q = mysql_query("SELECT * FROM `skittles_$type` WHERE `id`='$id'") or die(mysql_error());
$d = mysql_fetch_array($q);
$l = explode(" ", $d['likes']);
$people = count($l);
$like = "";
$n = 0;
$pname = (isset($_SESSION['pname']) ? $_SESSION['pname'] : " ");
if (in_array($pname, $l)) {
if ($people == 1) {
$like .= "You";
} else if ($people == 2){
$like .= "You and ";
} else {
$like .= "You, ";
}
}
// Now that it has already checked to see if "You" is involved, you can get rid of it
$l = $this->array_remove_value($l, $pname, "");
$people = count($l); // Now count how many there are left.
if ($people > 1) {
foreach ($l as $name) { // Go through all "likes"
if ($name != $_SESSION['pname'] && $name != "") {
$n++;
$name = $this->userHandler->userLink($name);
if ($n < $people) {
// If there are more than two people (and it's not the last name entered) use commas
if ($people > 2) {
// If there are MORE THAN 2 and it is on the second to last, add a space
if ($n == $people) {
$like .= " ";
// If it's on anything OTHER THAN the second to last, add a comma
} else {
$like .= $name.", ";
}
// If there are only 2 people
} else {
$like .= $name." ";
}
// If $n is equal to the number of people
} else if ($n == $people) {
$like .= "and ".$name;
}
}
}
// If there is only one person that likes it, and it isn't "You"
} else {
if ($like != "You") {
$like .= $this->userHandler->userLink($l[1]);
}
}
$like .= ($people == 1 ? ($like == "You" ? " like this." : " likes this.") : " like this.");
return $like;
}
}