if($i==6) {
$result .= '<a style="display:none" class="fancybox" data-fancybox-group="gallery" href="'.$value['images']['standard_resolution']['url'].'"></a> '.$caption.PHP_EOL;;}
else {
$result .= "\t".'<a class="fancybox" data-fancybox-group="gallery" href="'.$value['images']['standard_resolution']['url'].'"><img id="thumb'.++$i.'" src="'.$value['images']['low_resolution']['url'].'" alt="'.$value['caption']['text'].'" width="'.$width.'" height="'.$height.'" /></a> '.$caption.PHP_EOL;}
This means: for all the items greater than 6 $result
is display:none
and blahblah, for all the others $result is images with caption.
What I'd like to add is: if counter is EQUAL to 4, print the "else" $result
with the ONLY change that instead of src="'.$value['images']['low_resolution']['url'].'"
, it would be src="'.$value['images']['thumb_resolution']['url'].'"
I think that this would work, but I'm looking for a cooler, one-liner way :D
if($i==6) {
$result .= '<a style="display:none" class="fancybox" data-fancybox-group="gallery" href="'.$value['images']['standard_resolution']['url'].'"></a> '.$caption.PHP_EOL;;}
elseif($i===4) {
$result .= "\t".'<a class="fancybox" data-fancybox-group="gallery" href="'.$value['images']['standard_resolution']['url'].'"><img id="thumb'.++$i.'" src="'.$value['images']['thumb_resolution']['url'].'" alt="'.$value['caption']['text'].'" width="'.$width.'" height="'.$height.'" /></a> '.$caption.PHP_EOL;}
else {
$result .= "\t".'<a class="fancybox" data-fancybox-group="gallery" href="'.$value['images']['standard_resolution']['url'].'"><img id="thumb'.++$i.'" src="'.$value['images']['low_resolution']['url'].'" alt="'.$value['caption']['text'].'" width="'.$width.'" height="'.$height.'" /></a> '.$caption.PHP_EOL;}
Look up the conditional operator, or ternary operator. It is used like this:
$check = true;
$var = $check ? 5 : 3;
The conditional operator checks the value of the expression before the ?
and outputs either the value before the :
if the expression is true or the value after the ':' if it is false. So the line above sets $var
to 5 because $check
is true. You could also have done this:
$check = 2354;
$var = ($check < 40) ? 5 : 3;
That would have given you the opposite result, $var
would have been set to 3, because $check
did not meet the condition $check < 40
. Think of it like a function which outputs one of two possible values based on the truth value of a given expression. You can embed that directly into the result .=
as:
($i === 4) ? $value['images']['low_resolution']['url'] : $value['images']['thumb_resolution']['url'];
For more information about the conditional operator, you can look at the PHP documentation page about it here. Note that the part about the conditional operator is a little way down the page.
Use the ternary operator of php. The simple syntax is
Condition ? (if-equivalent-statement) : (else-equivalent-statement)
However, when this condition is used inside another statement, you should enclose it within parentheses like:
(Condition ? (if-equivalent-statement) : (else-equivalent-statement))
So, the code should be like this:
if($i==6) {
$result .= '<a style="display:none" class="fancybox" data-fancybox-group="gallery" href="'.$value['images']['standard_resolution']['url'].'"></a> '.$caption.PHP_EOL;;}
else {
//I've broken down the line to make it easier to read
$result .= "\t".'<a class="fancybox" data-fancybox-group="gallery" href="'.$value['images']['standard_resolution']['url'].'"><img id="thumb'.++$i.'" src="'.
(($i==4) ? $value['images']['thumb_resolution']['url'] : $value['images']['low_resolution']['url'])
.'" alt="'.$value['caption']['text'].'" width="'.$width.'" height="'.$height.'" /></a> '.$caption.PHP_EOL;
}
What you might be thinking is a Ternary operators but it doesn't "really" support elseif
and can be hard to read.
Why not try a switch for more cooler but it's not one liner. :)
switch ($i){
case "6":
$result .= '<a style="display:none" class="fancybox" data-fancybox-group="gallery" href="'.$value['images']['standard_resolution']['url'].'"></a> '.$caption.PHP_EOL;
break;
case "4":
$result .= "\t".'<a class="fancybox" data-fancybox-group="gallery" href="'.$value['images']['standard_resolution']['url'].'"><img id="thumb'.++$i.'" src="'.$value['images']['thumb_resolution']['url'].'" alt="'.$value['caption']['text'].'" width="'.$width.'" height="'.$height.'" /></a> '.$caption.PHP_EOL;
break;
default:
$result .= "\t".'<a class="fancybox" data-fancybox-group="gallery" href="'.$value['images']['standard_resolution']['url'].'"><img id="thumb'.++$i.'" src="'.$value['images']['low_resolution']['url'].'" alt="'.$value['caption']['text'].'" width="'.$width.'" height="'.$height.'" /></a> '.$caption.PHP_EOL;
break;
}