Skipping For Loop - php

After having a solution to this question.
my code:-
<?php for ($i = 1; $i <= 9; $i++):?>
<div class="col-md-2">
<div class="thumbnail">
<a href="<?php echo $details . $i;?>.php">
<img src="<?php echo $images . $i;?>.png" alt="<?php echo $i;?>">
</a>
</div>
</div>
<?php endfor; ?>
So now assume that there is now image as 8.png, so for loop should skip that and go to next, without showing no image.
Result is.
And I want this
The red Portion should not be displayed.

Since you stated in the comments that the URL to the images is localhost/downloads/vf/images, I will assume that with usage of $_SERVER['DOCUMENT_ROOT'] takes care of the folders outside public (which could be something like C:\users\chirag\htdocs\, but that $_SERVER variable should take care of that. If that's not the case, find the full path and use that instead - as file_exists() requires the system path, not the public path/URL. This also assumes that you use a relative path for the $images variable, which means that you have $images = "/downloads/vf/images/"; and not $images = "localhost/downloads/vf/images/";.
With that assumption, we can now use file_exists() - because that takes the system path, not the URL.
<?php
$images = "/downloads/vf/images/";
for ($i = 1; $i <= 9; $i++):
if (!file_exists($_SERVER['DOCUMENT_ROOT'].$images.$i.".png"))
continue; // Skip this iteration if it can't find the file
?>
<div class="col-md-2">
<div class="thumbnail">
<a href="<?php echo $details . $i;?>.php">
<img src="<?php echo $images . $i;?>.png" alt="<?php echo $i;?>">
</a>
</div>
</div>
<?php endfor; ?>
PHP.net on continue;
PHP.net on $_SERVER
PHP.net on file_exists()

You will have to add a check condition if the image exists or not, there are certain methods like file_exists
<?php for ($i = 1; $i <= 9; $i++):
if(file_exists("file_path")){ ?>
<img src="<?php echo $images . $i;?>.png" alt="<?php echo $i;?>">
<? { php endfor; ?>
This might help you: How to check if image exists

Use this code
File_exists is function check for the file exists or not
<?php for ($i = 1; $i <= 9; $i++):
if (file_exists($images.$i.'.png')) { ?>
<img src="<?php echo $images . $i;?>.png" alt="<?php echo $i;?>">
<?php } endfor; ?>
check this like for more detail
PHP: How to check if image file exists?

You need to use the continue directive. I also suggest you drop the use of : and endfor and all the extra PHP start/end blocks for readability and performance reasons.
<?php
for ($i = 1; $i <= 9; $i++) {
if (!is_readable($images . $i . '.png'))
continue;
echo "<img src=\"${images}${i}.png\" alt=\"$i\">";
}
?>

If you use the keyword continue inside any loop, the rest of the iteration after that would be skipped.
<?php for ($i = 1; $i <= 9; $i++): ?>
<?php if ($i == 8) continue;>
<img src="<?php echo $images . $i;?>.png" alt="<?php echo $i;?>">
<?php endfor; ?>
This keyword is common for a lot of languages.
Also in PHP, the continue accepts a numeric argument which tells it how many iterations it should skip.
PHP continue

Related

Use PHP to increment number for image ID

So, I have a problem in that I have 67 (and counting) images all named IMAGE01.jpg, IMAGE02.jpg all the way to IMAGE67.jpg.
In my HTML page, I have this code to display the image.
<article class="thumb">
<img src="images/thumbs/image01.jpg" alt="" />
</article>
So, I have this for 67 images. Is there a way of incrementing the image number using PHP? So I can just copy and paste that piece of code for the number of images I have?
That's the first part that would be GREAT to have a solution too. But, would there be a way to not have to repeat that code for every image? Would it be possible to use PHP to define the number of times I want this to be repeated?
So have the snippet of code once and have something to repeat it a defined number of times so as I add to the collection, all I have to do is change the repeat times?
Many thanks in advance for the help. Means allot.
<?php
for($i=1; $i<=67; $i++) {
?>
<article class="thumb">
<a href="images/fulls/image<?php
if($i<9)
echo "0".$i;
else
echo $i;
?>.jpg" class="image"><img src="images/thumbs/image<?php
if($i<9)
echo "0".$i;
else
echo $i;
?>.jpg" alt="" /></a>
</article>
<?php
}
?>
To make what you want you need to use a for loop from 1 to 67 (because your image name are from 1 to 67). And for each iteration you have to echo the html string.
Like this for example :
for($i = 1; $i<=67; $i++){
$name = $i<10?"0".$i:$i;
echo "<article class='thumb'>
<a href='images/fulls/image".$name.".jpg' class='image'>
<img src='images/thumbs/image".$name.".jpg' alt=''/>
</a>
</article>";
}
You need to construct the name if the name need to have 0 before (01, 02 ...). It's what I make here :$name = $i<10?"0".$i:$i;. It's mean that if $i < 10 then we add a "0" before else the name is $i.
You can also use a while loop :
$i = 1;
while($i<=67){
$name = $i<10?"0".$i:$i;
echo "<article class='thumb'>
<a href='images/fulls/image".$name.".jpg' class='image'>
<img src='images/thumbs/image".$name.".jpg' alt=''/>
</a>
</article>";
$i++;
}
It's the same idea.
Hope that helps you.
You can use a PHP variable and inject it into your HTML. The following code will count from 1 - 67.
<?php
$count = 1;
while($count <= 67){
?>
<p>Count: <?php echo $count; ?></p>
<?php
$count +=1;
}
?>
So you could use it as such:
<article class="thumb">
<?php
$count = 1;
while($count <= $numOfImages){
?>
<img src="images/thumbs/image<?php echo $count; ?>.jpg" alt="" />
<?php
$count +=1;
}
?>
</article>
Just set $numOfImages to however many images there are and it should work.
You can try this sample code below if images are one after the other. Using single for loop with printf inside echo...
<body>
<?php
for ($i = 0; $i < 67; $i++) {
?>
<article class="thumb">
<a href="images/full/image<?php printf("%02d\n", $i)?>.jpg" ><img src="images/thumb/image<?php printf("%02d\n", $i)?>.png" alt="" /></a>
</article>
<?php
}
?>
</body>
Here is the sample output I kept only one image for testing. http://i.stack.imgur.com/l1Lo5.png

How can I getMediaGalleryImages within a sort order range in Magento?

I am new to Magento.
How can I getMediaGalleryImages within a sort order range in Magento? I am new to magento and would like to get all images for a product within a sort oorder range of say 10-20.
This is what I have so far:
<?php $_images = Mage::getModel('catalog/product')->load($_product->getId())- >getMediaGalleryImages();?>();?><?php if($_images){?><?php $i=0; foreach($_images as $_image) if ($i++ < 8) { $i++;?>
Any help is welcome!
Thanks for the answers but, I'm not quite getting it I'm afraid, I want to display thumbs under my products on the product list page. I'm sure the code you have given is correct but I am not implementing it correctly. Here's the full code for that I have at the moment:
<?php $_images = Mage::getModel('catalog/product')->load($_product->getId())- >getMediaGalleryImages();?>
<?php if($_images){?>
<?php $i=0; foreach($_images as $_image){ $i++; if($i>=10 && $i<=20);?>
<a href="#">
<img src="<?php echo $helpImg->getImg($_product, $bigImageWidth, $bigImageHeight, 'image', $_image->getFile()); ?>" class="cloud-zoom-gallery lightbox-group"
title="<?php echo $this->escapeHtml($_image->getLabel()); ?>" width="25" height="25" alt="<? =$this->htmlEscape($_image->getLabel());?>" title="<?=$this->htmlEscape($_image->getLabel());?>" />
you need to change code
$_images = Mage::getModel('catalog/product')->load($_product->getId())->getMediaGalleryImages();
$i=1;
foreach($_images as $_image){
if($i > 10 && $i < 20){
echo $_image['url'];
}
$i++;
}
You can do it by doing following code.
<?php $_images = Mage::getModel('catalog/product')->load($_product->getId())->getMediaGalleryImages(); ?>
<?php if($_images){?>
<?php $i=0; foreach($_images as $_image){ $i++; if($i>=10 && $i<=20): ?>
<img src="<?php echo $this->helper('catalog/image')->init($_product, 'thumbnail', $_image->getFile())->resize(108,90); ?>" alt="<?php echo $this->htmlEscape($_image->getLabel());?>" title="<?php $this->htmlEscape($_image->getLabel());?>" /><?php endif; } ?>
<?php } ?>
Change the range whatever you want.
This is example for product gallery images order from 10 to 20.

How to stop for loop from looping infinitely?

In my version of Bootstrap there are 12 columns in a row.
In the below for loop each loop prints 2 columns wide.
What I want is that before the first column is printed (or $i === 0)<div class="row"> is printed.
Then after the number of columns is equal to 12 (or the variable $i is no longer less than 6) for the closing row tag to be printed (</div><!--row-->) and then for the variable $i to be reset to zero.
I have achieved the layout I desire but the problem is that Wordpress is looping infinitely and retrieving the logo of the same company hundreds of times.
<?php if (have_posts() ) : while (have_posts() ) : the_post(); ?>
<?php for ($i = 0; $i < 6; $i++) : ?>
<?php if ($i === 0) : ?>
<div class="row">
<?php endif; ?>
<div class="col-md-2">
<?php $image = get_field('sponsor_logo'); ?>
<a href="<?php the_permalink();?>">
<img class="img-responsive" src="<?php echo $image['sizes']['thumbnail-soft-crop'];?>" alt="<?php echo $image['alt']; ?>"/>
</a>
</div><!--col-md-2-->
<?php if ($i === 5) : ?>
</div><!--row-->
<?php $i = 0;
endif;
endfor; ?>
<?php endwhile; else : ?>
You are redeclaring $i = 0; at the bottom so when the loop reaches bottom after increment it is again reset to 0 and hence running again and again.
If i am right i think you want to start a new row after number of iterations you can try this with some modification according to your requirement
<?php if (have_posts()) : while (have_posts()) : the_post();
$i = 0;
if ($i!=0 && $i%3==0){ // add new row after 3 posts
echo "</div><div class='row'>";
// close previous row after 3 elements and start new.
}
$i++;
?>
Solution for Query in question.
<div class="row">
<?php if (have_posts()) : while (have_posts()) : the_post();
$i = 0;
if ($i != 0 && $i % 5 == 0) { // add new row after 5 posts
echo "</div><div class='row'>";
}
$i++;
?>
<div class="col-md-2">
<?php $image = get_field('sponsor_logo'); ?>
<a href="<?php the_permalink(); ?>">
<img class="img-responsive" src="<?php echo $image['sizes']['thumbnail-soft-crop']; ?>"
alt="<?php echo $image['alt']; ?>"/>
</a>
</div>
<?php endwhile; endif; ?>
</div>

show only three records and then move to next line

This is a very basic problem but I am very new to PHP.
I need to show results in such scenario that only three records in same line then add <br /> and then on next line, same thing should happend. I am unable to make its logic and in a great trouble :(
Right now, I am just using the simple way i.e.
while($data = mysql_fetch_array($res_set)) {
?>
<div><?php echo $data['name']?><img src="images/<?php echo $data['image']?>" /></div>
<?php
}
this is my code
$cnt = 0;
while ($prd = mysql_fetch_array($res)) {
?>
<div class="imageRow">
<?php
$cat_id = $prd['cat_id'];
$sql = "select * from tbl_category where id = $cat_id";
$cat_res = mysql_query($sql);
$cat_data = mysql_fetch_array($cat_res);
$cat_name = $cat_data['name'];
?>
<div class="set">
<div class="single first">
<img src="<?php echo $ru ?>admin/product_images/<?php echo $cat_name ?>/large/<?php echo $prd['thumb_img'] ?>" style="height: 100px; width: 100px;" /><br />
Choose
</div>
</div>
</div>
<?php
$cnt = $cnt++;
if($cnt%3 == 0) {echo "<br />";}
}
//echo $cnt;
?>
any help will be appreciated.
Thanks
<div class="imageRow">
<?php
$i = 0;
while($data = mysql_fetch_array($res_set)) { ?>
<div>
<?php echo $data['name']; ?><img src="images/<?php echo $data['image']; ?>" />
</div>
<?php
$i++;
if($i % 3 == 0) {
echo '</div><div class="imageRow"><br />';
}
}
?>
</div>
Explanation:
Set the variable $i to a number, which will then be used to keep track of how many items you've written.
Then, within the while loop, increment $i ($i++), which is the same as $i = $i + 1; By doing this, you always know which item you're on - whatever the value of $i is. Some people choose to set it to 1 initially then increment at the very end - other like to set it to 0, and set it near the beginning - either is completely fine - whatever you need it to do / whichever way you like better.
Lastly, check if $i is evenly divisible by 3 (kind of like remainder - it's called "mod" and is represented by the percent symbol). If it is, then echo a line break.
Try this
<?php
echo "<div>";
while($data = mysql_fetch_attay($res_set)) {
?>
<div style="width:30%; float:left"><?php echo $data['name]?><img src="images/<?php echo $data['image']?>" /></div>
<?php
}
echo "</div>";
?>
Output
<div>
<div>1</div> <div>2</div> <div>3</div>
<div>1</div> <div>2</div> <div>3</div>
<div>1</div> <div>2</div> <div>3</div>
</div>
That's some super ugly code you have there. This will do what you need, and is cleaner.
<?php
$i=1;
while($data = mysql_fetch_attay($res_set)) {
$frame = '<div><img src="%s" /></div>';
printf($frame, $data['image']);
if($i % 3 == 0) { echo '<br />'; }
$i++;
}
Also, <img> elements are already block elements to begin with, you really don't need to wrap another <div> around them.

breaking output of php array into chunks

I have an array containing information about some images, which I am using to print a number of img html tags by a foreach loop as follows:
<?php foreach ( $images as $image ) : ?>
<img alt="<?php echo $image->alttext ?>" src="<?php echo $image->thumbnailURL ?>" />
<?php endforeach; ?>
I would like to wrap a div around every 10 images. The remainder should get a div as well.
I am thinking I need to use array_chunkon $images and wrap the above in another loop for each chunk. The little bit of math I did to start is as follows:
$pics_per_page = 10;
$imgcount = count($images);
$num_pages = ceil($imgcount/$pics_per_page);
$pages = array_chunk($images, $num_pages);
How do I proceed from here? How do I use the $images array correctly, if at all, in outputting my HTML?
Forgetting about the array_chunk method, I have the following way, but the array_chunk method seems cleaner.
for i from 1 to num_pages:
echo "<div>"
j = (i-1) * pics_per_page;
while j <= i * pics_per_page
echo "<img src = "$images[j]->thumbnailURL />";
endwhile;
echo "</div>"
Thanks for your help in advance,
Sepehr
You are using array_chunk [docs] wrongly. You just have to specify the size of each chunk:
$chunks = array_chunk($images, 10);
Then you need a nested for (foreach) loop:
<?php foreach($chunks as $chunk): ?>
<div class="someClass">
<?php foreach($chunk as $image): ?>
<img alt="<?php echo $image->alttext ?>" src="<?php echo $image->thumbnailURL ?>" />
<?php endforeach; ?>
</div>
<?php endforeach; ?>
You could use it without array_chunk too.
<?php
$imgTot = count($images);
echo '<div>';
for($i=0; $i<$imgTot; $i++)
{
if ($i % 10 == 0) {
echo '<div>';
}
echo '<img alt="'.$image->alttext.'" src="'.$image->thumbnailURL.'" />';
if ($i % 10 == 9) {
echo '</div>';
}
}
echo '</div>';
?>
See the working example here: http://codepad.org/FkdTEH91

Categories