I am using a foreach to loop through image. There are maximum four images and minimum 1 image.
For example if there are two image (= two loops) i want to tell the foreach he needs to loop two times again and echo some placeholder pictures.
Heres my foreach:
<?php foreach($users as $k => $v) {?>
<img src="/images/user_<?php echo $k; ?>.jpg" alt="" title="" />
<?php } ?>
Outputs (two loops):
<img src="/images/user_0.jpg" alt="" title="" />
<img src="/images/user_1.jpg" alt="" title="" />
but the new script should output:
<img src="/images/user_0.jpg" alt="" title="" />
<img src="/images/user_1.jpg" alt="" title="" />
<img src="/images/user_placeholder.jpg" alt="" title="" />
<img src="/images/user_placeholder.jpg" alt="" title="" />
dont forget its possible that $users can have x entries (0-4)
Use array_fill to fill an array with as many items as needed (since they are all going to be identical) and then print them out.
<?php foreach($users as $k => $v) {?>
<img src="/images/user_<?php echo $k; ?>.jpg" alt="" title="" />
<?php } ?>
<?php
echo implode('', array_fill(0, count($users), 'placeholder image HTML'));
Of course instead of this cuteness you could also use another foreach that prints placeholder image HTML in each iteration.
Update: It turns out there's an even better method:
echo str_repeat('placeholder image HTML', count($users));
PHP really has too many functions to remember. :)
Use a counter...
<?php
$counter = 0;
foreach($users as $k => $v) {?>
<img src="/images/user_<?php echo $k; ?>.jpg" alt="" title="" />
<?php $counter++;
}
while{$counter < 4)
{?>
<img src="/images/user_placeholder.jpg" alt="" title="" />
<?php } ?>
this should work
$count = 1;
foreach($users as $k => $v) {
?>
<img src="/images/user_<?php echo $k; ?>.jpg" alt="" title="" />
<?php
$count++;
}
for ($i = $count; $i <= 4; $i++) {
?>
<img src="/images/user_placeholder.jpg" alt="" title="" />
<?php
}
?>
<?php
$placeholders = array();
foreach($users as $k => $v) {?>
<img src="/images/user_<?php echo $k; ?>.jpg" alt="" title="" />
<?php
$placeholders[] = '<img src="/images/user_placeholder.jpg" alt="" title="" />';
}
foreach ($placeholders as $placeholder){
echo $placeholder;
} ?>
As you can see, there are a dozen ways to skin this particular cat.
Related
My current code:
<?php if($_images){?>
<?php $i=0; foreach($_images as $_image){ $i++; ?>
<?php if($i > 2) { ?>
<div class = "largePic">
<img src="<?php echo $this->helper('catalog/image')->init($_product, 'thumbnail', $_image->getFile())->resize(600,900); ?>" alt="<?php echo $this->htmlEscape($_image->getLabel());?>" title="<?php $this->htmlEscape($_image->getLabel());?>" />
</div>
<?php } else { ?>
<div class = "smallPic">
<img src="<?php echo $this->helper('catalog/image')->init($_product, 'thumbnail', $_image->getFile())->resize(450,675); ?>" alt="<?php echo $this->htmlEscape($_image->getLabel());?>" title="<?php $this->htmlEscape($_image->getLabel());?>" />
</div>
<?php } ?>
<?php } ?>
<?php } ?>
So this is obviously wrong since every time an image is echo'ed it's assigned to a different div (same name but different). Is it possible to assign echoes to certain div depending on the count?
For example, first two images will be assigned to the smallPic div, then the rest will be in the largePic div.
Process the images first (storing the HTML for each image in either an array or string) and then create the div elements- see example below. This will reduce the number of opening/closing tags immensely.
Also note that if the keys of the array are numeric then the following syntax can be used instead of creating an extra variable (i.e. $i) just to track the index (so as to avoid extra bookkeeping like incrementing the variable - one of the major benefits of foreach compared to a for statement).
foreach (array_expression as $key => $value)
statement 1
<?php
$largePic = '';
$smallPic = '';
if($_images){
foreach($_images as $i => $_image){
if($i > 2) {
$largePic .= '<img src="'.$this->helper('catalog/image')->init($_product, 'thumbnail', $_image->getFile())->resize(600,900).'" alt="'. $this->htmlEscape($_image->getLabel()). '" title="'. $this->htmlEscape($_image->getLabel()).'" />';
} else {
$smallPic .= '<img src="'. $this->helper('catalog/image')->init($_product, 'thumbnail', $_image->getFile())->resize(450,675). '" alt="'. $this->htmlEscape($_image->getLabel()). '" title="'. $this->htmlEscape($_image->getLabel()). '" />';
}
}
} ?>
<div class = "largePic"><?php echo $largePic; ?></div>
<div class = "smallPic"><?php echo $smallPic; ?></div>
What you want is to first devide your array into two arrays and then foreach through both of them.
<?php
$largeImages = array();
$smallImages = array();
foreach ($_images as $k => $v) {
if ($k > 2) {
$largeImages[] = $v;
} else {
$smallImages[] = $v
}
}
?>
<div class = "largePic">
<?php foreach ($largeImages as $_image) { ?>
<img src="<?php echo $this->helper('catalog/image')->init($_product, 'thumbnail', $_image->getFile())->resize(600,900); ?>" alt="<?php echo $this->htmlEscape($_image->getLabel());?>" title="<?php $this->htmlEscape($_image->getLabel());?>" />
<?php } ?>
</div>
<div class = "smallPic">
<?php foreach ($smallImages as $_image) { ?>
<img src="<?php echo $this->helper('catalog/image')->init($_product, 'thumbnail', $_image->getFile())->resize(450,675); ?>" alt="<?php echo $this->htmlEscape($_image->getLabel());?>" title="<?php $this->htmlEscape($_image->getLabel());?>" />
<?php } ?>
</div>
I'm not in PHP but I'm quite sure for start you can loose those extensive open/close tags (<?php ?>) and replace it with only one at the start and the end. This is hardly readable.
Then, like in many server side scripts, what you need is some kind of buffer containing HTML which you will output at the end of loop. Because what you're doing now is outputting from inside the loop, this sending multiple DIVs to client.
Create largePic nad smallPic variables contianing DIVs HTML, and append HTML to it inside loop (instead outputing it to client like you do now). Then, after loop is finished, output those two variables to client.
I've written the following PHP statement but everytime i try to combine it into an else/if, it breaks.
Can someone please advise? I'm new to PHP and am getting a tad stuck.
Thanks :)
<?php if (is_page( 19 ) ) {?>
<div class="imageSlider"><img src="<?php echo bloginfo('template_directory');?>/Images/mainImages/innerPage-Image.jpg" alt="" /><img src="<?php echo bloginfo('template_directory');?>/Images/mainImages/innerPage-Image2.jpg" alt="" /><img src="<?php echo bloginfo('template_directory');?>/Images/mainImages/innerPage-Image3.jpg" alt="" /></div>
<?php }?>
<?php if (is_page( 23 ) ) {?>
<div class="imageSlider"><img src="<?php echo bloginfo('template_directory');?>/Images/mainImages/innerPage-Image.jpg" alt="" /><img src="<?php echo bloginfo('template_directory');?>/Images/mainImages/innerPage-Image.jpg" alt="" /><img src="<?php echo bloginfo('template_directory');?>/Images/mainImages/innerPage-Image.jpg" alt="" /></div>
}
<?php }?>
Use this syntax :
<?php if (condtion):?>
// html goes here,
<?php elseif(condtion): ?>
// html goes here,
<?php endif; ?>
<?php if (is_page( 19 ) ) {?>
<div class="imageSlider"><img src="<?php echo bloginfo('template_directory');?>/Images/mainImages/innerPage-Image.jpg" alt="" /><img src="<?php echo bloginfo('template_directory');?>/Images/mainImages/innerPage-Image2.jpg" alt="" /><img src="<?php echo bloginfo('template_directory');?>/Images/mainImages/innerPage-Image3.jpg" alt="" /></div>
<?php } elseif (is_page( 23 ) ) {?>
<div class="imageSlider"><img src="<?php echo bloginfo('template_directory');?>/Images/mainImages/innerPage-Image.jpg" alt="" /><img src="<?php echo bloginfo('template_directory');?>/Images/mainImages/innerPage-Image.jpg" alt="" /><img src="<?php echo bloginfo('template_directory');?>/Images/mainImages/innerPage-Image.jpg" alt="" /></div>
<?php }?>
Here is an sample if/else using your code
<?php if (is_page(19)) { ?>
//Insert html
<? { elseif (is_page(23)) } ?>
//Insert other html
<? } ?>
I want to create this loop
<li class="royalSlide">
<div class="celebFixSlider">
<img src="" />
<img src="" />
<img src="" />
<img src="" />
<img src="" />
<img src="" />
</div>
</li>
<li class="royalSlide">
<div class="celebFixSlider">
<img src="" />
<img src="" />
<img src="" />
<img src="" />
<img src="" />
<img src="" />
</div>
</li>
The LI and DIV elements are the first loop, the links are 6 row blocks per the Array.
$imagePath="//site.com/images/celeb1208/";
$productPath="//site.com/product.php?prodref=";
$array = array(
"632_white" => "celeb1208_317_large.jpg",
"631_white" => "celeb1208_316_large.jpg",
"630_white" => "celeb1208_315_large.jpg",
"629_white" => "celeb1208_314_large.jpg",
"628_white" => "celeb1208_313_large.jpg",
"627_white" => "celeb1208_312_large.jpg",
"532_white" => "celeb1208_311_large.jpg",
"531_white" => "celeb1208_310_large.jpg",
"530_white" => "celeb1208_309_large.jpg",
"529_white" => "celeb1208_308_large.jpg",
"528_white" => "celeb1208_307_large.jpg",
"527_white" => "celeb1208_306_large.jpg"
);
$i=0;
foreach ($array as $key => $val)
{
$i++;
echo '
<li class="royalSlide">
<div class="celebFixSlider">
<a href="'.$productPath.''.$key.'">
<img src="'.$val.'" />
</a>';
if($i==6){
echo '</div>
</li>';
break;
}
}
After 6 items, the break just stops the code, it does not pause and recreate the loop within side?? Sorry if I am not clear.
// getting data ready
foreach ($array as $key => $val) {
$array[$key] = array(
"href" => $productPath.$key,
"img" => $imagePath.$val
);
}
$data = array_chunk($array,2);
// printing it out
?>
<?php foreach ($data as $array): ?>
<li class="royalSlide">
<div class="celebFixSlider">
<?php foreach ($array as $row): ?>
<img src="<?=$row['img']?>" />
<?php endforeach ?>
</div>
</li>
<?php endforeach ?>
The break; command will stop the loop completly. Maybe you want continue; that will skip what is after the continue keyword and loop again from where it left off.
What you need is no break and no continue.:
$i=0;
echo '<li class="royalSlide"><div class="celebFixSlider">';
foreach ($array as $key => $val)
{
$i++;
echo '
<a href="'.$productPath.''.$key.'">
<img src="'.$val.'" />
</a>';
if($i==6){
echo '</div>
</li><li class="royalSlide"><div class="celebFixSlider">';
}
}
echo '</div></li>';
Use "continue" instead of "break";
Break will stop the loop
Thanks
So you should use this:
echo '<li class="royalSlide">
<div class="celebFixSlider">';
$i=0;
foreach ($array as $key => $val)
{
$i++;
echo '
<a href="'.$productPath.''.$key.'">
<img src="'.$val.'" />
</a>';
if($i==6){
echo '</div>
</li>
<li class="royalSlide">
<div class="celebFixSlider">';
}
}
Try this for your loop instead
$i=0;
foreach ($array as $key => $val)
{
$i++;
if(($i % 6) == 0){
echo '<li class="royalSlide"><div class="celebFixSlider">';
}
echo '<img src="'.$val.'" />';
if(($i % 6) == 0 && $i!=0){
echo '</div></li>';
}
}
I am using this code for a slideshow:
<img src="<?php echo($array[0]); ?>"/>
<img src="<?php echo($array[1]); ?>"/>
<img src="<?php echo($array[2]); ?>"/>
<img src="<?php echo($array[3]); ?>"/>
<img src="<?php echo($array[4]); ?>"/>
<img src="<?php echo($array[5]); ?>"/>
<img src="<?php echo($array[6]); ?>"/>
<img src="<?php echo($array[7]); ?>"/>
I want to show images, only if they exist.
Sometimes the $array has only 5 values.
How is this posible?
You should loop over the array values and echo an image tag for each value:
<?php
foreach($array as $img){
echo '<img src="'.$img.'"/>'."\n";
}
?>
That's the perfect opportunity for a loop. You can either use a for loop (since your array is numerically indexed) or a foreach loop.
Using a for loop:
<?php $count = count($array); for($i = 0; $i < $count; $i++): ?>
<img src="<?php echo($array[$i]); ?>" />
<?php endfor; ?>
In traditional syntax:
<?php $count = count($array); for($i = 0; $i < $count; $i++) { ?>
<img src="<?php echo($array[$i]); ?>" />
<?php } ?>
Using a foreach loop:
<?php foreach($array as $img): ?>
<img src="<?php echo $img; ?>" />
<?php endforeach; ?>
In traditional syntax:
<?php foreach($array as $img) { ?>
<img src="<?php echo $img; ?>" />
<?php } ?>
Since this is a fairly basic question, I suggest you take the time to read the PHP Documentation chapter about control structures. There are essential. It is available here:
PHP Documentation: Control Structures
foreach($array as $src){ echo "<img src='$src' />"; }
<?php
foreach($array as $img){
if(file_exists($img))
echo '<img src="'.$img.'"/>'."\n";
}
?>
for($i=0;$i<count($array);$i++)
{
?>
<img src="<?php echo($array[$i]); ?>"/>
<?php
}
<div id="prettyphoto" align="left"> <img src="http://images.idealer1.com/getimage/700/<?php echo $image['vpid'] ?>.jpg" width="100" alt="" />
<ul class="gallery clearfix">
<?php foreach ($images as $image) { ?>
<li><img src="http://images.idealer1.com/getimage/100/<?php echo $image['vpid'] ?>.jpg" width="100" alt="" /></li>
<? } ?>
</ul>
</div>
I am trying to take this foreach loop of thumbnails and exclude the first image and have it load as a larger image I can't seem to figure out the proper way to accomplish this.
You could use array_shift():
<?php $firstImage = array_shift($images); ?>
<!-- do something with $firstImage -->
<?php foreach ($images as $image): ?>
...
<?php endforeach; ?>
or if you don't need a reference to the first image, array_slice():
<?php foreach(array_slice($images, 1) as $image): ?>
...
<?php endforeach; ?>
Also note the use of the alternative syntax for control structures which makes reading the mixture of PHP and HTML a bit easier (but is not related to your problem).
You can iterate the array "manually", with next(), current(), etc:
<?php
$images = array(
array('vpid' => 1, ),
array('vpid' => 2, ),
array('vpid' => 3, ),
array('vpid' => 4, ),
);
$image = current($images);
?>
<div id="prettyphoto" align="left"> <img src="http://images.idealer1.com/getimage/700/<?php echo $image['vpid'] ?>.jpg" width="100" alt="" />
<ul class="gallery clearfix">
<?php
next($images);
while(list($idx, $image) = each($images)) { ?>
<li><img src="http://images.idealer1.com/getimage/100/<?php echo $image['vpid'] ?>.jpg" width="100" alt="" /></li>
<? } ?>
</ul>
</div>
Read: http://www.php.net/manual/en/function.each.php
With array_shift() you could "loose" the first image (you have to put it back in after you're done).
With array_slice() you would duplicate data, which is bad practice in general.
you can use some idea from this:
foreach($array as $key=>$value)
{
if($key==0)
echo "code for the large image";
else
echo "code for the thumbnail image";
}