我已經看過關於此問題的其他問題,但沒有人對我有任何答案。我有這行代碼:在HTML輸入標籤中運行PHP函數
foreach ($categories as $category){
echo '<label><input type="checkbox" id="type-'. $category->name . '" value="'. $category->name . '" class="shopping" name="top_level[]" <?php checked('. $category->name . ', $current) ?>>'. $category->name . '</label>';
}
當我運行該文件時,正確的字段顯示,但在文本前有一個'>'。 Firebug將HTML輸出顯示爲:
<input type="checkbox" id="type-Fashion" value="Fashion" class="shopping" name="top_level[]" <?php="" echo="" checked(fashion,="" $current)="" ?="">
爲什麼在整個PHP代碼中都有這些=""
?
checked()
函數是Wordpress並返回HTML屬性checked='checked'
,但我相信問題在於在HTML標記中使用PHP。
該文件與.php一起保存,我在我的WampServer上運行PH P5.5.12。
我試過很多事情,以及別人的建議:
foreach ($categories as $category){
?>
<label><input type="checkbox" id="type-<?php echo $category->name; ?> value="<?php echo $category->name; ?>" class="shopping" name="top_level[]" <?php if(in_array($category->name, $current)){ echo 'checked="checked"';} ?>><?php echo $category->name; ?> </label>
<?php
}
得到這個錯誤:
WARNING: IN_ARRAY() EXPECTS PARAMETER 2 TO BE ARRAY, BOOLEAN GIVEN
$current
是一個數組。
另一個帖子建議使用等號<?= ..... ?>
。這並沒有改變任何事情。
如果我從輸入標籤中刪除PHP代碼,一切正常。
我很感激任何幫助。提前致謝!
你顯然錯了'$ current'是一個數組。在循環之前放入'var_dump($ current)'。 – Barmar