0

I am trying to print some information within a foreach loop:

<?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; ?>  

The part that is NOT working correctly is the <?php else: "no data found for the room number you entered."; ?>

The else statement echo's that information for every entry within the foreach loop, rather than just one time. Easiest way of explaining what i'm trying to do:

if room_number matches the id the user enters, then print everything that corresponds to that room number, else print that room's data was not found. If room number not found, I only want "No data found for the room number you entered" to display one time on the page.

I can give you more code if you need it, but I think this should cover it. I just haven't been able to correctly include the else within the foreach loop.

Thank you for your help! I'll be sure to mark your answer correct if you can help me figure it out.

ValleyDigital
  • 1,460
  • 4
  • 21
  • 37
  • Is it printing the Hotel information correctly? Or does it just print the text in the else statement? – Gohn67 Oct 22 '14 at 00:22
  • It prints the hotel information correctly. The room number == id, it prints the information for that room number. If room number !== id, it echos "no data found" dozens of time, for each entry within the foreach loop. @Gohn67 – ValleyDigital Oct 22 '14 at 00:24
  • So in this example you have 12 rooms and it prints "no data..." for all 12 rooms, but also prints the correct Hotel information? Sorry, just want to make sure I understand. – Gohn67 Oct 22 '14 at 00:26
  • Yes, exactly. No worries. I appreciate your help. – ValleyDigital Oct 22 '14 at 00:28

2 Answers2

1

Generally, the way I'd go about doing this is a little bit different from what you have.

// 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

Let me know if you need clarification. It's been a while since I used PHP, so I'm a bit rusty on the syntax, but I could probably figure out how to write the "TODO:" lines if you can't figure it out yourself.

Edit, I think this is correct syntax and it should work:

<?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; ?>
peaceoutside
  • 942
  • 7
  • 12
  • 1
    I tried it that way, too. The problem remained that even though the room_number == id, and brought in the correct information and below that the matchFound variable is false still printed the "no data found"... If you can give me an example of what you're talking about that would be great. But don't worry too much, I am still working on it. Thank you, though! – ValleyDigital Oct 22 '14 at 00:31
  • 1
    Perfect. Thank you! I just needed to add the last – ValleyDigital Oct 22 '14 at 00:41
  • 1
    you have been a huge help in with this question. I was wondering if you could please take a look at this new question I have http://stackoverflow.com/questions/26744290/json-creating-a-form-to-search-a-date-range-using-datetime-value – ValleyDigital Nov 04 '14 at 20:35
1

If I understand the situation correctly, you'd like to stop looping once you identify that the room is not found.

If so, do this (I added a break statement in the else condition). You may need to encapsulate what do do when the else condition is met with { }. I prefer the if (condition) {do this first; do this second;} syntax.

Whatever the case, adding the break statement as part of the code to run when it hits the else condition should fix the issue.

<?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; ?>  
arkyc
  • 149
  • 6
  • Whilst this will fix your problem, it's probably not the best way to solve it. There are better solutions. But I've provided an answer based upon your existing code. See here also: [http://stackoverflow.com/questions/9215588/break-out-of-if-and-foreach] – arkyc Oct 22 '14 at 00:32
  • this solved the issue of repeated entries for the foreach loop. Thank you. Problem now, is that even when room number == id, the else statement is still applied. It displays the correct information, and also displays "no data found..." any idea? – ValleyDigital Oct 22 '14 at 00:35
  • Not sure if you still have the issue, but I'd do a string compare. – arkyc Oct 22 '14 at 00:53