Page 1 of 1

Array problem, I think

Posted: Mon Dec 27, 2004 10:29 am
by rkwinternet
Hi,

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";

}
}
I've tried many permutations, but this is the nearest I get to the
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

Posted: Mon Dec 27, 2004 11:41 am
by magicrobotmonkey
The problem is that $test is not an array - its just a regular var. So whats happening is you are looping through and setting it to each persID and then when all rows have been hit, its left set to the last row and then you go and do the rest. Also, your logic on printing the different traits either doesn't seem right or I don't understand something. Here's one way to do it:

Code: Select all

<?php
$resultStuPers = mysql_query("SELECT persID FROM stuPers WHERE stuID = '$stuID'");

while ($row = mysql_fetch_array($resultStuPers))
{

  $test = $row['persID'];


  $resultPersonality = mysql_query("SELECT  trait FROM personality WHERE persID = $test");

  while ( $row = mysql_fetch_array($resultPersonality) ) 
    //this will give you a two dimensional array with each persID listed and each trait for each persID listed
    //also note the use of the trait as the index, in effect setting up a hash table for quick, easy look up later on
    $trait[$test][$row['trait']] = true;

}

//now what you need is an array of all possible traits from somewhere (lets call it $allTraits) and you do it like this
foreach($trait as $persID =>$traits)
{
  foreach($allTraits as $traitID)
  {
    if($traits[$traitID])
    {
      $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";
    }
  }
}
?>