Reply to Re: dynamic checkboxes from mysql table

Your name:

Reply:


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

[Back to original message]


Удаленная работа для программистов  •  Как заработать на Google AdSense  •  England, UK  •  статьи на английском  •  PHP MySQL CMS Apache Oscommerce  •  Online Business Knowledge Base  •  DVD MP3 AVI MP4 players codecs conversion help
Home  •  Search  •  Site Map  •  Set as Homepage  •  Add to Favourites

Copyright © 2005-2006 Powered by Custom PHP Programming

Сайт изготовлен в Студии Валентина Петручека
изготовление и поддержка веб-сайтов, разработка программного обеспечения, поисковая оптимизация