I have three (of many) tables I'm working with and need to get an
interaction. Sorry if this is a bit vague but I'm bit vague myself.
First, there is a table called 'personality' with two
columns 'persID and 'trait', another called 'student' with columns
including 'stuID' and another called 'stuPers' with two
columns 'stuID' and 'persID'.
My aim is to have a form displayed for editing the db. This form
displays information about a student. I want to display the
personality traits via ticked checkboxes the student has ticked and
been entered into the db in the table 'stuPers' with the student
ID 'stuID' and the personality ID 'persID.
Also the form must display checkboxes for those personality traits
which were not originally ticked by the student. The idea is that an
administrator could change the personality traits of any particular
student.
The code I have so far (with error trapping removed) is as follows:
Code: Select all
$resultStuPers = mysql_query("SELECT persID FROM stuPers WHERE stuID
= '$stuID'");
while ($row = mysql_fetch_array($resultStuPers))
{
$test = $row['persID'];
}
$resultPersonality = mysql_query("SELECT persID, trait FROM
personality");
while ( $row = mysql_fetch_array($resultPersonality) ) {
$persID = $row['persID'];
$trait = $row['trait'];
if($test == $persID)
{
$checkboxesPers .= "<span
class="textcontent">$trait</span> <input name="personalities
[$trait]" type="checkbox" value="$persID" checked><br>\n";
}
else
{
$checkboxesPers .= "<span
class="textcontent">$trait</span> <input name="personalities
[$trait]" type="checkbox" value="$persID"><br>\n";
}
}end result I'm trying to acheive.
All the personality trait checkboxes appear in the form with no
ticks in the checkboxes except for the last one the student entered
into the db originally. So, if the students persIDs were 1,2,3,5,7
then persID with the value 7 would have its checkbox ticked but
1,2,3 & 5 would not.
When I put print_r($test); in the $resultstuPers while loop it
prints out 12357 as I would expect, but when I put print_r($test);
outside the loop it only prints out 7.
It seems the problem is getting the array $test from the
$resultStuPers while loop into the $resultPersonality while loop -
it just doesn't want to do it!
Hope someone can enlighten me as to what I'm doing wrong!
TIA.
Richard