[英]Enter into MySQL table column a user name if selected by a user who has also chosen them using php
I would like to set up a voting system to buddy up a fair few people in a table if they select each other. 我想建立一个投票系统,以团结表中的少数几个人,如果他们互相选择的话。 An example table would be 一个示例表是
---------------------
User | vote | Buddy |
---------------------
A |C |no |
B |C |C |
C |B |B |
D |G |no |
E |no |no |
F |G |no |
G |A |no |
---------------------
The votes are cast by a radio button form, where users have a choice of anyone but themselves. 投票是通过单选按钮表单进行的,用户可以选择自己以外的任何人。 These choices then update the vote column with the username of the person they have voted for. 然后,这些选择将使用他们投票的人的用户名来更新“投票”列。 In this instance user E has not voted, and so has the default 'no'. 在这种情况下,用户E尚未投票,因此默认的“否”。 I have built up to this point. 我已经建立了这一点。
Next, I would like there to be a comparison of the user and vote column. 接下来,我希望对用户和投票列进行比较。 If one user has voted for another, and they in turn have voted for them, I would like their username added to the users buddy column. 如果一个用户投票赞成另一个用户,而他们又投票支持他们,我希望将其用户名添加到“用户好友”列中。 In this instance, B would like to buddy with C and C would like to buddy with B, so there is a match and each others users names have been added to the buddy column. 在这种情况下,B希望与C成为好友,而C希望与B成为好友,因此存在一个匹配项,并且彼此的用户名已添加到好友列。 Although a couple of people voted for G, G voted for A, and so no further additions to the buddy column need to be added in this instance, so remain at the deafult 'no'. 尽管有几个人投票支持G,但G投票赞成A,因此在这种情况下无需在伙伴列中添加其他任何内容,因此请保持默认。
I've guessed that a way to do it may be to cycle through the user column, select which one they have voted for and compare that choice with the choice of the user they voted for. 我猜想,这样做的方法可能是循环浏览“用户”列,选择他们投票支持的用户,然后将该选择与他们投票支持的用户的选择进行比较。 However, putting this into code has proven very difficult, and so far I've only managed this code below. 但是,将其放入代码中已证明非常困难,到目前为止,我仅在下面管理了此代码。 I tried to narrow it down to only users who have been voted for, before cycling through to save time. 为了节省时间,我尝试将其范围缩小到仅获得投票的用户。 I'm not that keen on the 2 selections, I guess that one can go, and I know it won't be ideal to SELECT * for this basic function, but it's there whilst I work on it while the table is in trial mode in case I think of another column that may help. 我不太热衷于这两个选择,我想一个可以去,并且我知道使用SELECT *来实现此基本功能并不理想,但是当表处于试用模式时,我可以在其中进行选择以防万一我认为可能会有所帮助。
$sql = <<<SQL
SELECT *
FROM `table`
SQL;
if(!$result = $db->query($sql)) {
die('There was an error running the query [' . $db->error . ']');
}
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
$vote = $row['vote'];
$sql2 = <<<SQL
SELECT *
FROM `players`
WHERE user = '$vote';
SQL;
if(!$res = $db->query($sql2)) {
die('There was an error running the query [' . $db->error . ']');
}
if ($res->num_rows > 0) {
while($line = $res->fetch_assoc()) {
$user = $line['user'];
echo $name;
}
}
}
}
Any ideas, answers or tutorials that may help will be much appreciated - let me know if you need any more info. 我们将不胜感激任何有帮助的想法,答案或教程-如果您需要更多信息,请告诉我。 Thanks, Toastie 谢谢,Toastie
You would just do this with an update
: 您只需通过update
:
update example e join
example e2
on e.user = e2.vote and e2.user = e.vote
set e.buddy = e.vote;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.