Dave
Dave

Reputation: 108

PHP Loop start at 0 instead of 1

Trying get an increasing number for each image which has to start at 0, its currently outputting from 1 and I don't know why!

Any help is appreciated, thanks in advance!

  <div id="carousel">

    <ul class="thumbs">

   <?php $counts = 0 ; ?>

        <?php  foreach( $images as $image ): $counts++; ?>
            <li>
                <a data-slide-index="<?php echo $counts ;?>" href="">
<img src="<?php echo $image['sizes']['thumbnail']; ?>" alt="<?php echo $image['alt']; ?>" /></a>
            </li>

<?php endforeach; ?>

    </ul>
  </div>

<?php endif; ?>

Upvotes: 0

Views: 3801

Answers (6)

user7461010
user7461010

Reputation:

Try This Code

    <div id="carousel">

    <ul class="thumbs">

   <?php $counts = 0 ; ?>

        <?php  foreach( $images as $image ): $counts++; ?>
            <li>
                <a data-slide-index="<?php if($counts == 1){echo '0';}else {echo $counts 
 ;} ?>" href="">
<img src="<?php echo $image['sizes']['thumbnail']; ?>" alt="<?php echo $image['alt']; ?>" /></a>
            </li>

<?php endforeach; ?>

    </ul>
  </div>

<?php endif; ?>

Upvotes: 0

max
max

Reputation: 101891

foreach( $images as $image ): $counts++;

Is run on the first iteration. Thus

$counts = 0;
foreach( $images as $image ): $counts++;

$counts == 1 // true on first iteteration

You can either do

$counts = -1 // Code smell!

Or

<?php 
$counts = 0 ;  
foreach( $images as $i => $image )?>
<li>
  <a data-slide-index="<?php echo $i ?>" href="#">
     <img src="<?php echo $image['sizes']['thumbnail']; ?>" alt="<?php echo $image['alt']; ?>" /></a>
</li>
<?php endforeach; ?>

Upvotes: 1

Mohammad Masoudian
Mohammad Masoudian

Reputation: 3491

because you are increamenting $counts before echo

so just do this :

<?php $counts = -1 ; ?>

or this:

foreach( $images as $image )
{
    echo '<li>
        <a data-slide-index="' . $counts . '" href="">
        <img src="' . $image['sizes']['thumbnail'] . '" alt="' . $image['alt'] . '" /></a>
    </li>';
    $counts++;
}

Upvotes: 0

Xanarus
Xanarus

Reputation: 775

Write your code like this :

<div id="carousel">

    <ul class="thumbs">

   <?php $counts = 0 ; ?>

        <?php  foreach( $images as $image ) { ?>
            <li>
                <a data-slide-index="<?php echo $counts ;?>" href="">
<img src="<?php echo $image['sizes']['thumbnail']; ?>" alt="<?php echo $image['alt']; ?>" /></a>
            </li>



<?php  $counts++; }  ?>

    </ul>
  </div>

<?php endif; ?>

Upvotes: 0

Nathan Dawson
Nathan Dawson

Reputation: 19308

You increment $counts before you use it. You start it at 0 but as soon as you start the foreach and run $counts++; it becomes 1.

Move $counts++; to the end of the foreach loop.

<?php $counts = 0 ; ?>
<?php foreach( $images as $image ) : ?>
    <li>
        <a data-slide-index="<?php echo $counts; ?>" href="">
            <img src="<?php echo $image['sizes']['thumbnail']; ?>" alt="<?php echo $image['alt']; ?>" />
        </a>
    </li>

    <?php $counts++; ?>
<?php endforeach; ?>

Upvotes: 1

deem
deem

Reputation: 1884

Instead of

<?php foreach( $images as $image ): $counts++; ?> 

do

<?php $counts++; endforeach; ?>

to increment after the text is printed.

Upvotes: 0

Related Questions