doubipiao1611 2013-01-24 20:09
浏览 86

如果用户已经喜欢某事,则显示消息

In my web application I have 2 buttons: one is "like", the other one is "dislike".

Users can't like their own profile.

I am looking for now a bit of code so that if a database check shows there is already a row for the user who wants to like or dislike that profile a message comes up: "You have already liked this user" or "You have already disliked this user"

<? if ($_POST['like']){
  if(strtolower($view) == strtolower($fetchAccount['UserName'])){
    echo "You cannot Like yourself!<br /><br/>";
  } else {
     mysql_query("INSERT INTO `ProfileLikes` (`id`, `Profile`, `Rated`,
         `LikedDisliked`, `Date`) VALUES ('', '{$fetchUser['UserName']}',
         '{$fetchAccount['UserName']}', 'Liked', '$time')");
    echo "You Liked {$fetchUser['UserName']}!<br /><br />";
  }
}

if ($_POST['Dislike']){
  if(strtolower($view) == strtolower($fetchAccount['UserName'])){
    echo "You cannot DisLike yourself!<br /><br />";
  } else {
    mysql_query("INSERT INTO `ProfileLikes` (`id`, `Profile`, `Rated`,
      `LikedDisliked`, `Date`) VALUES ('', '{$fetchUser['UserName']}',
      '{$fetchAccount['UserName']}', 'Disliked', '$time')"); 
    echo "You DisLiked {$fetchUser['UserName']}!<br /><br />";
  }
}
?>

Can anyone help please? Thanks in advance. :)

  • 写回答

1条回答 默认 最新

  • duanchi19820419 2013-01-25 14:31
    关注

    Here you go.

    <?php
    
    //-------Like section---------
    if ($_POST['like']){
    
      if(strtolower($view) == strtolower($fetchAccount['UserName'])){
    
        echo "You cannot Like yourself!<br /><br/>";
    
      } else {
    
         //Check whether user 'liked' the other user already.
         $selectQuery = "select * from `ProfileLikes` where `Profile` = '" . 
              $fetchUser['UserName'] ."' AND `Rated` = '" . 
              $fetchAccount['UserName'] . "' AND `LikedDisliked` = 'Liked' ";
         $resultSetCheck = mysql_query($selectQuery);
         $rowLikedExistsArray = mysql_fetch_assoc($resultSetCheck);
    
         if (!empty($rowLikedExistsArray)) {
    
             //He 'liked' it already!
             echo "You have already liked this user! <br /><br/>";
    
         } else {         
    
             //insert new 'like'
             mysql_query("INSERT INTO `ProfileLikes` (`id`, `Profile`, `Rated`,
                 `LikedDisliked`, `Date`) VALUES ('', '{$fetchUser['UserName']}',
                 '{$fetchAccount['UserName']}', 'Liked', '$time')");
            echo "You Liked {$fetchUser['UserName']}!<br /><br />";
    
         }
      }
    }
    
    //-------Dislike section---------
    if ($_POST['Dislike']){
    
      if(strtolower($view) == strtolower($fetchAccount['UserName'])){
    
        echo "You cannot DisLike yourself!<br /><br />";
    
      } else {
    
         //Check whether user 'disliked' the other user already.
         $selectQuery = "select * from `ProfileLikes` where `Profile` = '" . 
              $fetchUser['UserName'] ."' AND `Rated` = '" . 
              $fetchAccount['UserName'] . "' AND `LikedDisliked` = 'Disliked' ";
         $resultSetCheck = mysql_query($selectQuery);
         $rowDislikedExistsArray = mysql_fetch_assoc($resultSetCheck);
    
        if (!empty($rowDislikedExistsArray)) {
    
             //He 'disliked' it already!
             echo "You have already disliked this user! <br /><br/>";
    
        } else {
            //insert new dislike
            mysql_query("INSERT INTO `ProfileLikes` (`id`, `Profile`, `Rated`,
              `LikedDisliked`, `Date`) VALUES ('', '{$fetchUser['UserName']}',
              '{$fetchAccount['UserName']}', 'Disliked', '$time')"); 
            echo "You DisLiked {$fetchUser['UserName']}!<br /><br />";
        }
    
      }
    }
    
    ?>
    

    Note: I can see some optimizations to make. But dont wanna break your code flow.

    评论

报告相同问题?

悬赏问题

  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭
  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么