- Joined
- May 2, 2010
- Messages
- 120
- Reaction score
- 10
I'm not releasing a function to add a person's "like" to your "likes" column in web_events/web_news table, if you can't do that you shouldn't be using this.
PHP:
/**
* This function will remove all the specified values from an array and return the final array.
* Arguments : The first argument is the array that should be edited
* The arguments after the first argument is a list of values that must be removed.
* Example : array_remove_value($arr,"one","two","three");
* Return : The function will return an array after deleting the said values
* This is not my function, i got it from some random site.
*/
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;
}
/**
* Credits : Trahb
* To use: Create a function to add " $userName" to a column of "likes" in your web_events or web_news table
* Where $userName is the name of the user that "likes" the post. There MUST be a space before the name
* For this function to work properly.
* @param $type - "news" or "events"
* @param $id is the id of $type
*/
function showLikes($type, $id) {
$a = mysql_query("SELECT * FROM `web_$type` WHERE `id`='$id'") or die(mysql_error());
$b = mysql_fetch_array($a);
if ($b['likes'] != NULL) {
$q = mysql_query("SELECT * FROM `web_$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;
}
}