从数据库返回时显示选定的表单项

时间:2011-11-03 23:48:49

标签: php arrays forms default-value

当用户正在编辑从数据库填充的表单中的数据时,我正在努力获取预先选中的复选框,我正在使用2个数组,第一个是可用的主题,

Array ( [0] => Array ( 
            [topic_id] => 402 
            [topic_title] => Website Development 
            [topic_slug] => website-development 
            [topic_description] => This topic will cover everything form the most CSS to the most advanced PHP. You will learn not only how to code but create maintable code bases that can be authored by various authors with no previous knowledge of the project.
            [date_created] => 2011-10-03 17:27:41 
      ) 
        [1] => Array ( 
             [topic_id] => 404 
             [topic_title] => j41-ramp-handling 
             [topic_slug] => j41-ramp-handling 
             [topic_description] => Ramp handling course for J41
             [date_created] => 2011-11-02 23:14:00 
        ) 
        [2] => Array ( 
            [topic_id] => 405 
            [topic_title] => aviation-regulations 
            [topic_slug] => 
            [topic_description] => Changes to aviation regulations.
            [date_created] => 2011-10-17 10:19:40 ) ) 

我还有一个数组,显示用户注册的主题,

   Array ( [0] => Array ( 
                 [topic_title] => Website Development 
                 [topic_id] => 402 
                 [topic_slug] => website-development  
                 [topic_description] => This topic will cover everything form the most CSS to the most advanced PHP. You will learn not only how to code but create maintable code bases that can be authored by various authors with no previous knowledge of the project.
) 
            [1] => Array ( 
                [topic_title] => aviation-regulations  
                [topic_id] => 405 
                [topic_slug] => 
                [topic_description] => Changes to aviation regulations.
           ))

第一个数组被称为$topics,第二个数组被称为$signedup我试图在默认情况下检查复选框,如果两个数组中的ttopic id匹配,但是,无论我尝试什么,我都不会打勾,下面是我的HTML / PHP代码,

<fieldset>
                    <legend>Topics Sign Up</legend>
                    <?php $i = 0; ?>
                    <?php foreach ($topics as $k => $v) : ?>
                        <?php //var_dump($signedup[$i]['topics_topic_id']); ?>
                        <label for="topics_topic_id[]" class="checkbox"><?php echo $v['topic_title'];?></label>
                        <input type="checkbox" name="topics_topic_id[]" value="<?php echo $v['topic_id']; ?>"/>
                        <?php $i++; ?>
                    <?php endforeach; ?>
                </fieldset>

如果已注册的数组中的主题ID与主题aray的主题ID相匹配,我如何默认选中该复选框?

2 个答案:

答案 0 :(得分:1)

您无法使用value=""所需的checked="checked",例如:

<input type="checkbox" name="topics_topic_id[]" value="<?php 
if ( in_array( $v['topic_id'], $signedup[$i] ) == true ) echo ' checked="checked"'; 
?>"/>

答案 1 :(得分:0)

您可以像$signedup那样存储一个主题ID数组

$signedUpIds = array();
foreach ($signedup as $topic) {
    $signedUpIds[] = $topic['topic_id'];
}

然后当您浏览$topics时,请检查该ID是否在$signedUpIds中,如果是,请将checked="checked"添加到复选框属性

<?php foreach ($topics as $k => $v) : ?>
      <?php $checkedText = in_array($v['topic_id'], $signedUpIds) ? ' checked="checked"' : ''; ?>
      <label for="topics_topic_id[]" class="checkbox">
          <?php echo $v['topic_title'];?>
      </label>
      <input type="checkbox" name="topics_topic_id[]" value="<?php echo $v['topic_id']; ?>" <?php echo $checkedText; ?> />
<?php endforeach; ?>