从PHP foreach循环中排除ID

时间:2012-09-09 22:18:44

标签: php select foreach

我试图从foreach循环填充的选择列表中排除多个ID:

$types = new Type();
$types->where('id !=', 13)->order_by('name')->get();
$data['types'] = $types;

<select class="styled" name="form[type_id]">
<option value="0"> - select one - </option>
<?php foreach ($types as $t): ?>
<option value="<?=$t->id?>" <?=check_selected($t->id, $i->type_id)?>><?=$t->name?></option>
<?php endforeach; ?>
<option value="13" <?=check_selected(13, $i->type_id) ?>>Other</option>
</select>

所以目前它将排除ID 13(并手动将其添加到底部),但如何添加更多ID以排除?

提前致谢。

1 个答案:

答案 0 :(得分:0)

试试这个:

...
$exclude = array(13,25);// add IDs to exlude
....
<?php foreach ($types as $t): ?>
   <?php if(!in_array($t->id, $exclude)){ ?>// if the ID is in the exlude array don't add it
      <option value="<?=$t->id?>" <?=check_selected($t->id, $i->type_id)?>><?=$t->name?></option>
   <?php } ?>
<?php endforeach; ?>
...