lundi 29 octobre 2018

How to break only one if condition and go to next if condition within foreach in php

I have an array containing some size as key, and quantity as value

  Array

[0] => Array
    (
        [design] => ODES657
        [color] => 347
        [06XL] => 2
        [mrp] => 0
        [net_qty] => 2
    )

[1] => Array
    (
        [design] => ODES665
        [color] => 303
        [04XL] => 3
        [mrp] => 0
        [net_qty] => 3
    )

[2] => Array
    (
        [design] => MSAF104
        [color] => 332
        [S] => 1
        [mrp] => 0
        [net_qty] => 3
        [XXL] => 2
    )

In my view page I want to add table header like if any of the array key contains a size key like "4XL" or "5XL", it will be printed as table head, or if no key contains that size value, no table header will be printed. So, I did a foreach loop, and write if condition to check if array key exists or not

    <th>Design</th>
                            <th>Color</th>
                            <th>S/20/6NO</th>
                            <th>M/22/7NO</th>
                            <th>L/24/8NO</th>
                            <th>XL/26/9NO</th>
                            <th>XXL/28/10NO</th>
                            <th>03XL/30</th>
                            @foreach($cart_items as $size)
                              @if(array_key_exists('04XL',$size) || array_key_exists('32/80CM',$size))
                              <?php $first = "ok"; break; ?>
                              @else
                              <?php $first = "no"; ?>
                              @endif
                              @if(array_key_exists('05XL',$size) || array_key_exists('34/85CM',$size))
                              <?php $second = "ok";break; ?>
                              @else
                              <?php $second = "no"; ?>
                              @endif
                              @if(array_key_exists('06XL',$size))
                              <?php $third = "ok"; break; ?>
                              @else
                              <?php $third = "no"; ?>
                              @endif
                              @if(array_key_exists('UNDEFINED',$size))
                              <?php $fourth = "ok";break; ?>
                              @else
                              <?php $fourth = "no"; ?>
                              @endif
                            @endforeach

                            @if($first == "ok")
                              <th>04XL/32</th>
                            @endif
                            @if($second == "ok")
                              <th>05XL/34</th>
                            @endif
                            @if($third == "ok")
                              <th>06XL</th>
                            @endif
                            @if($fourth == "ok")
                              <th>UNDEFINED</th>
                            @endif
                            <th>Total Qty</th>
                            <th>RSP Value</th>

In table data section I also do a normal foreach loop and print the data like

     @if($first == "ok")
                              @if(array_key_exists('04XL',$contain))
                                <td></td>
                              @elseif(array_key_exists('32/80CM',$contain))
                                <td></td>
                              @else
                                <td>-</td>
                              @endif
                            @endif
                            @if($second == "ok")
                              @if(array_key_exists('05XL',$contain))
                                <td></td>
                              @elseif(array_key_exists('34/85CM',$contain))
                                <td></td>
                              @else
                                <td>-</td>
                              @endif
                            @endif
                            @if($third == "ok")
                              @if(array_key_exists('06XL',$contain))
                                <td>
                              @endif
                            @endif
                            @if($fourth == "ok")
                              @if(array_key_exists('UNDEFINED',$contain))
                                <td>
                              @endif
                            @endif
                            <td></td>
                            <td></td>

On using break in if statement, this break put me out of the foreach loop, so if $first exists in array, it breaks the foreach loop, and $second, $third, $fourth become undefined. So, I want to know how to break only that if statement, go to next if statement but not breaking the foreach loop.

Aucun commentaire:

Enregistrer un commentaire