我的页面上有一个评论功能,但我正在尝试添加一些功能。
like-,report-和delete按钮
但似乎我的代码不必要复杂。
function comment_func_bar($how_many,$comment_id,$uid)
{
// connect to the database
//include_once('config.php');
$mysqli = new mysqli(DB_HOST, DB_USER, DB_PASS, DB_NAME);
// get the records from the database
$result=$mysqli->query("SELECT `uid_fk` FROM `comment_likes` WHERE `comment_id_fk` = '".$comment_id."'");
$row = mysqli_fetch_row($result);
if($result->num_rows === 0) //If no likes
{
$how_many='';
} else if(($result->num_rows === 1) && ($row[0] === "$uid")) { //If one like, and that one like is "you"
$how_many = "You like, "; //Should be $_SESSION["user_name"] or what ever
}else if($result->num_rows === 1) //If one like, and its not "you"
{
$other_user_id=$row[0];
$result=$mysqli->query("SELECT `user_name` FROM `users` WHERE `user_id` = '".$other_user_id."'");
$row = mysqli_fetch_row($result);
$how_many=$row[0];
}
//Check if the user already have liked a comment
//http://stackoverflow.com/questions/10164035/how-to-find-specific-row-in-mysql-query-result
while($row = mysqli_fetch_row($result))
{
if($row === $uid)
{
//row found, do processing ...
//reset pointer and break from loop
echo '<p id="txtHint"><small><a href="" class="clickable">' . $how_many .' Unlike</a> - <a href="">Report</a> - <a href="#" class="del_button" id="del-'.$comment_id.'">Delete</a></small></p>';
if(!mysql_data_seek($result, 0))
//Row not found
echo '<p id="txtHint"><small><a href="" class="clickable">' . $how_many .' Like</a> - <a href="">Report</a> - <a href="#" class="del_button" id="del-'.$comment_id.'">Delete</a></small></p>';
break;
}
}
}
但实际问题是在底部。
我正在尝试为用户$ uid搜索行,以确定该用户是否已经喜欢。但我无法让它发挥作用。
它什么都不返回,没有错误,什么都没有!
答案 0 :(得分:0)
自己解决,谢谢你
希望这可以为下一个试图做同样事情的人提供一些想法!
您实际上可以在代码中找到帮助我的线程。 和ofc http://php.net/manual/en/control-structures.if.php
function comment_func_bar($how_many,$comment_id,$uid)
{
// connect to the database
//include_once('config.php');
$mysqli = new mysqli(DB_HOST, DB_USER, DB_PASS, DB_NAME);
// get the records from the database
$result=$mysqli->query("SELECT `uid_fk` FROM `comment_likes` WHERE `comment_id_fk` = '".$comment_id."'");
$row = mysqli_fetch_row($result);
if($result->num_rows === 0) //If no likes
{
$how_many='';
} else if(($result->num_rows === 1) && ($row[0] === "$uid")) { //If one like, and that one like is "you"
$how_many = "You like, "; //Should be $_SESSION["user_name"] or what ever
}else if($result->num_rows === 1) //If one like, and its not "you"
{
$other_user_id=$row[0];
$result=$mysqli->query("SELECT `user_name` FROM `users` WHERE `user_id` = '".$other_user_id."'");
$row = mysqli_fetch_row($result);
$how_many=$row[0];
}
//Check if the user already have liked a comment
//http://stackoverflow.com/questions/4037145/mysql-how-to-select-rows-where-value-is-in-array
////http://stackoverflow.com/questions/10164035/how-to-find-specific-row-in-mysql-query-result
// $row = mysqli_fetch_row($result);
$result=$mysqli->query("SELECT `uid_fk` FROM `comment_likes` WHERE `comment_id_fk` = '".$comment_id."'");
if(!cycelUser($result,$uid))
{
echo '<p id="txtHint"><small><a href="" class="clickable">' . $how_many .' Like</a> - <a href="">Report</a> - <a href="#" class="del_button" id="del-'.$comment_id.'">Delete</a></small></p>';
}else {
echo '<p id="txtHint"><small><a href="" class="clickable">' . $how_many .' Unlike</a> - <a href="">Report</a> - <a href="#" class="del_button" id="del-'.$comment_id.'">Delete</a></small></p>';
}
}
// Cycel throu the uid_fk from comment_likes to find if the user have liked it before!
// Not a very nice solution, but it works (apperently)
function cycelUser ($result,$uid)
{
while ( $row = mysqli_fetch_assoc( $result ) ) { //_array or _assoc whatever
if ( $row['uid_fk'] == $uid ) {
mysqli_data_seek( $result, 0 );
return $row;
}
}
}