|
Posted by Mike P2 on 05/01/07 20:37
> For a certain user UID, how can I get php to automatically create html
> checkboxes listing ALL traits from PTable and check each trait based
> on the UserP table on a webpage?
Try a JOIN.
First, query something like:
SELECT
`u`.`uid`,
`t`.`pid`,
`t`.`ptype`
FROM
`PTable` AS `t`
INNER JOIN
`UserP` AS `u`
ON
`t`.`pid` = `u`.`pid`
WHERE
`u`.`uid` = $userId
Then iterate over the results
<?php
//...
while( @list( $uid, $tid, $tval ) = /* ... */ )
{
echo "<p>Trait: $tid<br />";
if( '1' == $tval )
echo "<input type='checkbox' id='traits[$tid]' value='1'
checked='checked'>Good</input><br />";
else
echo "<input type='checkbox' id='traits[$tid]' value='1'>Good</
input><br />";
if( '2' == $tval )
echo "<input type='checkbox' id='traits[$tid]' value='2'
checked='checked'>Bad</input><br />";
else
echo "<input type='checkbox' id='traits[$tid]' value='2'>Bad</
input><br />";
if( '3' == $tval )
echo "<input type='checkbox' id='traits[$tid]' value='3'
checked='checked'>Neutral</input>";
else
echo "<input type='checkbox' id='traits[$tid]' value='3'>Neutral</
input>";
echo '</p>';
}
//...
?>
The output would be xHTML compliant if you were to use underscores in
the middle of the trait id instead of doing it the easy way with
square brackets. You can also JOIN in another table that has the names
of the traits so you don't have to memorize what all of the trait ids
are.
-Mike PII
Navigation:
[Reply to this message]
|