Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/260.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/77.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
HTML中可读的嵌套PHP循环/Ifs_Php_Html - Fatal编程技术网

HTML中可读的嵌套PHP循环/Ifs

HTML中可读的嵌套PHP循环/Ifs,php,html,Php,Html,我正在尝试分离我的PHP和HTML。我正在将一个PHP变量数组传递给我的HTML代码。我正在创建一个选择下拉列表,并尝试根据我的变量选择一个特定的选项。像这样: //example vars from PHP array (uses magic getter and accessed by $this->varName) //$array = array('aaa' => '123', 'bbb' => '456', 'ccc' => '789'); //$selecte

我正在尝试分离我的PHP和HTML。我正在将一个PHP变量数组传递给我的HTML代码。我正在创建一个选择下拉列表,并尝试根据我的变量选择一个特定的选项。像这样:

//example vars from PHP array (uses magic getter and accessed by $this->varName)
//$array = array('aaa' => '123', 'bbb' => '456', 'ccc' => '789');
//$selected = 'bbb';

<select name="name" id="id">
<option value="0">Choose n Option</option>
<?
foreach($this->array as $key => $value)
{
    ?>
    //$selected may not be set
    <option value="<?=$key?>" <? echo isset($this->selected) ? (strcmp($key,$this->selected)==0 ? 'selected' : '') : '' ?> ><?=$value?></option>
    <?
}
?>
</select>
//PHP数组中的示例变量(使用magic getter并由$this->varName访问)
//$array=array('aaa'=>'123','bbb'=>'456','ccc'=>'789');
//$selected='bbb';
选择n选项
//无法设置$selected

大多数人会开始尖叫你使用模板系统。。。但是由于PHP是一个模板系统,这是多余的。与其打破PHP模式输出几个字符的文本,不如尝试以下方法:

foreach($this->array as $key => $value)
   $sel = isset($this->selected) ? (strcmp($key,$this->selected)==0 ? 'selected' : '') : '';
   echo <<<EOL
<option value="{$key}" {$sel}>{$value}></option>
EOL;
}
foreach($this->array as$key=>$value)
$sel=isset($this->selected)?(strcmp($key,$this->selected)=0?selected':'':'';

echo永远不要绑定业务逻辑和表示层(本例中为HTML模板)。你现在所做的一切都已经失败了

好的,为了使这个东西更易于维护,您需要类似于HTML助手类的东西。这避免了表示中的任何逻辑

class HTMLHelper
{
   public static function isSelected($actual, $expected)
   {
       if ($actual === $expected){
           print 'selected="selected"';
       }
   }

   public static function isChecked()
   {
      // The same goes for checkbox, but you want
      // checked="checked" instead of selected="selected" here
   }
}
注意:对于专用于模板的模板,您应该使用完整的备用模板,而不是

然后在你的演讲中

<select>

<?php foreach($this->array as $key => $value) : ?>

    <option value="<?php echo $key;?>" <?php HTMLHelper::isSelected($this->selected, $key);?> ><?php echo $value; ?></option>

<?php endforeach; ?>

</select>


在使用循环编写HTML时,我经常将所有HTML代码附加到一个变量,并在末尾回显它。对于
所选
变量,不要使用true或false,而是使用
所选
或nothing,然后始终使用变量(如果小于1),谢谢您的建议,Naryl。听起来对我来说很合理。所以我读到的建议说“在HTML中只使用内联PHP”不应该用在这种情况下?这一切都归结为你嵌入了多少PHP。简单的
没有问题。但是如果你要嵌入复杂的逻辑(尤其是嵌套的三元组),那么最好使用其他东西,比如中间变量。这种替代语法对我来说是新的,看起来是个好主意。还有一件事<代码>
。这种语法也适用。我喜欢这个,因为它保留了括号。这也可以吗?替代语法只能在模板中使用。是的,标准语法也可以工作(我没说不行),但你最好避免这种情况。因此,模板更合适的语法应该是
.html,这里的东西..
同样适用于:
foreach/endforeach
while/endwhile
Ha,我通常将括号分隔到一个新行,因此最初它看起来像是另一种语法。哦,显然只是标准语法。为什么(在模板中)要避免这种情况?它似乎仍然是内联PHP。您应该避免模板中的默认语法(

class HTMLHelper
{
   public static function isSelected($actual, $expected)
   {
       if ($actual === $expected){
           print 'selected="selected"';
       }
   }

   public static function isChecked()
   {
      // The same goes for checkbox, but you want
      // checked="checked" instead of selected="selected" here
   }
}
<select>

<?php foreach($this->array as $key => $value) : ?>

    <option value="<?php echo $key;?>" <?php HTMLHelper::isSelected($this->selected, $key);?> ><?php echo $value; ?></option>

<?php endforeach; ?>

</select>