2011-11-03 54 views
0

当用户正在编辑从数据库填充的表单中的数据时,我想要获取预先选中的复选框,我正在处理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在它那场比赛主题的那个?

回答

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; ?> 
+0

我血腥的爱你! – Udders

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"'; 
?>"/> 
相关问题