duande1985 2014-05-20 05:28
浏览 48
已采纳

如何使用MySQL中的数据填充复选框?

I have been trying to populate a list of checkboxes which values and names are retrieved from MySQL as well. I believe I need to match two iD's together to be able to mark it as checked but I am not sure at the moment.

$boardsiD = $pClass->checkedBoards();

foreach($pClass->retrieveBoards() as $data)
{
    $boardiD    =   $data['boardiD'];
    $boardName  =   $data['boardName'];

    if($boardiD == $boardsiD) ------------------ 1 ---------------------
    {
        $checked = 'checked="1"';
    }

    echo '
        <li><input '.$checked.' type="checkbox" value="'.$boardiD.'">'.$boardName.'</li>
    ';
}

Where the dashes with the number one in the middle is, I am there checking if the board id's are the same and if so then the checked="1" is applied. But it does not seem to be working.

The following is the output for the $boardsiD; These are the checkboxes that have been checekd in the database; saved the ids which is what I am trying to match against the $boardiD = $data['boardiD'] which is the whole list of id's(about 30 id's).

Array (
        [0] => Array
                    ([board_iD] => 106468047379795203)
        [1] => Array
                    ([board_iD] => 286119451265381250)
        [2] => Array
                    ([board_iD] => 136234026166934321)
        [3] => Array
                    ([board_iD] => 468022654964640361)
        [4] => Array
                    ([board_iD] => 409757334785529893)
        [5] => Array
                    ([board_iD] => 409757334785575605)
        [6] => Array
                    ([board_iD] => 490681390589888313)
        ) 

I'm not sure if I can use preg_match for this or if I even should, I tried but it did not work very well, any ideas why the following does not work?

  • 写回答

2条回答 默认 最新

  • douketangyouzh5219 2014-05-20 06:10
    关注

    checked="1" is invalid HTML. The correct Syntax for HTML4 would be

    <input type="checkbox" value="someValue" checked>
    

    or for HTML5

    <input type="checkbox" value="someValue" checked="checked" />
    

    To check if your $boardID is a value of $boardsID you can do the following:

    $checked = '';
    
    for($i = 0; !$checked && $i < sizeof($boardsID); $i++)
      if($boardID == $boardsID[$i]['board_iD'])
        $checked = 'checked="checked"';
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 unity第一人称射击小游戏,有demo,在原脚本的基础上进行修改以达到要求
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?
  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line
  • ¥500 火焰左右视图、视差(基于双目相机)
  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染