Home > Software engineering >  Laravel grouping div according to their input value
Laravel grouping div according to their input value

Time:12-05

I am trying a sport project using laravel 8. There is a hidden input inside the div and if the value is 1, it creates a div with a class named "superseted" and gathers it all in it. As follows:

<div class="superseted">
    <div><input type="hidden" value="1"> Products</div>
    <div><input type="hidden" value="1"> Products2</div>
</div>

But not all inputs values are 1. Values in the database are sorted from top to bottom like this,

1, 1, 0, 0, 1, 1,

So, we need to group in a structure like this,

<div class="program">
    <!-- grouped -->
    <div class="superseted">
        <div> <input type="hidden" value="1"> Products</div>
        <div> <input type="hidden" value="1"> Products2</div>
    </div>
    <!-- This is not grouped -->
    <div><input type="hidden" value="0"> Products3</div>
    <div><input type="hidden" value="0"> Products4</div>
    <!-- grouped -->
    <div class="superseted">
        <div> <input type="hidden" value="1"> Products5</div>
        <div> <input type="hidden" value="1"> Products6</div>
    </div>
</div>

We need to create a structure like this. The foreach we are using does not work as it should be. Here is the foreach

<div class="program">
    @foreach($d->movementdetail as $detail)
        <div class="{{ $detail->movement_superset==1 ? "superseted" :  "" }}">
            <div><input type="hidden" value="{{$detail->movement_superset}}">ProductName</div>
            @if($detail->movement_superset==1)
                <input type="checkbox" hljs-string">">
            @endif
        </div>
    @endforeach
</div>

How do we get the structure we want? Thanks.

CodePudding user response:

You could try the following code:

<div class="program">
    @php
        $last_value = 0;
    @endphp
    @foreach($d->movementdetail as $detail)
        <!-- superset div tag control -->
        @if ($detail->movement_superset == 1)
            @if(!$last_value)
                <div class="superseted">
            @endif
        @else
            @if($last_value)
                </div>
            @endif
        @endif
        <!-- superset div tag control ends -->

        <div><input type="hidden" value="{{$detail->movement_superset}}">{{$detail->movement_superset}}</div>
        @if($detail->movement_superset==1)
            <input type="checkbox" hljs-string">">
        @endif

        @php
            $last_value = $detail->movement_superset;
        @endphp
    @endforeach
</div>
  • Related