I am running a foreach over an array and am looking to break up my foreach statement into four stages:
<?php foreach ($result->data as $post): ?>
<?php if($photo_count < 2) { ?>
<!-- Renders images. @Options (thumbnail,low_resoulution, high_resolution) -->
<a class="col-md-2" rel="group1" href="<?= $post->images->standard_resolution->url ?>"><img src="<?= $post->images->thumbnail->url ?>"></a>
<?php $photo_count++; ?>
<?php echo $photo_count; ?>
<?php } elseif($photo_count = 2) { ?>
<p>testing</p>
<?php $photo_count++; ?>
<?php echo $photo_count; ?>
<?php } elseif($photo_count > 2) { ?>
<a class="col-md-2" rel="group1" href="<?= $post->images->standard_resolution->url ?>"><img src="<?= $post->images->thumbnail->url ?>"></a>
<?php $photo_count++; ?>
<?php echo $photo_count; ?>
<?php } else {
break;
} ?>
<?php endforeach ?>
Earlier I declared $photo_count to equal 1. Basically what I want to do is: -When the photo count is less than 2 - do something -When the photo count is equal to 2 - do something -When the photo count is great than 2 - do something.
For some reason it is getting stuck when $photo_count is equal to 3 and looping over that.
Any advice would be appreciated.
=
is assignment and ==
is comparison while ===
is comparison of content as well as datatype.. use them carefully.
Change
elseif($photo_count = 2)
to
elseif($photo_count == 2)
Replace
<?php } elseif($photo_count = 2) { ?>
By
<?php } elseif($photo_count == 2) { ?>
Do not confuse assignment operators and comparison operators.
@Danyal Sandeelo's answer is correct, I just want to help you cleanup your code, you dont need too much php tags there, here is a optimized code:
<?php
foreach ($result->data as $post){
if($photo_count < 2) {
echo '<a class="col-md-2" rel="group1" href="'.$post->images->standard_resolution->url.'"><img src="'.$post->images->thumbnail->url.'"></a>';
$photo_count++;
echo $photo_count;
} elseif($photo_count = 2) {
echo '<p>testing</p>';
$photo_count++;
echo $photo_count;
}
elseif($photo_count > 2) {
echo'<a class="col-md-2" rel="group1" href="'.$post->images->standard_resolution->url.'"><img src="'.$post->images->thumbnail->url.'"></a>';
$photo_count++;
echo $photo_count;
}
else {
break;
}
}
?>