Saud Kazia
Saud Kazia

Reputation: 192

Display foreach for different keys in an array in PHP

I have the following array:

<?php

$sets = array (
    array (
        'img'        => 'file.png',
        'heading'      => 'Slide Title 1',
        'lead'       => 'Slide leadription 1',

    ),
    array (
        'img'        => 'file.png',
        'heading'      => 'Slide Title 2',
        'lead'       => 'Slide leadription 2',

    ),
    array (
        'img'        => 'file.png',
        'heading'      => 'Slide Title 3',
        'lead'       => 'Slide leadription 2',

    ),
    array (
        'img'        => 'file.png',
        'heading'      => 'Slide Title 3',
        'lead'       => 'Slide leadription 2',

    )

);

?>

Which provides the input for this

<?php  
    foreach ($sets as $set) {
?>
    <!-- START THE FEATURETTES -->
    <div class="row featurette">
        <div class="col-md-7">
            <h2 class="featurette-heading"><?php echo $set['heading']?></h2>
            <p class="lead"><?php echo $set['lead']?></p>
        </div>
        <div class="col-md-5">
            <img class="featurette-image img-responsive center-block" src="<?php echo $set['img']?>" alt="Feature">
        </div>
    </div>

<?php
    }
?>

Now this is working perfectly but I want the md-7 HTML and md-5 HTML to alternate so every other one will now be

<hr class="featurette-divider">
<div class="row featurette">
    <div class="col-md-7">
        <img class="featurette-image img-responsive center-block" src="<?php echo $set['img']?>" alt="Feature">
    </div>
    <div class="col-md-5">
        <h2 class="featurette-heading"><?php echo $set['heading']?></h2>
        <p class="lead"><?php echo $set['lead']?></p>
    </div>
</div>

So basically alternating between the picture and details left-right

UPDATE

As per Jhansen's suggestion This DOES NOT WORK. It will only take the first set, it won't alternate between the two.

<?php 


foreach ($sets as $set) {
?>

      <!-- START THE FEATURETTES -->

          <?php $count = 1; ?>
<?php if( $count % 2 != 0 ): ?>
      <hr class="featurette-divider">

          <div class="row featurette">
            <div class="col-md-7">
              <img class="featurette-image img-responsive center-block" src="<?php echo $set['img']?>" alt="Feature">
            </div>

            <div class="col-md-5">
              <h2 class="featurette-heading"><?php echo $set['heading']?></h2>
              <p class="lead"><?php echo $set['lead']?></p>
            </div>
</div>

<?php else: ?>

      <hr class="featurette-divider">

          <div class="row featurette">

            <div class="col-md-7">
              <h2 class="featurette-heading"><?php echo $set['heading']?></h2>
              <p class="lead"><?php echo $set['lead']?></p>
            </div>
            <div class="col-md-5">
              <img class="featurette-image img-responsive center-block" src="<?php echo $set['img']?>" alt="Feature">
            </div>

</div>
<?php endif; ?>`


<?php
}?>

Upvotes: 0

Views: 102

Answers (2)

Nacho Badia
Nacho Badia

Reputation: 136

If you want to switch all loops, the proposed jhansen is correct:

<?php 
$count=1;
foreach ($sets as $set) { ?>
    <!-- START THE FEATURETTES -->
    <hr class="featurette-divider">
    <div class="row featurette">

        <?php if($count % 2 != 0){ ?>

        <div class="col-md-7">
            <img class="featurette-image img-responsive center-block" src="<?php echo $set['img']?>" alt="Feature">
        </div>
        <div class="col-md-5">
            <h2 class="featurette-heading"><?php echo $set['heading']?></h2>
            <p class="lead"><?php echo $set['lead']?></p>
        </div>

        <?php }else{ ?>

        <div class="col-md-7">
            <h2 class="featurette-heading"><?php echo $set['heading']?></h2>
            <p class="lead"><?php echo $set['lead']?></p>
        </div>
        <div class="col-md-5">
            <img class="featurette-image img-responsive center-block" src="<?php echo $set['img']?>" alt="Feature">
        </div>

        <?php } ?>
    </div>
<?php  $count++; } ?>

If you want to switch only the first should do something like this:

<?php 
foreach ($sets as $k => $set) { ?>
    <!-- START THE FEATURETTES -->
    <hr class="featurette-divider">
    <div class="row featurette">
        <?php if($k==0){ ?>

        <div class="col-md-7">
            <img class="featurette-image img-responsive center-block" src="<?php echo $set['img']?>" alt="Feature">
        </div>
        <div class="col-md-5">
            <h2 class="featurette-heading"><?php echo $set['heading']?></h2>
            <p class="lead"><?php echo $set['lead']?></p>
        </div>

        <?php }else{ ?>

        <div class="col-md-7">
            <h2 class="featurette-heading"><?php echo $set['heading']?></h2>
            <p class="lead"><?php echo $set['lead']?></p>
        </div>
        <div class="col-md-5">
            <img class="featurette-image img-responsive center-block" src="<?php echo $set['img']?>" alt="Feature">
        </div>

        <?php } ?>
    </div>
<?php  } ?>

Upvotes: 1

jhansen
jhansen

Reputation: 284

If you're just wanting to alternate, why not encapsulate the html within a modulus statement?

IE,

<?php $count = 1; ?>
<?php if( $count % 2 != 0 ): ?>
    ... HTML for first arrangement ...
<?php else: ?>
    ... HTML for second arrangement ...
<?php endif; ?>`

Upvotes: 2

Related Questions