|
Posted by Rik on 10/11/45 11:52
David wrote:
>> Offcourse I can't read the OP's mind, but I'd imagine he has the
>> table 'users', 'skills' & 'userskills', where the table 'userskills'
>> contains the userid & skillid of the particular skills that person
>> has.
>> Taking my previous assumption as a starting point, you'd have a form,
>> containing for instance:
>>
>> <label for="userid">User-ID</label><input type="text" name="userid"
>> id="userid" />
>> <?php
>> $skills = mysql_query('SELECT id, name FROM skills');
>> while($skill = mysql_fetch_assoc($result)){
>> echo '<label for="'.$skill['name'].'">'.$skill['name'].'</label>';
>> echo '<input type="checkbox" name="skill[]"
>> id="'.$skill['name'].'" value="'.$skill['id'].'" />';
>> }
> Ok. I can accept this :-); however, the original post does not show
> anything like this. He is passing a constant in his checkbox--the
> number 16.
He is, and I'm not sure wether this is what he would want to do, but it's
the only logical story I can imagine (allthough my imagination coulc be a
little limited) while looking at the clues from the op.
>> And on submitting you have the very nice compact code:
>> foreach($_POST['skill'] as $skill){
>> mysql_query("INSERT INTO userskills (userid, skillid) VALUES
>> ($userid,$skill)");
>> }
>
> We do agree, it seems, that his "problem" was a syntax in his query
> statement, I think.
I'd say so, no way to know for sure untill he confirms it.
>> So, this will only add the specific skills checked to the table
>> userskills, skills the user doesn't have don't need to be added.
>> This way the code is still valid when adding other skills to the
>> database, without having to write the code to check for the specific
>> setting. If the form has to be able to not only assign skills but
>> also to change them, the code will become somewhat bigger.
>
>
> If you wanted to maintain a record of all languages that a person is
> proficient in, isn't a multiple select perhaps a better way to do
> this? Using a checkbox simply does not make sense to me.
Yes and no. From an information standpoint on, it would be the right choice.
However, when possible skillsets grow larger and larger, it's difficult to
keep a good overview of options. Multiple multiple selects grouping the
skills come in to play. And that's not the only thing. From a user-interface
standpoint, one might want the checkboxes for several reasons:
1. You can control the layout, and group options easier in a way that makes
sense to the user.
2. I have tried to explain some people mulitple selects, and fed up ended up
using checkboxes. True, the people in question were somewhat
computer-illiterates, but it seems checkboxes work a lot more intuitively.
Years of ticking boxes on paper forms leave their mark :-).
>> Offcourse, one should check post-values and enforce referential
>> integrity, but that's another story :-).
>
> Sure, ruin a nice conversation talking about work :-)
Hehehe :-).
But hey, what the hell:
$skills = array();
$result = mysql_query('SELECT id FROM skills');
while($row = mysql_fetch_assoc($result)){
$skills[] = (int)$row['id'];
}
foreach($_POST['skill'] as $skill){
if(in_array((int)$skill,$skills, true){
mysql_query("INSERT INTO userskills (userid, skillid) VALUES
($userid,$skill)");
}
}
And offcourse, referential integrity on the database site could in MySQL be
handled by using InnoDB-tables, using foreign key constraints, and on delete
cascades.
Grtz,
--
Rik Wasmus
Navigation:
[Reply to this message]
|