views:

26

answers:

1

I'm trying to extract and make variables out of the "thinking skills" (like analyzing, evaluating, etc.) from a test and set their value to the number of test items within each. I'm stuck, so any help would be appreciated. (The SQL statement seems to work fine.)

Example of what I want: $analyzing = 7, $applying = 13, etc.... Thanks!

$sql = "SELECT  thinkskill        AS tskill
              , COUNT(thinkskill) AS counttskill
          FROM $c_keytable
      GROUP BY thinkskill
      ORDER BY thinkskill" ;
$result = mysql_query ( $sql ) ;

while ( $row = mysql_fetch_assoc ( $result ) )
{
   // Example: $analyzing = 7  --> 
   ${$row["tskill"]} = $row["counttskill"] ;
}
+1  A: 

Try this:

$summary = array ();
while ( $row = mysql_fetch_assoc ( $result ) )
{ 
   $summary[$row["tskill"]] = $row["counttskill"] ;
}
// now you can get the count for 'analyzing' with $summary['analyzing']

I don't recommend it, but if you really want the info out of the array and into local variables, you can do

extract ($summary)
grossvogel
Yes, thank you, that works!! May I ask why putting into local vars is not recommended? Aren't the variables more easily manipulated if in the variable format? Thanks again for your help.
dave
It's not local variables that I'm recommending against, it's letting their names come from dynamic data (the call to `extract`). Example:Say you keep track of the logged-in user with the variable `$userid`. For some reason, somebody adds a 'tskill' value called 'userid'. Now, when you extract the data from your query, `$userid` will get overwritten, and you'll have a hell of a time figuring out why.
grossvogel
OK, I understand! Thanks very much for the explanation.
dave