-1

I want to display only certain data from database on my page, so I end my foreach loop with if and break; statement. I checked with var_dump() and the data is being provided from database correctly, function getName() is also correct.

Moreover I inserted there a test "echo"test"; which is displaying well. But getName() is still missing.

I think that problem might be with this if statement, because when I delete it everything works correctly (except that I get data I dont want to get).

Same situation is when I insert if statement after the getName() function - this part of the code is not executed, it seems.

<?php //var_dump($leagues); ?>
<?php foreach($leagues as $leauge): ?>
  <?php if($leauge->getLevel() != 'A') { break; } ?>
  <li>
    <i class="fas fa-long-arrow-alt-right"></i>
    <?php $leauge->getName(); echo "test"; ?>
  </li>
<?php endforeach; ?>
ADyson
  • 57,178
  • 14
  • 51
  • 63
Gutek
  • 75
  • 7

2 Answers2

3
  1. There is one echo missing.

    Replace: <?php $leauge->getName(); echo "test"; ?>

    With: <?php echo $leauge->getName(); ?>

  2. Consider to use continue; instead of break;

The break statement terminates the whole loop early whereas the continue brings the next iteration early. Reference: Geeks for Geeks

As you are using break IF $leauge->getLevel() != 'A', in case of your first iteration goes to this condition, your entire loop will terminate. Just change to continue if you wanna "skip the current league, but go to the next one and check again".

Leonardo Gomes
  • 275
  • 2
  • 7
  • No, sadly it didnt help at all. The number of iteration of foreach loop is correct, and its not a problem. Problem is that the getName function doesnt give any output, though it should. – Gutek Jan 26 '22 at 13:18
  • Moreover if i switch the lines of code places. And if first call the getName() function and will then check the end of loop condition. The loop will not end, even echo"test" not gonna show at all – Gutek Jan 26 '22 at 13:21
  • @Gutek, can you try to use: getName(); ?> Please let me know. I think the "echo" is also missing, in addition to the break vs continue. – Leonardo Gomes Jan 26 '22 at 14:05
  • Sorry that i`m answering so late. Had a lof of things to do. In meanwhile i discovered what was bad. In this line of code '''getName(); echo "test"; ?>''' . When i want to display results i have to use '''=''' instead of ''' – Gutek Jan 26 '22 at 19:38
0

Rewriting it here so it will be more seeable. What helped me: In this line of code

<?php $leauge->getName(); echo "test"; ?>

When i want to display results i have to use

<?= instead of <?php . Why is that i dont really know.

Gutek
  • 75
  • 7