Good day. Please tell me how you can implement the following function:
when creating a vote, a table is displayed - a list of all users. You can select all or someone specific who will see the vote. She has 3 columns - All (checkboxes, select all or someone specific), user ID, user name.
I probably can make a sign, but what about the handling of checkboxes. After selecting and submitting a form, what should happen? Record an array of users and how then to determine the visibility? And is it possible to do this in one table with polls or is it necessary to create another one, specifically for this array?
Maybe there are specific examples of the implementation of such a functional?
1 answer
How to organize a form
There are a million options. I would probably do something like this: checkboxes in your spreadsheet have something like this
<input type=checkbox name="users[{{user.id}}]" value=1>
where {{user.id}}
is the user id, substituted here via php.
With this type of input on the server, after submitting the form, we will receive an array of data
$_GET['users'] = [ 102 => 1, 104 => 1, ];
if only users 102 and 104 have chosen.
We simply iterate over the array and create entries in our mediation table using the voting id (we also get it from the form, for example, from the <input type=hidden name="vote_id" value={{vote.id}}>
) and the next user id . The code, simply, is:
$mysqli = new \mysqli("example.com", "user", "password", "database"); $voteId = $_GET['vote_id']; $stmt = $mysqli->prepare("INSERT INTO votes_users(vote, `user`) VALUES (?, ?)"); $stmt->bind_param("ii", $voteId, $userId); foreach($_GET['users'] as $userId => $checked){ $stmt->execute(); } $stmt->close();
How to organize tables
I would build data storage like this:
Table votes , voting entities:
__________________________ id | name | 1 | Первое голосование | 2 | Второе голосование | 3 | Третье голосование | ____|_____________________|
Table users , user entities:
__________________________ id | name | 101 | Jonny | 102 | Ivan | 103 | Alexey | 104 | Nikolai | ____|_____________________|
Table votes_users , ManyToMany links (many to many), vote links with users:
____________________ vote | user | 1 | 102 | 1 | 104 | 2 | 101 | 2 | 102 | 3 | 102 | 3 | 103 | 3 | 104 | ________|___________|
Thus, it is always easy for us to get users who have access to certain polls, and vice versa - to choose a vote by user. For example, with such a simple request we will select all users who can vote with id = 2:
SELECT u.* FROM users u JOIN votes_users vu ON vu.`user` = u.id JOIN votes v ON vu.vote = v.id WHERE v.id = 2
- Why do you need to go to the base with your hands if there is a good tool in Yii2 for this?
$vote = new Vote(); $vote->userId = $id; $vote->save();
WhereVote
inheritor ofActiveRecord
and contains the attributeuserId
and others. - withoutname - And it is better to give more obvious names to the table fields, and not just
vote
anduser
, because then you can not remember what theuser
and why there are numbers. Instead, it is better to give the namevoteId
anduserId
. - withoutname - I explained the principle using a universal example, without reference to the platform. It should be noted that the author did not mention Yii in the question, and I, I repent, noticed the label only after. And user or user_id or userId is a matter of taste and has no functional difference. I would give the field the name user_id. - Ivan Pshenitsyn
- @IvanPshenitsyn, yes, in the question about yii I did not say, just the label. But the course of your idea is clear to me. Thank you all for the feedback, I will try. - Alexey
- one@IvanPshenitsyn turned out, everything works. Thanks you! - Alexey