我有一个朋友系统,并显示添加为朋友按钮,或取消,或取消联系我试图这样做:
当用户发送好友请求时,会转到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不是会话用户show add me按钮,否则如果他们'然后,朋友们会显示一个无法获取的按钮,或者如果它是发出请求的用户显示取消,或者如果是其他用户显示接受或拒绝按钮,则我的所有代码都在那里,这只是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')); ?>
“&GT;
<!-- 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" }
答案 0 :(得分:0)
尝试将逻辑OR更改为AND。 首先,我可以看到你的目的是什么,但你应该把你的条件分组到括号中但是如果下一个条件成立,基本上OR会说要忽略所有条件。
很难说这种情况的结果是什么:
$likes->friends_friend != $likes->user_id && $likes->friends_user != Session::get('id'))
但如果它是真的那么代码将会执行。
我的猜测是,如果所有条件都成立,你只想要执行代码吗?
如果是这样,请尝试所有AND(&amp;&amp;)。如果没有,只要把我击倒,我将删除这篇文章。