您将需要视频 id 和点击过的用户,以及已经喜欢过类似内容的人的另一个表(称为 Liked_People):
+-------+---------+
|UserId |VideoId |
+-------+---------+
|1 |5739 |
+-------+---------+
现在,用户点击 Like 向那个 PHP 函数发送一个 XMLHTTP 请求:
function Like($usrid , $vidid){
//usrid = userid And vidid = video id
$Comm1 = $Con->prepare("SELECT * FROM Liked_People WHERE UserId = ? And VideoId = ?");
$Comm1->execute(Array($usrid , $vidid);
if($Comm1->rowCount() == 0){
$Comm2 = $Con->prepare("INSERT INTO Liked_People (UserId , VideoId) VALUES (?,?)");
$Comm2->execute(Array($usrid , $vidid));
$Comm3 = $Con->prepare("SELECT Likes FROM VideoTable WHERE Id = ?");
$result = $Comm3->Execute(Array($vidid));
$final = $result + 1;
$Comm4 = $Con->prepare("UPDATE VideoTable SET Likes=? WHERE ID = ?");
$Comm4->Execute(Array($final , $vidid));
}else { return false;}
}
那就是,但是如果您想获得喜欢的用户名列表,可以说您有这样的人员表:
+-------+--------------+
|Id |Username |
+-------+--------------+
|1 |Ahmad Mehmet |
+-------+--------------+
这是代码:
function get_likers($vidid){
if($vidid == null or $vidid == "" or $vidid == 0){
return false;
}
$Comm1 = $Con->prepare(SELECT UserId FROM Liked_People WHERE VideoId = ?);
$Comm1->Execute(Array($vidid));
$arr = $Comm1->fetchAll();
$finale = Array();
$i = 0;
foreach($arr as $row){
$Comm = $Con->Prepare("SElECT Username WHERE Id = ?");
$finale[$i] = $Conm->Execute(Array($row[0]));
$i++;
}
return $finale
}
此功能仅用于备份,不是必需的。
function Work_Array($Arr){
echo '<table border="2">';
echo '<tr><th>Users That Has Liked</th></tr>';
foreach($Arr as $el){
echo"<tr><td>";
echo $el;
echo "</td></tr>";
}
echo "</table>";
}
像这样称呼:
work_array(get_likers($vidid));
问题?:D