0

这对于每个 / 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

4 回答 4

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>
于 2013-04-01T16:52:03.413 回答
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>"
  }

?>
于 2013-04-01T16:52:46.630 回答
0

你可以用iflike来包装你的代码

if(count($audit['Events'])>0)
{

//Your code

}
于 2013-04-01T16:52:50.797 回答
0

你是否每次都打开和关闭 php 标签?最好在 PHP 中制作所有内容并在您想要打印 HTML 代码时回显。

<?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>";
?>
于 2013-04-01T16:58:31.877 回答