Foreach里面的if()| if()关闭问题

时间:2014-06-05 07:10:21

标签: php codeigniter if-statement foreach

我将一个foreach语句放在if语句中,并且在关闭if语句时遇到问题。 if检查“type”,如果条目类型与选择的“type”匹配,它将输出带有foreach的“type”的所有条目。

这是一个例子。

<?php 
  if($row->type == 'HTML')
  {
    foreach($content as $row): ?>
       <h2><?= $row->title ?></h2> 
       <span class="rating"> <?= $row->rating?>/5</span>
       <p class="desc"><?= $row->description ?> </p>
       <span class="date"><?= $row->created ?> </span>
       <a href="<?= $row->url ?>">View Tutorial</a>
       <span class="diff"><?= $row->difficulty ?></span>
       <span class="auth">Added by </span> 
<?php } else {
      echo 'No results found';
 }
 endforeach; ?>

4 个答案:

答案 0 :(得分:1)

您会在循环中找到$row,因此请将if() else保留在foreach中以获取$row->type

  <?php 
    foreach($content as $row): 
     if($row->type == 'HTML')
      {?>
       <h2><?= $row->title ?></h2> 
       <span class="rating"> <?= $row->rating?>/5</span>
       <p class="desc"><?= $row->description ?> </p>
       <span class="date"><?= $row->created ?> </span>
       <a href="<?= $row->url ?>">View Tutorial</a>
       <span class="diff"><?= $row->difficulty ?></span>
       <span class="auth">Added by </span> 
    <?php } else {
      echo 'No results found';
 }
endforeach; ?>

答案 1 :(得分:1)

<?php 
foreach($content as $row):
    if($row->type == 'HTML') { ?>
        <h2><?= $row->title ?></h2> <span class="rating"> <?= $row->rating?>/5</span>
        <p class="desc"><?= $row->description ?> </p>
        <span class="date"><?= $row->created ?> </span>
        <a href="<?= $row->url ?>">View Tutorial</a>
        <span class="diff"><?= $row->difficulty ?></span>
        <span class="auth">Added by </span> 
    <?php 
    } else {
        echo 'No results found';
    }
endforeach; ?>

答案 2 :(得分:1)

你的结构应该是:

  • 如果
    • 的foreach
      • 含量
    • 结束foreach
  • 否则
    • 其他内容
  • 结束如果

您的结构是

  • 如果
    • 的foreach
      • 含量
  • 否则
    • 其他内容
  • 结束如果
    • 结束foreach

你可能正在寻找这个:

<?php 
$found = FALSE; // used to track whether something has been found.
foreach($content as $row){
   if($row->type == 'HTML')
   {
       $found = TRUE; // there is at least one row with ->type == 'HTML'
       ?>
       <h2><?= $row->title ?></h2> 
       <span class="rating"> <?= $row->rating?>/5</span>
       <p class="desc"><?= $row->description ?> </p>
       <span class="date"><?= $row->created ?> </span>
       <a href="<?= $row->url ?>">View Tutorial</a>
       <span class="diff"><?= $row->difficulty ?></span>
       <span class="auth">Added by </span> 
    <?php 
   }
 }; // moved to the {} closing
 // test whether any valid rows have been found (set above)
 if(!$found){echo 'No results found';}
 ?>

答案 3 :(得分:0)

你必须这样:

<?php 
    foreach($content as $row): ?>
      if($row->type == 'HTML')
      {
       <h2><?= $row->title ?></h2> 
       <span class="rating"> <?= $row->rating?>/5</span>
       <p class="desc"><?= $row->description ?> </p>
       <span class="date"><?= $row->created ?> </span>
       <a href="<?= $row->url ?>">View Tutorial</a>
       <span class="diff"><?= $row->difficulty ?></span>
       <span class="auth">Added by </span> 
     <?php } else {
         echo 'No results found';
      }
     ?>
  <?php   endforeach; ?>

你的foreach条件应高于if else条件。