如果声明不显示div / h2

时间:2013-04-01 16:50:36

标签: php

这对于每个/ if语句显示更改,然后在其下方显示所做的更改。

我正在尝试写一个if语句,告诉它不显示h2和#change_box,如果没有变化。

非常感谢帮助。

<h2 class="changes"> Changes: </h2>
<div id="change_box">
<? foreach ($audit['Events'] as $event):?>  
<?if ( $event['Type'] != 'Comment'):?>  
<span class="field">
<?= $event['Field']?>
</span>:
<?= $event['Value'] ?>      
<?=($event['Previous'])?>
<?endif?>
<?endforeach?>  
</div>  

4 个答案:

答案 0 :(得分:0)

请在每个<?后留出空格,并确保启用短标记

  <?php if(is_array($audit['Events']) && $audit['Events']):?>
  <h2 class="changes"> Changes: </h2>
  <div id="change_box">
  <?php foreach ($audit['Events'] as $event):?>  
     <?php if ( $event['Type'] != 'Comment'):?>  
        <span class="field">
        <?php echo $event['Field'];?>
        </span>:
        <?php echo $event['Value']; ?>      
        <?php echo $event['Previous'];?>
     <?php endif;?>
  <?php endforeach;endif;?>  
 </div>

答案 1 :(得分:0)

<?php

    if ($changes) { // You'll have to set this variable
        //Echo all of your HTML
    else {
        //Echo the stuff you'd rather show if they didn't change anything
    }

?>

让您了解我如何编写代码

<?php

  if ($changes) {
    echo '<h2 class="changes">Changes:</h2>';
    echo '<div id="change_box">';

    foreach ($audit['Events'] as $event) { 
      if ($event['Type'] != 'Comment') {
        echo $event['Field'] . </span> . $event['Value'] . $event['Previous'];
      }   
    }

    echo "</div>"
  }

?>

答案 2 :(得分:0)

您可以使用if之类的

来包装代码
if(count($audit['Events'])>0)
{

//Your code

}

答案 3 :(得分:0)

你每次都打开和关闭php标签吗?当你想要打印HTML代码时,最好用PHP编写所有内容并回显。

<?php
echo "<h2 class=\"changes\"> Changes: </h2>";
echo "<div id=\"change_box\">";

foreach ($audit['Events'] as $event) {
    if ( $event['Type'] != 'Comment') {
        echo "<span class=\"field\">".$event['Field']."</span>";
        echo $event['Value'];
        echo "(".$event['Previous'] .")";
    }
}

echo "</div>";
?>