jeudi 26 janvier 2017

Alternating between elements of two different arrays on a matrix with duplicates every 4

I have a web app, where I have two collections (users and products). I have a 4x4 grid where I want to show an alternating element from each collection. However, if the element is in a position where position%4 == 0, there should be two items from that collection. It should look like this where A and B are two different collections:

A - B - A - B

B - A - B - A

A - B - A - B

B - A - B - A

Additionally, if we run out of elements from one of the collections, we continue until the other collection has run out, like this:

A - B - A - B

B - A - B - A

A - B - B - B

B - B - B - B

I am writing this in PHP Laravel 5.3, and here is my blade algorithm as is:

<?php $subjectCount = 1 ?>
<?php $userCount = 1 ?> 
<?php $lastTileType = 'Product' ?> 

@for ($i = 1; $i < $totalTiles; $i++)

    @if ($i%4 == 0 && $lastTileType == 'Product' ? $featureProducts->count() >= $productCount : $featureUsers->count() >= $userCount )
        @if ($lastTileType == 'Product')
            <!-- show the product on the page -->
            <?php $userCount++ ?>
            <?php $lastTileType = 'Product' ?>
        @else
            <!-- show the user on the page -->
            <?php $userCount++ ?>
            <?php $lastTileType = 'User' ?>
        @endif

    @elseif ($i%2 == 0 && $featureUsers->count() >= $userCount)
        <!-- show the product on the page -->
        <?php $userCount++ ?>
        <?php $lastTileType = 'Product' ?>

    @elseif ($i%2 == 1 && $featureProduct->count() >= $productCount)
        <!-- show the product on the page -->
        <?php $userCount++ ?>
        <?php $lastTileType = 'Product' ?>
    @endif 

@endfor



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire