Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to write a simple if-statement for indexes 0 and 10, 1 and 11, 2 and 12 etc

I need to write a condition for the index.
First condition do a function for indexes 0, 10, 20, 30
Second condition do a function for index 1, 11, 21, 31
Third condition do a function for index 2, 12, 22, 32
(..)

Actually I have look like:

@if ($loop->index == 0 || $loop->index == 10 || $loop->index == 20 || $loop->index == 30 || $loop->index == 40 || $loop->index == 50 || $loop->index == 60 || $loop->index == 70 || $loop->index == 80 || $loop->index == 90) 
   
     //do something
    
        ...
        
@elseif ($loop->index == 4 || $loop->index == 14 || $loop->index == 24 || $loop->index == 34 || $loop->index == 44 || $loop->index == 54 || $loop->index == 64 || $loop->index == 74 || $loop->index == 84 || $loop->index == 94) 

        ...

     // do something

@elseif ($loop->index == 9 || $loop->index == 19 || $loop->index == 29 || $loop->index == 39 || $loop->index == 49 || $loop->index == 59 || $loop->index == 69 || $loop->index == 79 || $loop->index == 89 || $loop->index == 99) 

    // do something

@endif

I know it doesn’t look good, it works for me now, but I need advice because I have no idea how to write it a bit simpler.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

All the best,
Thanks.

>Solution :

You can use the modulo operator % to get the remainder when dividing.

@switch($loop->index % 10)
  @case(1)
    // First case...
    @break

  @case(2)
    // Second case...
    @break

  @default
    // otherwise
@endswitch

You can use switch, but you can use your @if/@elseif as well.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading