mardi 28 mars 2017

Laravel If Else statement inside foreach

I'm trying to use if/else statement inside my foreach loop. With DomDocument i'm trying to find the description tag and the inside of the description tag.

This works fine. When there is a description tag, it display's what's inside the description tag. If there is no description tag it displays: No description found No description found No description found No description found. Why does it show the error four times? How do i fix this?

I'm using DomDocument and Laravel 5.4

Meta tag:

$meta = $dom->getElementsByTagName('meta');

My code:

@foreach ($meta as $node)
  @if($node->getAttribute('name')=='description') 
    
      @else 
       No description found
  @endif
@endforeach

Edit, according to Muthu17:

I have almost written the same code as you. But the only problem is: It shows "No description found" all the time. Even if it finds the description tag, it show's the description tag init and "No description found".

I've declared the $showErrorMessage = 0; in my controller.

@foreach ($meta as $node)
 @if($node->getAttribute('name')=='description') 
  
   @else
    {!! $showErrorMessage == 1; !!}
   @endif
@endforeach

@if($showErrorMessage = 1)
 No description found
@endif

Aucun commentaire:

Enregistrer un commentaire