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
Related
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
So I have this code I want to repeat. Uses wordpress acf to generate the image:
`<div class="row">
<?php if( get_field('image_1') ): ?>
<img class="this-image" src="<?php the_field('image_1'); ?>" />
<?php endif; ?>
</div>`
I'm just wondering how I can loop this in with which I would also need to increment the numbers (ie. image_1, image_2, image_3).
I'm having trouble figuring out the logic of PHP, if there already is a similar post, a kind link would be much appreciated, thanks!
Use below code - it is using for loop to iterate through all images from image_1 to image_X (where X can be defined by you). similar code can be modified by add an array to store all the image names, just in case if there is no patterns for the image names.
<div class="row">
<?php $numImages = 10; #Define how many images you have
for ($i=1; $i<= $numImages; $i++){ #starting from image_1 ?>
<?php if( get_field('image_' . $i)): ?>
<img class="this-image" src="<?php the_field('image_' . $i); ?>" />
<?php endif; ?>
<?php } ?>
</div>
You can try something like this
`<div class="row">
<?php
for (i=1; i<=3; i++) {
if( get_field('image_'.i) ): ?>
<img class="this-image" src="<?php the_field('image_'.i); ?>" />
<?php endif; } ?>
</div>`
I am looking to offset a group of PHP array results to create multiple rows of data.
For example, the first row will contain the first four array results, with the second row displaying results 5-8 and the third row containing results 9-12.
Currently, I am printing out all 12 in one list, however I'd like a bit more display control (i.e. ordering the results into columns which I can style independent of each), hence why I'd like to offset the results.
My current PHP is below:
<?php
if (empty($tmdb_cast['cast'])) {
} else {?>
<section class="cast">
<p class="title">Cast</p>
<ul class="section_body"><?php
foreach($tmdb_cast['cast'] as $key => $castMember){
if ($key < 12) {?>
<li class="actor_instance clearfix"><?php
if ($castMember['profile_path'] != '') {?>
<img src="http://cf2.imgobject.com/t/p/w45<?php echo $castMember['profile_path']; ?>" class="actor_image pull-left" alt="<?php echo $castMember['name']; ?>" title="<?php echo $castMember['name']; ?>" /><?php
} else {?>
<img src="assets/images/castpic_unavailable.png" class="actor_image pull-left" alt="No image available" title="No image available" /><?php
}?>
<p class="actor"><?php echo $castMember['character']; ?> - <?php echo $castMember['name']; ?></p>
</li><?php
}
}?>
<div class="morecast pull-right">[...view all cast]</div>
</ul>
</section><?php
}?>
P.S. Apologies for how I've formatted the code in the above block - I'm still not 100% sure how to make it look "nice" on StackOverflow.
Use array_chunk to break a single dimension array into a 2D array. Then you can loop through each chunk, and then each result, to get that "offset" effect between them.
$chunks = array_chunk($tmdb_cast['cast'], 4); // 4 here, is the number you want each group to have
foreach($chunks as $key => $chunk)
{
foreach($chunk as $castMember)
{
//display castMember here
}
// add code between groups of 4 cast members here
}
First: mixing php and html this way is a very bad habbit... one day you will have to maintain your code and that day you will vomit all over your desk because you mixed different languages in one file...
That being said..
and without creating a new array:
foreach($tmdb_cast['cast'] as $key => $castMember)
{
if ($key < 12)
{
// your code goes here
// if key is 3, or 7 the code below will close the listcontainer and open a new one...
if( ($key+1)%4 == 0 AND $key <11)
echo '</ul> <ul class="section_body">';
}
}
Also replace this:
if (empty($tmdb_cast['cast']))
{
}
else {
with this:
if (!empty($tmdb_cast['cast']))
{
I'm not entirely sure what you're looking for but here's how I would format your code. Alternative syntax for 'if' is a little more readable, and the use of for loops instead of a foreach will give you the control over row numbers you say you're looking for.
<?php if( ! empty($tmdb_cast['cast'])): ?>
<section class="cast">
<p class="title">Cast</p>
<ul class="section_body">
<?php
for($i = 0; $i < 12; $i++):
$castMember = $tmdb_cast['cast'][$i];
?>
<li class="actor_instance clearfix">
<?php if($castMember['profile_path'] != ''): ?>
<img src="http://cf2.imgobject.com/t/p/w45<?php echo $castMember['profile_path']; ?>" class="actor_image pull-left" alt="<?php echo $castMember['name']; ?>" title="<?php echo $castMember['name']; ?>" />
<?php else: ?>
<img src="assets/images/castpic_unavailable.png" class="actor_image pull-left" alt="No image available" title="No image available" />
<?php endif; ?>
<p class="actor"><?php echo $castMember['character']; ?> - <?php echo $castMember['name']; ?></p>
</li>
<?php endfor; ?>
<div class="morecast pull-right">[...view all cast]</div>
</ul>
</section>
<?php endif; ?>
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.
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