0

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
}?>
2
  • So you just want to change md-5 with md-7 every other iteration? If yes, have you tried something to accomplish that? Commented Apr 26, 2015 at 15:09
  • Rizier. If I was able to find a way to do it, I wouldn't ask in the first place. I have shown my work and the downvote is completely uncalled for. Atleast Jhansen was helpful. Thank you. Commented Apr 26, 2015 at 15:43

2 Answers 2

2

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; ?>`
Sign up to request clarification or add additional context in comments.

1 Comment

this doesn't work -? HTML is too much to paste here
1

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  } ?>

1 Comment

This works. Thanks. Jhansen's answer didn't include <?php $count++; } ?>

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.