在Foreach循环中正确使用If Else

时间:2014-10-22 00:15:13

标签: php if-statement foreach

我试图在foreach循环中打印一些信息:

<?php foreach($output['data']['rooms'] as $info): ?>
            <?php if($info['room_number'] == $id): ?>
               <h1 style="font-size: 20pt; color: white;">Room: <?php echo $info['room_number']; ?></h1>
               <?php echo '<a style="float: right;" class="button1" href="'.$curIndex.'">Home</a>'; ?>
               <hr style="margin-top: 20px;">
                    <?php $services = reset($info['services']); ?>
               <h2 style="font-size: 14pt; color: #924c9e;"> Room Charges: <?php echo ($services['room_charges']['enabled'] == 1) ? 'ENABLED' : 'DISABLED'; ?></h2>
               <h2 style="font-size: 14pt; color: #924c9e;"> Adult: <?php echo ($services['adult']['enabled'] == 1) ? 'ENABLED' : 'DISABLED'; ?>  </h2>              
               <h2 style="font-size: 14pt; color: #924c9e;"> Room Status: <?php echo $info['status']; ?></h2>
    <?php else : echo "No data found for the room number you entered."; ?>
      <?php endif; ?>
        <?php endforeach; ?>  

无法正常工作的部分是<?php else: "no data found for the room number you entered."; ?>

else语句回显了foreach循环中每个条目的信息,而不仅仅是一次。最简单的解释我想要做的事情:

如果room_number与用户输入的id匹配,则打印与该房间号对应的所有内容,否则打印未找到该房间的数据。如果找不到房间号,我只希望"No data found for the room number you entered"在页面上显示一次。

如果你需要,我可以给你更多的代码,但我认为这应该涵盖它。我还没有能够在else循环中正确地包含foreach

感谢您的帮助!如果你能帮我解决一下,我一定要把答案标记得正确。

2 个答案:

答案 0 :(得分:1)

一般来说,我这样做的方式与你的方式略有不同。

// TODO: declare a variable called matchFound and set to false
foreach($output['data']['rooms'] as $info):
    if($info['room_number'] == $id):
        // TODO: set matchFound = true
        // TODO: print out room data
        // TODO: break the foreach loop since looping through the remaining records is pointless
    endif;
endforeach;
// TODO: if the matchFound variable is false, then print the "No data found for the room number you entered" message

如果您需要澄清,请告诉我。自从我使用PHP以来已经有一段时间了,所以我的语法有点生疏,但如果你自己无法解决这个问题,我可能会想出如何编写“TODO:”行。

编辑,我认为这是正确的语法,它应该有效:

<?php $matchFound = false; ?>
<?php foreach($output['data']['rooms'] as $info): ?>
    <?php if($info['room_number'] == $id): ?>
        <?php $matchFound = true; ?>
        <h1 style="font-size: 20pt; color: white;">Room: <?php echo $info['room_number']; ?></h1>
        <?php echo '<a style="float: right;" class="button1" href="'.$curIndex.'">Home</a>'; ?>
        <hr style="margin-top: 20px;">
        <?php $services = reset($info['services']); ?>
        <h2 style="font-size: 14pt; color: #924c9e;"> Room Charges: <?php echo ($services['room_charges']['enabled'] == 1) ? 'ENABLED' : 'DISABLED'; ?></h2>
        <h2 style="font-size: 14pt; color: #924c9e;"> Adult: <?php echo ($services['adult']['enabled'] == 1) ? 'ENABLED' : 'DISABLED'; ?>  </h2>              
        <h2 style="font-size: 14pt; color: #924c9e;"> Room Status: <?php echo $info['status']; ?></h2>
        <?php break; ?>
    <?php endif; ?>
<?php endforeach; ?>
<?php if (!$matchFound): ?>
    <?php echo "No data found for the room number you entered."; ?>
<?php endif; ?>

答案 1 :(得分:1)

如果我理解了这种情况,一旦你发现找不到房间,你就想停止循环。

如果是这样,请执行此操作(我在else条件中添加了break语句)。您可能需要封装在使用{}满足else条件时执行的操作。我更喜欢if(条件){先做这个;做第二个;}语法。

无论如何,将break语句作为代码的一部分添加到其他条件时运行应解决问题。

<?php foreach($output['data']['rooms'] as $info): ?>
        <?php if($info['room_number'] == $id): ?>
           <h1 style="font-size: 20pt; color: white;">Room: <?php echo $info['room_number']; ?></h1>
           <?php echo '<a style="float: right;" class="button1" href="'.$curIndex.'">Home</a>'; ?>
           <hr style="margin-top: 20px;">
                <?php $services = reset($info['services']); ?>
           <h2 style="font-size: 14pt; color: #924c9e;"> Room Charges: <?php echo ($services['room_charges']['enabled'] == 1) ? 'ENABLED' : 'DISABLED'; ?></h2>
           <h2 style="font-size: 14pt; color: #924c9e;"> Adult: <?php echo ($services['adult']['enabled'] == 1) ? 'ENABLED' : 'DISABLED'; ?>  </h2>              
           <h2 style="font-size: 14pt; color: #924c9e;"> Room Status: <?php echo $info['status']; ?></h2>
<?php else : echo "No data found for the room number you entered."; break;?>
  <?php endif; ?>
    <?php endforeach; ?>  
相关问题