I've got a bunch of images in a folder called img and am trying to do a foreach loop to loop over them and output them to the screen, but then adding a unique class to each image.
Here's what I have so far but it says $i is undefined, so I'm not sure I'm on the right track. I'm very new to this.
$dir = "img/gallery/*.jpg";
$classes = array('promotional', 'beaches', 'volunteers');
$images = glob( $dir );
foreach ( $images as $image ):
$class = $classes[$i++ % 3];
echo '
<div class="col-sm-6 col-md-4 col-lg-4 col-xl-4 mix ' . $class . '">
<div class="portfolio-item">
<div class="shot-item">
<a class="overlay lightbox" href="' . $image . '">;
<img src="' . $image . '" />;
<i class="lnr lnr-plus-circle item-icon"></i>
</a>
</div>
</div>
</div>
';
endforeach;
Thanks for any help!
I would modify the foreach a little bit like so:
$dir = "img/gallery/*.jpg";
$classes = array('promotional', 'beaches', 'volunteers');
$images = glob( $dir );
$i = 0;
foreach ( $images as $x => $image ):
$class = $classes[$i++ % 6];
echo '
<div class="col-sm-6 col-md-4 col-lg-4 col-xl-4 mix ' . $class . '-' . $x . '">
<div class="portfolio-item">
<div class="shot-item">
<a class="overlay lightbox" href="' . $image . '">;
<img src="' . $image . '" />;
<i class="lnr lnr-plus-circle item-icon"></i>
</a>
</div>
</div>
</div>
';
endforeach;
I think best way would be using two class rather than one unique.
First class will be use to detect item/content type,
and second will be unique to identify uniquely.
By doing this it will give you more control over content.
Here is little bit modification.
$dir = "img/gallery/*.jpg";
$classes = array('promotional', 'beaches', 'volunteers');
$images = glob( $dir );
foreach ( $images as $index => $image ):
$class = $classes[$i++ % 6];
echo "
<div class="col-sm-6 col-md-4 col-lg-4 col-xl-4 mix my-list-item-{$class} image-id-{$index}">
<div class="portfolio-item">
<div class="shot-item">
<a class="overlay lightbox" href="' . $image . '">;
<img src="' . $image . '" />;
<i class="lnr lnr-plus-circle item-icon"></i>
</a>
</div>
</div>
</div>
";
endforeach;
What changes:
Changes single quote(') to double quote(") to be able to parse variable inside string, like this {$class} or simply like this $class
added class to identify type of item, my-list-item-{$class} : will result like this this my-list-item-promotional
added class to identify as a unique item, image-id-{$index} : will result like this image-id-5
Use in css or js
CSS:
.my-list-item-promotional{background: green;}
.my-list-item-promotional.image-id-2{background: yellow;}
This will provide all promotional type content background green except item number 2
You can use jQuery to select element like CSS does.
Thank you.
You don't need to define your own $i; it already exists as the index of the $images array.
After you do $images = glob( $dir );, $images will be a numerically indexed array. So instead of explicitly incrementing a counter variable like this:
foreach ( $images as $image ):
$class = $classes[$i++ % 3];
You can just use the index from the array like this:
foreach ( $images as $i => image ):
$class = $classes[$i % 3];
Related
I have code for a random image in the php section of my page
$imagesDir = 'images/eggs/';
$images = glob($imagesDir . '*.{jpg,jpeg,png,gif}', GLOB_BRACE);
$randomImage = $images[array_rand($images)];
it will display an image with this code
echo "<img src='$randomImage'>";
but when i try to do this in the body section of the page like this
<body>';
}
function template_body_above()
{
global $context, $settings, $options, $scripturl, $txt, $modSettings;
echo !empty($settings['forum_width']) ? '
<div id="wrapper" style="width: ' . $settings['forum_width'] . '">' : '', '
<div class="forest">
<div id="egg1"></div>
</div>
<div id="header"><div class="frame">
<div id="baloon1">
<img src="egg.php"/> //tried including seperate php code
<img src="<?php echo $randomImage ?>"> //php on same page
</div>
I am showing for the image source ...%3C?php...
Any help on where I'm going wrong ?
Try this way:
function template_body_above()
{
global $context, $settings, $options, $scripturl, $txt, $modSettings;
echo ( (!empty($settings['forum_width'])) ? '<div id="wrapper" style="width: ' . $settings['forum_width'] . '">' : '') .
'
<div class="forest">
<div id="egg1"></div>
</div>
<div id="header"><div class="frame">
<div id="baloon1">
<img src="' . $randomImage . '">
</div>
Is there any way I can use an unique variable for every time the code runs a foreach loop? Maybe there is an other way to fix this. If I have more than one project in the directory it will count both of the projects.
Everytime I use the carousel the counter will count through all of the directories. It need to be unique for every project.
Here is the code:
<?php
$directory = "./public/img/portfolio/". $row['naam_project'] ."/";
$images = glob($directory . "*.jpg");
$i = 0;
foreach($images as $image)
{
?>
<div class="mySlides fade">
<img src="<?php echo $image ?>" style="width:100%">
</div>
<?php
}
?>
<a class="prev" onclick="plusSlides(-1)">❮</a>
<a class="next" onclick="plusSlides(1)">❯</a>
</div>
<br>
<div style="text-align:center">
<?php
foreach($images as $image)
{
$i++;
?>
<span class="dot" onclick="currentSlide(<?php echo $i; ?>)"></span>
<?php
}
?>
use foreach key value key is unique for array
{foreach($images as $key->$value)
{
?>
<span class="dot" onclick="currentSlide(<?php echo $key; ?>)"></span>
<?php
}}
I have this code inside a php file:
<?php
function convertToReadableSize($size){
$base = log($size) / log(1024);
$suffix = array("", "KB", "MB", "GB", "TB");
$f_base = floor($base);
return round(pow(1024, $base - floor($base)), 1) . $suffix[$f_base];
}
$file_path = 'online_assets/images/';
?>
With a repeating loop displaying every entry:
<?php
$fileSystemIterator = new FilesystemIterator('online_assets/images/');
foreach ($fileSystemIterator as $fileInfo){
$entries[] = $fileInfo->getFilename();
?>
<div class="row">
<div class="col-md-7">
<a href="<?php echo $file_path;?>Room.jpg" target="new">
<img src="/slir/w320-h180-c320x180/<?php echo $file_path;?>Room.jpg" class="img-responsive"/></a>
</div>
<div class="col-md-5">
<h4 class="red"><?php $filename;?></h4>
<p>Filesize: <?php echo convertToReadableSize(filesize($cannery_file_path . 'Room.jpg'));?><br>Format: jpg<br>Quality: <?php list($width, $height) = getimagesize($file_path . 'Room.jpg'); echo ($width) . " x " . ($height); ?></p>
<a href="<?php echo $file_path;?>Room.jpg" target="new">
<button type="button" class="btn btn-primary btn-sm">Download Here</button></a>
</div>
</div>
<?php } ?>
What I am trying to accomplish here is a loop that will return a value essentially foreach image inside the /images/ folder - so all the client has to do is upload images to that folder and they appear, with a thumbnail, file information and a link to download.
I have the rest working, but the loop is giving me some troubles. What am I missing for this?
I picked the code for the FileSystemIterator from the php online manual.
I am editing my category page. Using some custom fields I am defining an image. For each post within a category I want to add this custom image to an array which I am turning into a gallery of images. I'm using the below code, but for some reason when it comes to imploding the array all I get back is one image (which corresponds to the last post that's loaded in). I'm sure there is probably just something I've put in the wrong place but I just can't figure it out. Any help would be much appreciated.
<?php if ( have_posts() ) : while ( have_posts() ) : the_post();
$gallery_images = get_custom_field('catImage:to_array');
$thumbs_html = array();
foreach ($gallery_images as $galleryID) {
$attachment = get_post( $galleryID );
$description = get_custom_field('catIntro:do_shortcode'); //get pages introductory text
$caption = get_the_title(); //get page title
$button_html .= '<div id="description-button-' . $gallery_images_count . '" class="show-description-button">Show Caption</div>';
$description_html .= '<div id="description-' . $gallery_images_count . '" class="photoDescription" style="display:none;">' . $description . '</div>';
$caption_html .= '<div id="caption-' . $gallery_images_count . '" class="photoCaption" style="display:none;">' . $caption . '</div>';
$thumb_img = wp_get_attachment_image_src( $galleryID, 'thumbnail' ); //thumbnail src
$full_img = wp_get_attachment_image_src( $galleryID, 'full' ); //full img src
$thumbs_html[] = '<div class="indvlThumbCont"><img class="thumbImg" src="' . $thumb_img[0] .'"></div>';
$gallery_images_count++;
}//end forEach
//calculate the width of the thumbar
$widthPerImg = 157;
$thumbBarWidth = $gallery_images_count * $widthPerImg;
print $gallery_images_count;
?>
<?php endwhile; else: ?>
<p><?php _e('Sorry, no posts matched your criteria.'); ?></p>
<?php endif; ?>
<div id="thumbsBar">
<div id="left" class="scrollCtrl left desktopOnly"><span></span></div>
<div class="toHideScrollBar" id="toHideScrollBar">
<div class="moveCanvas" style="width:<?php echo $thumbBarWidth; ?>px;">
<?php echo implode("\n", $thumbs_html); ?>
</div>
</div>
<div id="right" class="scrollCtrl right desktopOnly"><span></span></div>
<span id="total_number_in_gallery " class="<?php echo $gallery_images_count; ?>"></span>
</div>
If you are using a theme like TwentyTwelve (which by default only displays one post on the category page) then that's where the issue is. You'll solve this by (if you are fine with modifying the main loop), adding this just before your code:
query_posts('showposts=y&cat=x');
This is my first post, so I will be trying to be as thorough as possible. I am also very new to PHP.
This is a wordpress site using PODS CMS plugin so keep in mind the wordpress image uploader allows access to multiple sizes of one singular image upload. The concept is that I have a group of data called "team" and this group has three fields - images, title, bio. I will be generating a list of thumbnails for each team member in one containing unordered list and then in another containing div I will have a larger version of he image, the title, and the bio. I am basically making a tabbed content area where the thumbnails are the tabs
The ultimate HTML output would be:
<ul>
<li> <img src="thumbnailurl.jpg"/></li>
<li> <img src="thumbnailurl2.jpg"/></li>
<li> <img src="thumbnailurl3.jpg"/></li>
</ul>
<div class="panes">
<div><h2>Title 1</h2> <p> BIO CONTENT </p></div>
<div><h2>Title 1</h2> <p> BIO CONTENT </p></div>
<div><h2>Title 1</h2> <p> BIO CONTENT </p></div>
</div>
The current issue I am having is that I can get all of the image urls for the first record, but when it comes to the second record in the second foreach i need to re-run the array for the new record but I can not figure out how.
<?php
$Record = new Pod('the_team');
$Record->findRecords($orderby = 't.id DESC');
$mylist=array();
while ($Record->fetchRecord())
{
$image_array = $Record->get_field('photo');
$title = $Record->get_field('name');
$desc = $Record->get_field('bio');
$id = $Record->get_field('id');
$mylist[] = array('name' => $title, 'desc' => $desc, 'id'=> $id );
?>
<ul>
<?php
foreach($image_array as $i => $image)
{
$image_thumb_url = wp_get_attachment_image_src( $image['ID'], 'thumbnail', false );
$image_thumb_url = $image_thumb_url[0];
$image_med_url = wp_get_attachment_image_src( $image['ID'], 'medium', false );
$image_med_url = $image_med_url[0];
$image_large_url = wp_get_attachment_image_src( $image['ID'], 'large', false );
$image_large_url = $image_large_url[0];
$image_full_url = wp_get_attachment_image_src( $image['ID'], 'full', false );
$image_full_url = $image_full_url[0];
?>
<li>
<a href="<?php echo $image_large_url; ?>">
<img src="<?php echo $image_thumb_url; ?>" />
</a>
</li>
<?php } ?>
<?php } ?>
</ul>
<div class="panes">
<?php
foreach ($mylist as $person)
{ ?>
<div class="team-member" id="member<?php echo $person['id']; ?>">
<h2><?php echo $person['name']; ?></h2>
<?php echo $person['desc']; ?>
<a href="<?php echo $person['photo']; ?>">
<img src="<?php echo $person['photo']; ?>" />
</a>
<?php } ?>
</div>
</div>
Okkkay.. So i have the first problem solved!!! But it brings up a second one. I am thinking I will either need a second image field OR call just the first image in the array in the <li> and just the second image in the array for the <div>:
<?php
$Record = new Pod('the_team');
$Record->findRecords($orderby = 't.id DESC');
$mylist=array();
$cnt = 0;
?>
<ul class="tabs">
<?php
while ($Record->fetchRecord()) :
$mylist[$cnt] = array(
'name' => $Record->get_field('name'),
'desc' => $Record->get_field('bio'),
'id'=> $Record->get_field('id')
);
?>
<?php
$image_array = $Record->get_field('photo');
foreach($image_array as $i => $image) :
$image_thumb_url = wp_get_attachment_image_src( $image['ID'], 'thumbnail', false );
$mylist[$cnt]['img_thumb'] = $image_thumb_url[0];
$image_med_url = wp_get_attachment_image_src( $image['ID'], 'medium', false );
$mylist[$cnt]['img_med'] = $image_med_url[0];
$image_large_url = wp_get_attachment_image_src( $image['ID'], 'large', false );
$mylist[$cnt]['img_large'] = $image_large_url[0];
$image_full_url = wp_get_attachment_image_src( $image['ID'], 'full', false );
$mylist[$cnt]['img_full'] = $image_full_url[0];
?>
<li>
<a href="#">
<img src="<?php echo $image_thumb_url[0]; ?>" />
</a>
</li>
<?php endforeach; ?>
<?php
$cnt++;
endwhile;
?> </ul>
<div class="panes">
<?php foreach ($mylist as $person) : ?>
<div class="team-member" id="member<?php echo $person['id']; ?>"><div id="member-info"><h2>Meet <?php echo $person['name']; ?></h2>
<?php echo $person['desc']; ?></div>
<a href="<?php echo $person['img_large']; ?>" rel="prettyPhoto">
<img src="<?php echo $person['img_med']; ?>" style="float:left" />
</a>
</div>
<?php endforeach; ?>
</div>
I think I can see what your problem is. Your second foreach loop doesn't benefit from the while loop, as such your only option if following this path would be to reloop through the fetched data. I wouldn't recommend doing this as there is nearly always a way you can write the code needed for the second loop into the original loop.
As an example of this I've re-written your code to incorporate the second foreach loop into the while loop (negating its need). This way, every record has a new entry into $html and $html2. Once it's looped through you have two variables that you can concat to produce the full html you are looking for.
<?php
$Record = new Pod('the_team');
$Record->findRecords($orderby = 't.id DESC');
// Custom variables
$html = "<ul>\n";
$html2 = "";
$image_type=array('thumbnail','medium','large','full');
while ($Record->fetchRecord()) {
$image_array = $Record->get_field('photo');
$title = $Record->get_field('name');
$desc = $Record->get_field('bio');
$id = $Record->get_field('id');
foreach($image_array as $i => $image) {
foreach ($image_type as $type) {
$image_temp = wp_get_attachment_image_src( $image['ID'], $type , false );
$image_url[$type] = $image_temp[0];
} # End of Foreach loop
// Create List HTML in primary output variable
$html .= "<li>\n".
"<a href=\"" . $image_url['large'] ."\">\n".
"<img src=\"" . $image_url['thumbnail'] ."\" />\n".
"</a>\n".
"</li>\n";
} #End of foreach loop
// Create User HTML in secondary output variable
$html2 .= "\t<div class=\"team-member\" id=\"member" . $id . ">\n".
"<h2>" . $title . "</h2>\n".
$desc . "\n".
"<a href=\"" . $photo . ">\n". # $photo is not declared in this code (will error)
"<img src=\"" . $photo . " />\n". # as above comment
"</a>\n";
"</div>\n";
} # End of while loop
// Wrap up the list elements, concat the secondary HTML.
$html .= "</ul>\n\n".
"<div class=\"panes\">\n".
$html2 . "\n".
"</div>\n";
echo $html;
?>