I have a foreach loop giving out the results of a landscape gallery images.
Basically starting with
<?php foreach ( $images as $image ) : ?>
and then the regular div + anchor + image tags for the gallery images.
<?php if ( $image->description == "portrait" ) : ?>
<div id="ngg-image-<?php echo $image->pid ?>" class="ngg-gallery-thumbnail-box portrait" <?php echo $image->style ?> >
<div class="ngg-gallery-thumbnail" >
<a class="portrait-image" href="<?php echo $image->imageURL ?>" title=" " id="<?php echo $image->alttext ?>" rel="lightbox" <?php echo $image->thumbcode ?> >
<img title="<?php echo ' ' /* $image->alttext */ ?>" alt="<?php echo ' ' /* $image->alttext */ ?>" src="<?php echo $image->thumbnailURL ?>" <?php echo $image->size ?> />
</a>
<?php else: ?>
<div id="ngg-image-<?php echo $image->pid ?>" class="ngg-gallery-thumbnail-box landscape" <?php echo $image->style ?> >
<div class="ngg-gallery-thumbnail" >
<a class="landscape-image" href="<?php echo $image->imageURL ?>" title=" " id="<?php echo $image->alttext ?>" rel="lightbox" <?php echo $image->thumbcode ?> >
<img title="<?php echo ' ' /* $image->alttext */ ?>" alt="<?php echo ' ' /* $image->alttext */ ?>" src="<?php echo $image->thumbnailURL ?>" <?php echo $image->size ?> />
</a>
<?php endif; ?>
I want to have special case, where I have two portrait images side-by-side, filling the space of one landscape image.
What I was thinking about is to have a tag in the admin area which would trigger the break out of the foreach loop to have two images in one container div, and then it would continue the regular loop just with the container + image for the landscape images.
Is it possible in the case of foreach loop, to jump out of the loop, make a case with different settings, and then return to the regular looping method?
You can try something like this:
foreach($images as $key => $image) {
if($image has special tag) { // <--- PSEUDOCODE
echo "<div class='SPECIAL_CSS_CLASS'><img src='" . $img->path . "' /></div>";
}
}
Then in your CSS just do your Magic!
I would go ahead and do something like this:
//Function to get all the images based on a specific portrait_group
function getImagesPortraitGroup($group, $images) {
$result = array();
foreach ($images as $image) {
if (isset($image['portrait_group']) && $image['portrait_group'] == $group) {
$result[] = $image;
}
}
return $result;
}
//The images to show
$images = array(
["url" => "http://lorempixel.com/400/200/", "caption" => "Lorem Image1", "portrait_group" => 1],
["url" => "http://lorempixel.com/400/200/", "caption" => "Lorem Image2", "portrait_group" => 1],
["url" => "http://lorempixel.com/800/200/", "caption" => "Lorem Image3"],
["url" => "http://lorempixel.com/400/200/", "caption" => "Lorem Image4", "portrait_group" => 2],
["url" => "http://lorempixel.com/400/200/", "caption" => "Lorem Image5", "portrait_group" => 2],
);
//Everytime we show a group, we track it by adding it to this array in order not to show it again
$addedGroups = array();
//Loop through all the images
for ($i = 0; $i < count($images); $i++) {
echo "<div>";
//If the image has a portrait_group that is not already shown, show it
if (isset($images[$i]['portrait_group']) && !in_array($images[$i]['portrait_group'], $addedGroups)) {
$groupImages = getImagesPortraitGroup($images[$i]['portrait_group'], $images);
foreach ($groupImages as $image) {
echo "<img src='{$image['url']}' title='{$image['caption']}' >";
}
//Save the group to the array in order not to show it again
$addedGroups[] = $images[$i]['portrait_group'];
} else {
echo "<img src='{$images[$i]['url']}' title='{$images[$i]['caption']}' >";
}
echo "</div>";
}
I managed to make the grouping by jQuery, this is the code
jQuery(".ngg-gallery-thumbnail-box.portrait").filter(":odd").each(function(){
$content = jQuery(this).find("a");
jQuery(this).prev().find(".ngg-gallery-thumbnail").append($content);
jQuery(this).remove();
});
Related
Here's my problem, I have to display array values (description) under pictures like this :
Instead it appears like this
I'm working on this problem, this is my current code :
if (preg_match_all('/<div id="description" class="description">([^<]*)<\/div>/', $content, $match)) {
for( $i = 0; $i < count($match[0]); $i = $i+1 ) {
$description[] = $match[0][$i];
}
}
$attachments =& get_children($args);
$arrayMatches = array();
if ($attachments) {
foreach(array_chunk($attachments, 2) as $img) {
echo '<div class="two_cols">';
foreach($img as $attachment) {
foreach($attachment as $attachment_key => $attachment_value) {
$imageID = $attachment->ID;
$imageTitle = $attachment->post_title;
$imagearray = wp_get_attachment_image_src($attachment_value, $size, false);
$imageAlt = get_post_meta($imageID, '_wp_attachment_image_alt', true);
$imageURI = $imagearray[0]; // 0 is the URI
$imageWidth = $imagearray[1]; // 1 is the width
$imageHeight = $imagearray[2]; // 2 is the height
?>
<div class="col_1_2">
<div id="attachment_<?php echo $imageID; ?>" class="wp-caption alignnone" style="width: 356px;">
<a rel="lightbox-0" href="<?php echo $imageURI; ?>"><img class="wp-image-<?php echo $imageID; ?> size-full" title="<?php echo $imageTitle; ?>" src="<?php echo $imageURI; ?>" alt="<?php echo $imageAlt; ?>" width="456" height="304" /></a>
<p class="wp-caption-text"><?php echo $imageTitle; ?></p>
<?php
$arrayMatches[] = $match[0][$j];
?>
</div>
</div>
<?php
break;
}
$j++;
}
foreach(array_chunk($arrayMatches, 2) as $desc) {
echo '<div class="description">';
foreach($desc as $item) {
echo $item;
}
echo '</div>';
}
echo '</div>';
}
}
I try so many solutions but no one was the good one.
Thanks for the help ;)
Try:
foreach(array_chunk($arrayMatches, 2) as $k => $desc) {
if($k < 1) continue;
echo '<div class="description">';
foreach($desc as $item) {
echo $item;
}
echo '</div>';
}
I am trying to use $description variable outside the loop. Help me do it please.
<?php
$sql_album = "SELECT * FROM albums";
$res_album = mysql_query($sql_album) or die(mysql_error());
$albums = array();
$description = "";
while ($row_album = mysql_fetch_assoc($res_album)) {
$description = $row_album['description'];
$albums[$row_album['title']] = array(
'images/albums/'.$row_album['title'].'/1.jpg',
'images/albums/'.$row_album['title'].'/2.jpg',
'images/albums/'.$row_album['title'].'/3.jpg',
'images/albums/'.$row_album['title'].'/4.jpg'
);
}
foreach ($albums as $name => $a) {
?>
<div id="album">
<a href="view_album.php?name=<?php echo $name; ?>" data-images="<?php echo implode('|', array_slice($a,1))?>" class="album">
<img src="<?php echo $a[0]?>" alt="<?php echo $name?>" />
<span class="preloader"></span>
</a>
<div class="album_info">
<h4><?php echo $name?></h4>
<p><?php echo $description; ?></p>
</div>
</div>
<?php
}
?>
Should I make an array, or define it first, i am totally confused, need help.
In your $albums array (in the while loop), store your images and description like this:
$albums[$row_album['title']] = array(
"description" => $row_album['description'],
"images" => array(
'images/albums/'.$row_album['title'].'/1.jpg',
'images/albums/'.$row_album['title'].'/2.jpg',
'images/albums/'.$row_album['title'].'/3.jpg',
'images/albums/'.$row_album['title'].'/4.jpg'
)
);
Then in your foreach loop, act like this:
<img src="<?php echo $a['images'][0]?>" alt="<?php echo $name?>" />
and
<p><?php echo $a['description']; ?></p>
Edit:
Don't forget to change this
array_slice($a,1)
to this:
array_slice($a['images'],1)
I would just combine the whole thing into a single loop; the below code is untested, but I hope you can follow it.
while ($row_album = mysql_fetch_assoc($res_album)) {
print_album($row_album);
}
function print_album(array $album)
{
$name = htmlspecialchars($album['title'], ENT_QUOTES, 'UTF-8');
$description = htmlspecialchars($album['description'], ENT_QUOTES, 'UTF-8');
$view_link = sprintf('view_album.php?%s', http_build_query([
'name' => $album['title'],
]);
$images = array_map(function($index) use ($album) {
return sprintf(
'images/albums/%s/%d.jpg',
urlencode($album['title']),
$index
);
}, range(1, 4));
$images_data = htmlspecialchars(join('|', array_slice($images, 1)), ENT_QUOTES, 'UTF-8');
?>
<div id="album">
<a href="<?php echo $view_link ?>" data-images="<?php echo $images_data; ?>" class="album">
<img src="<?php echo htmlspecialchars($images[0], ENT_QUOTES, 'UTF-8'); ?>" alt="<?php echo $name; ?>" />
<span class="preloader"></span>
</a>
<div class="album_info">
<h4><?php echo $name; ?></h4>
<p><?php echo $description; ?></p>
</div>
</div>
<?php
}
I've added escaping with the use of urlencode(), htmlspecialchars() and http_build_query() to prevent a potential break in your HTML (or XSS is the worst case).
I am trying to achieve result with below structure in foreach loop where after every two images it will repeat entire structure.
I have some basic knowledge for something I can use eg. counter++; and than %2 but don't know syntax and how to use it for my code.
<?php
function dt_attached($postid=0, $size='thumbnail', $attributes='', $linksize='full', $count=-1) {
if ($postid<1) $postid = get_the_ID();
if ($images = get_children(array(
'post_parent' => $postid,
'post_type' => 'attachment',
'numberposts' => $count,
'post_mime_type' => 'image',)))
foreach($images as $image) {
$attachment=wp_get_attachment_image_src($image->ID, 'thumbnail');
$small_image = wp_get_attachment_image_src($image->ID, 'midium');
$big_image = wp_get_attachment_image_src($image->ID, 'full');
?>
<div class="mainrow">
<div class="block">
<a href='<?php echo $big_image[0]; ?>' class='cloud-zoom-gallery' title='Thumbnail 1' rel="useZoom: 'zoom1', smallImage: '<?php echo $small_image[0]; ?>' ">
<img src="<?php echo $attachment[0]; ?>" <?php echo $attributes; ?> />
</a>
</div>
<!--[I want to get two images in mainrow]-->
<div class="block">
<a href='<?php echo $big_image[0]; ?>' class='cloud-zoom-gallery' title='Thumbnail 1' rel="useZoom: 'zoom1', smallImage: '<?php echo $small_image[0]; ?>' ">
<img src="<?php echo $attachment[0]; ?>" <?php echo $attributes; ?> />
</a>
</div>
</div>
<?php //the_attachment_link($image->ID, false, true, false); ?>
<?php }
}
?>
So what I want is if there is more than two images it will repeat entire html structure. Thanks a lot for your help
What I gathered from your comments is that you want to display two images per row, and if there's one extra image, to display a placeholder next to it in the final row.
All you need is a count of how many images there are, and whether it's an even or odd number. Then once you know you're at the last image (using an incrementing counter), you add the placeholder:
What your code doesn't do is place two images in one row. For that we need to take the modulo (%) of the counter as well.
<?php
$counter = 0;
$imgCount = count($images);
foreach ($images as $image) {
$attachment = wp_get_attachment_image_src($image->ID, 'thumbnail');
$small_image = wp_get_attachment_image_src($image->ID, 'midium');
$big_image = wp_get_attachment_image_src($image->ID, 'full');
if ($counter % 2 == 0): ?>
<div class="mainrow">
<?php endif; ?>
<div class="block">
<a href='<?php echo $big_image[0]; ?>' class='cloud-zoom-gallery' title='Thumbnail 1' rel="useZoom: 'zoom1', smallImage: '<?php echo $small_image[0]; ?>' ">
<img src="<?php echo $attachment[0]; ?>" <?php echo $attributes; ?> />
</a>
</div>
<?php if ($counter++ % 2 == 1): ?>
</div>
<?php endif; ?>
<?php //the_attachment_link($image->ID, false, true, false); ?>
<?php
}
// Since (if there are an odd number of images) the loop may not close the <div>,
// we have to make sure it does.
if ($counter % 2 == 0) {
?>
<!-- placeholder goes here -->
</div>
<?php
}
quite new to PHP and I'm trying to get my page to show 3 blog entries, followed by 3 photos, followed by another 3 blog entries and so on and soforth.
I've been recommended using do while loops but having a hard time getting it working or even getting my head round it, have been more used to using foreach loops in the CMS.
This was my original code which would work but only if I explicitly added each loop by hand!
<?php // Show 3 blog entries
$entries = $page->children("sort=-sort, limit=3");
$count = 0;
foreach ($entries as $entry) {
$count++;
$class = "blog_box";
if ($entry == $entries->last()) {$class .= " blog_box_last"; }
if ($entry == $entries->first()) {$class .= " blog_box_first"; }
if (0 == $count % 2) { $class .= " blog_box_even"; }
?>
<div class="<?php echo $class; ?>">
<div class="blog_text">
<h3><?php echo $entry->title; ?></h3>
<h6><?php echo $entry->entry_date; ?></h6>
<p><?php echo $entry->body; ?></p>
</div><!-- /.blog_text -->
<?php if ($entry->image) {
$image = $entry->image->width(350);
?>
<img src="<?php echo $image->url; ?>" width="<?php echo $image->width; ?>" alt="<?php echo $entry->title; ?>" />
<?php } ?>
<div class="clear"></div><!-- /.clear -->
</div><!-- /.blog_box -->
<?php }
?>
// Show 3 blog images
<?php $blog_images = $page->get("image_uploads")->find("limit=3");
foreach ($blog_images as $img) {
$b_img = $img->size(200,140); ?>
<img src="<?php echo $b_img->url; ?>" width="<?php echo $b_img->width; ?>" height="<?php echo $b_img->height; ?>" alt="<?php echo $b_img->description; ?>" class="small_frame" />
<?php } ?>
The CMS developer kindly tried some code for me but I couldn't get it to work for me:
$entries = $page->children("sort=-sort");
$images = $page->get("/image_uploads/");
$cnt = 0;
do {
$cnt++;
$entry = $entries->shift();
if($entry) {
// output 1 entry
}
if($cnt == 3) {
while(++$cnt <= 6) {
$image = $images->shift();
if($image) {
// output 1 image
}
}
$cnt = 0;
}
} while(count($entries) && count($images));
I'm using ProcessWire.
Thanks in advance guys!
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;
?>