tags:

views:

241

answers:

2

This is the beginning of a php for loop. I would like to edit it so that 'INSERT NUMBER HERE' gets replaced with an incrementing number. Ie. first loop it would be 1, then 2, then 3 etc.

<?php foreach ($_productCollection as $_product): ?>
    <div style="float: left; font-size: 120px;height:50px;padding-top:50px; color:#ccc">INSERT NUMBER HERE</div>
    <div class="listing-item<?php if( ++$_iterator == sizeof($_productCollection) ): ?> last<?php endif; ?>">

How can I achieve this?

Thanks

+2  A: 
<?php 
$n = 0;
foreach ($_productCollection as $_product): 
$n++;
?>
<div style="float: left; font-size: 120px;height:50px;padding-top:50px; color:#ccc"><?php echo $n; ?>
</div>
<div class="listing-item<?php if( ++$_iterator == sizeof($_productCollection) ): ?> last<?php endif; ?>">
dfilkovi
+1, I was about fifteen seconds too slow... =)
David Thomas
A: 

You could use array_keys() to get an indexed array from an associative array, at which point foreach will yield the index as the new key, and the old key as the new value. Indexing the old array with the new value will give you the old value, and the new key will be a counter for elements in the array.

Ignacio Vazquez-Abrams