gpt4 book ai didi

php - 我的 comment_func_bar 功能不起作用

转载 作者:行者123 更新时间:2023-11-29 22:50:30 24 4
gpt4 key购买 nike

我的页面上有评论功能,但我正在尝试向其中添加一些功能。

like-, report- and delete button

但是我的代码似乎没有必要复杂。

    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;
}
}
}

但实际的问题是在底部的 while 。

我正在尝试搜索用户 $uid 的行,以了解该用户是否已经喜欢。但我无法让它工作。

它什么也没返回,没有错误,什么也没有!

最佳答案

您实际上可以在代码中找到帮助我解决此问题的线程。和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;
}
}
}

关于php - 我的 comment_func_bar 功能不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28929390/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com