dynamic checkboxes from mysql table

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • hermanh28@gmail.com

    dynamic checkboxes from mysql table

    tearing my hair out over this!

    I have 2 tables:

    PTable which lists all traits
    pid,ptype
    1,good
    2,bad
    3,neutral

    UserP Table which records user and traits
    uid,pid
    1,2
    1,3
    9,1
    9,2
    9,3

    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?

    Hence, if UID is 1

    it should show
    good - unchecked
    bad - checked
    neutral - checked

  • Mike P2

    #2
    Re: dynamic checkboxes from mysql table

    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='checke d'>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='checke d'>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='checke d'>Neutral</input>";
    else
    echo "<input type='checkbox' id='traits[$tid]' value='3'>Neutr al</
    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

    Comment

    • Mike P2

      #3
      Re: dynamic checkboxes from mysql table

      On May 1, 4:37 pm, Mike P2 <sumguyovrt...@ gmail.comwrote:
      if( '1' == $tval )
      echo "<input type='checkbox' id='traits[$tid]' value='1'
      checked='checke d'>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='checke d'>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='checke d'>Neutral</input>";
      else
      echo "<input type='checkbox' id='traits[$tid]' value='3'>Neutr al</
      input>";
      Oops...I meant "type='radi o'".

      -Mike PII

      Comment

      Working...