【发布时间】:2015-08-27 15:36:32
【问题描述】:
我有一个朋友系统,并且要显示添加为朋友按钮、取消或取消朋友我正在尝试这样做:
当用户发送好友请求时,它会进入friend_requests,当他们接受它时,它会从friend_requests中删除该行并为好友加1
我想要做的是,如果 user_id 不同的是用户 id(会话用户)并且没有好友请求或好友行然后显示一个添加我按钮,这是我的查询,但里面的代码这在不应该的时候继续运行,因为用户已经发送了请求。
带有示例数据的朋友表
CREATE TABLE IF NOT EXISTS `friends` (
`friends_id` int(11) NOT NULL,
`friends_user` int(11) NOT NULL,
`friends_friend` int(11) NOT NULL,
`friends_date_added` datetime NOT NULL
) ENGINE=InnoDB AUTO_INCREMENT=17 DEFAULT CHARSET=latin1;
--
-- Dumping data for table `friends`
--
INSERT INTO `friends` (`friends_id`, `friends_user`, `friends_friend`, `friends_date_added`) VALUES
(15, 12, 3, '2015-08-27 05:07:36'),
(16, 3, 12, '2015-08-27 05:07:36');
带有样本数据的好友请求:
CREATE TABLE IF NOT EXISTS `friend_request` (
`friend_request_id` int(11) NOT NULL,
`friend_request_from` int(11) NOT NULL,
`friend_request_to` int(11) NOT NULL,
`friend_request_date` datetime NOT NULL,
`friend_request_enabled` int(11) NOT NULL DEFAULT '1',
`friend_request_accepted` int(11) NOT NULL DEFAULT '0',
`friend_request_denied` int(11) NOT NULL DEFAULT '0'
) ENGINE=InnoDB AUTO_INCREMENT=40 DEFAULT CHARSET=latin1;
--
-- Dumping data for table `friend_request`
--
INSERT INTO `friend_request` (`friend_request_id`, `friend_request_from`, `friend_request_to`, `friend_request_date`, `friend_request_enabled`, `friend_request_accepted`, `friend_request_denied`) VALUES
(38, 12, 11, '2015-08-27 05:07:52', 1, 0, 0),
(39, 3, 11, '2015-08-27 15:49:28', 1, 0, 0);
这是我的 PHP 和 html 以及 if 语句,显示是否没有好友请求,如果他们不是好友,如果 user->user_id 不是会话用户显示添加我按钮,否则如果他们是朋友然后显示一个取消按钮,或者如果是发送请求的用户显示取消但或者如果是其他用户显示接受或拒绝按钮,我所有的代码都在那里,只是 if 语句失败:
<!-- if the like doesn't belong to the user who is logged in and they're not friends and no friend request exists, show an add as friend button !-->
<?php if($likes->user_id != Session::get('id') && $likes->friend_request_from != $likes->user_id && $likes->friend_request_to == Session::get('id') ||
$likes->user_id != Session::get('id') && $likes->friend_request_from != Session::get('id') && $likes->friend_request_to == $likes->user_id
|| $likes->friends_friend != $likes->user_id && $likes->friends_user != Session::get('id')): ?>
<a class="add_friend btn btn-success" id="<?php echo System::escape($likes->timeline_likes_id); ?>" href="<?php echo Config::get('URL'); ?>friends/addfriend/<?php echo System::escape($likes->user_id).'/'.System::escape(Session::get('token')); ?>
">
<!-- if the like doesn't belong to the user who is logged in and they are friends, show an unfriend button !-->
<?php elseif($likes->user_id != Session::get('id') && isset($likes->friends_friend)): ?>
<a class="btn btn-danger unfriend_user" onclick="confirm('<?php echo System::translate("are you sure you want to unfriend this person?"); ?>')" href="<?php echo Config::get('URL'); ?>friends/unfriend/<?php echo System::escape($likes->user_id).'/'.System::escape(Session::get('token')); ?>"><?php echo System::translate("Unfriend User"); ?></a>
<!-- end if the like doesn't belong to the user who is logged in and no friend request exists but they are friends, show an unfriend button !-->
<!-- if the like doesn't belong to the user who is logged in and a friend request exists and they are the person who received the requests, show a reject or accept friend request buttons !-->
<?php elseif($likes->user_id != Session::get('id') && isset($likes->friend_request_id) && $likes->friend_request_to == Session::get('id') && $likes->friend_request_from == $likes->user_id): ?>
<a class="btn btn-success" title="<?php echo System::translate("Accept friend request"); ?>" onclick="confirm('<?php echo system::translate("Are you sure you want to accept this friend request?"); ?>')" href="<?php echo Config::get('URL'); ?>friends/accept_request/12/accept/<?php echo System::escape(Session::get('token')); ?>"><?php echo System::translate("Accept"); ?></a> |
<a class="btn btn-danger" title="<?php echo System::translate("Reject friend request"); ?>" onclick="confirm('<?php echo system::translate("Are you sure you want to reject this friend request?"); ?>')" href="<?php echo Config::get('URL'); ?>friends/accept_request/12/reject/<?php echo System::escape(Session::get('token')); ?>"><?php echo System::translate("Reject"); ?></a>
<!-- end if the like doesn't belong to the user who is logged in and a friend request exists and they are the person who received the requests, show a reject or accept friend request buttons !-->
<!-- if the like doesn't belong to the user who is logged in and a friend request exists and they are the person who sent the requests, show a cancel friend request button !-->
<?php elseif($likes->user_id != Session::get('id') && isset($likes->friend_request_id) && $likes->friend_request_from == Session::get('id') && $likes->friend_request_to == $likes->user_id): ?>
<a id="<?php echo System::escape($likes->friend_request_id); ?>" class="btn btn-info cancel_friend_request" title="<?php echo System::translate("Cancel friend request"); ?>" onclick="confirm('<?php echo system::translate("Are you sure you want to cancel this friend request?"); ?>')" href="<?php echo Config::get('URL'); ?>friends/cancel_request/<?php echo System::escape($likes->friend_request_id).'/'.System::escape(Session::get('token')); ?>"><?php echo System::translate("Cancel Request"); ?></a>
<?php endif; ?>
<!-- end if the like doesn't belong to the user who is logged in and a friend request exists and they are the person who sent the requests, show a cancel friend request button !-->
这是我的主查询中的 var 转储:
object(stdClass)#8 (57) { ["timeline_likes_id"]=> string(2) "37" ["timeline_likes_user"]=> string(2) "11" ["timeline_likes_main_status"]=> string(2) "75" ["timeline_likes_date"]=> string(19) "2015-08-26 18:15:03" ["user_id"]=> string(2) "11" ["user_username"]=> string(13) "blahahaha" ["user_email"]=> string(13) "lol@gmail.com" ["user_password"]=> string(60) "rerhjifgdkowergifedmfrgfm" ["user_enabled"]=> string(1) "1" ["user_firstname"]=> string(3) "LOL" ["user_surname"]=> string(7) "corkish" ["user_gender"]=> string(0) "" ["user_birthdate"]=> string(19) "0000-00-00 00:00:00" ["user_country"]=> string(14) "United Kingdom" ["user_telephone"]=> string(5) "12345" ["user_mobile"]=> string(9) "123456789" ["user_contactemail"]=> string(18) "ncorkish@gmail.com" ["user_isadmin"]=> string(1) "0" ["user_staff"]=> string(1) "0" ["user_premium"]=> string(1) "1" ["user_registerdate"]=> string(19) "2015-07-21 19:33:20" ["user_introduction"]=> string(0) "" ["user_occupation"]=> string(18) "Working at present" ["user_available"]=> string(1) "0" ["user_relocate"]=> string(1) "0" ["user_twofactor"]=> string(1) "0" ["user_twofackey"]=> string(1) "0" ["user_privacy_telephone"]=> string(6) "public" ["user_privacy_mobile"]=> string(6) "public" ["user_privacy_email"]=> string(6) "public" ["user_privacy_friends"]=> string(6) "public" ["user_privacy_country"]=> string(6) "public" ["user_privacy_dob"]=> string(6) "public" ["user_privacy_available"]=> string(6) "public" ["user_forgot_email_code"]=> string(1) "0" ["user_emailverified"]=> string(10) "unverified" ["user_banned"]=> string(8) "unbanned" ["user_has_avatar"]=> string(1) "0" ["user_has_banner"]=> string(1) "0" ["timeline_status_id"]=> string(2) "75" ["timeline_status_user"]=> string(1) "3" ["timeline_status_privacy"]=> string(6) "public" ["timeline_status_type"]=> string(0) "" ["timeline_status_post"]=> string(5) "hello" ["timeline_status_date"]=> string(19) "2015-08-26 18:10:51" ["timeline_status_enabled"]=> string(7) "enabled" ["friends_id"]=> NULL ["friends_user"]=> NULL ["friends_friend"]=> NULL ["friends_date_added"]=> NULL ["friend_request_id"]=> string(2) "38" ["friend_request_from"]=> string(2) "12" ["friend_request_to"]=> string(2) "11" ["friend_request_date"]=> string(19) "2015-08-27 05:07:52" ["friend_request_enabled"]=> string(1) "1" ["friend_request_accepted"]=> string(1) "0" ["friend_request_denied"]=> string(1) "0" } Add as friend
LOL 2 Corkish object(stdClass)#9 (57) { ["timeline_likes_id"]=> string(2) "39" ["timeline_likes_user"]=> string(2) "12" ["timeline_likes_main_status"]=> string(2) "75" ["timeline_likes_date"]=> string(19) "2015-08-26 20:37:00" ["user_id"]=> string(2) "12" ["user_username"]=> string(13) "sirfaceless91" ["user_email"]=> string(14) "lol2@gmail.com" ["user_password"]=> string(60) "$" ["user_enabled"]=> string(1) "1" ["user_firstname"]=> string(5) "LOL 2" ["user_surname"]=> string(7) "corkish" ["user_gender"]=> string(0) "" ["user_birthdate"]=> string(19) "0000-00-00 00:00:00" ["user_country"]=> string(14) "United Kingdom" ["user_telephone"]=> string(5) "12345" ["user_mobile"]=> string(9) "123456789" ["user_contactemail"]=> string(18) "ncorkish@gmail.com" ["user_isadmin"]=> string(1) "0" ["user_staff"]=> string(1) "0" ["user_premium"]=> string(1) "1" ["user_registerdate"]=> string(19) "2015-07-21 19:33:20" ["user_introduction"]=> string(0) "" ["user_occupation"]=> string(18) "Working at present" ["user_available"]=> string(1) "0" ["user_relocate"]=> string(1) "0" ["user_twofactor"]=> string(1) "0" ["user_twofackey"]=> string(1) "0" ["user_privacy_telephone"]=> string(6) "public" ["user_privacy_mobile"]=> string(6) "public" ["user_privacy_email"]=> string(7) "private" ["user_privacy_friends"]=> string(7) "private" ["user_privacy_country"]=> string(6) "public" ["user_privacy_dob"]=> string(6) "public" ["user_privacy_available"]=> string(6) "public" ["user_forgot_email_code"]=> string(1) "0" ["user_emailverified"]=> string(10) "unverified" ["user_banned"]=> string(8) "unbanned" ["user_has_avatar"]=> string(1) "0" ["user_has_banner"]=> string(1) "0" ["timeline_status_id"]=> string(2) "75" ["timeline_status_user"]=> string(1) "3" ["timeline_status_privacy"]=> string(6) "public" ["timeline_status_type"]=> string(0) "" ["timeline_status_post"]=> string(5) "hello" ["timeline_status_date"]=> string(19) "2015-08-26 18:10:51" ["timeline_status_enabled"]=> string(7) "enabled" ["friends_id"]=> string(2) "16" ["friends_user"]=> string(1) "3" ["friends_friend"]=> string(2) "12" ["friends_date_added"]=> string(19) "2015-08-27 05:07:36" ["friend_request_id"]=> string(2) "38" ["friend_request_from"]=> string(2) "12" ["friend_request_to"]=> string(2) "11" ["friend_request_date"]=> string(19) "2015-08-27 05:07:52" ["friend_request_enabled"]=> string(1) "1" ["friend_request_accepted"]=> string(1) "0" ["friend_request_denied"]=> string(1) "0" } Unfriend User
profile-image Nath Corkish object(stdClass)#10 (57) { ["timeline_likes_id"]=> string(2) "40" ["timeline_likes_user"]=> string(1) "3" ["timeline_likes_main_status"]=> string(2) "75" ["timeline_likes_date"]=> string(19) "2015-08-26 22:22:07" ["user_id"]=> string(1) "3" ["user_username"]=> string(13) "sirfaceless91" ["user_email"]=> string(23) "natecorkish91@gmail.com" ["user_password"]=> string(60) "$2y$10$a.1vZUr5HU6vQhoUQG9YDupZaMFTgdvxSweEqiUCz3b9ivx2uOTcu" ["user_enabled"]=> string(1) "1" ["user_firstname"]=> string(4) "nath" ["user_surname"]=> string(7) "corkish" ["user_gender"]=> string(0) "" ["user_birthdate"]=> string(19) "0000-00-00 00:00:00" ["user_country"]=> string(7) "Bahamas" ["user_telephone"]=> string(4) "2222" ["user_mobile"]=> string(9) "123456789" ["user_contactemail"]=> string(18) "ncorkish@gmail.com" ["user_isadmin"]=> string(1) "1" ["user_staff"]=> string(1) "1" ["user_premium"]=> string(1) "1" ["user_registerdate"]=> string(19) "2015-07-21 19:33:20" ["user_introduction"]=> string(61) "We specialse in finding ways for our users to socialise more." ["user_occupation"]=> string(16) "Looking for work" ["user_available"]=> string(1) "0" ["user_relocate"]=> string(1) "0" ["user_twofactor"]=> string(1) "0" ["user_twofackey"]=> string(16) "5GILYNBWBXVAUV3A" ["user_privacy_telephone"]=> string(6) "public" ["user_privacy_mobile"]=> string(6) "public" ["user_privacy_email"]=> string(6) "public" ["user_privacy_friends"]=> string(6) "public" ["user_privacy_country"]=> string(6) "public" ["user_privacy_dob"]=> string(6) "public" ["user_privacy_available"]=> string(6) "public" ["user_forgot_email_code"]=> string(32) "d71a30cb75faed7c48cba971cf934922" ["user_emailverified"]=> string(10) "unverified" ["user_banned"]=> string(8) "unbanned" ["user_has_avatar"]=> string(1) "1" ["user_has_banner"]=> string(1) "1" ["timeline_status_id"]=> string(2) "75" ["timeline_status_user"]=> string(1) "3" ["timeline_status_privacy"]=> string(6) "public" ["timeline_status_type"]=> string(0) "" ["timeline_status_post"]=> string(5) "hello" ["timeline_status_date"]=> string(19) "2015-08-26 18:10:51" ["timeline_status_enabled"]=> string(7) "enabled" ["friends_id"]=> string(2) "15" ["friends_user"]=> string(2) "12" ["friends_friend"]=> string(1) "3" ["friends_date_added"]=> string(19) "2015-08-27 05:07:36" ["friend_request_id"]=> string(2) "39" ["friend_request_from"]=> string(1) "3" ["friend_request_to"]=> string(2) "11" ["friend_request_date"]=> string(19) "2015-08-27 15:49:28" ["friend_request_enabled"]=> string(1) "1" ["friend_request_accepted"]=> string(1) "0" ["friend_request_denied"]=> string(1) "0" }
【问题讨论】:
-
请尝试通过回显所有这些变量和调用的结果来调试它。在不知道这些结果是什么的情况下,没有人能够说出出了什么问题。 (顺便说一句:您的代码是否缺少
</a>结束标记,就在您帖子中的格式有点中断的地方?)