I have fetched some list of destinations cities from database and want to show it in view.
here is my normal approach
<?php
$this->db->select('*');
$citydata = $this->db->get('cities')->result_array();
foreach ($citydata as $citydatas){?>
<div class="col-lg-4">
<img src="<?=$citydatas['banner];?>" />
</div>
<? } ?>
Above code will display 3 columns in each row,
But What i want is different approach, I need to show 2 columns in first row, then 3 column in second row.
and
Another Approach would be - three column in first row but first column will take half screen and other two will take another half and in second row first two will take half and then third will take rest half screen using col-6
here is the first grid style what i am looking for
here is the second grid style
My Approach for first style, is below approach good to use?
<?php
$this->db->select('*');
$citydata = $this->db->get('cities')->result_array();
$i = 1;
foreach ($citydata as $citydatas){?>
<div class="<?php if($i++ == 1 || $i++ == 2){ echo "col-lg-6";}else{echo"col-lg-4";}?>">
<img src="<?=$citydatas['banner];?>" />
</div>
<? } ?>
You can use nth-child selector along with CSS grid to get the desired result.
First Grid Style
.grid-container {
display: grid;
grid-template-columns: repeat(4, 1fr);
grid-gap: 10px;
grid-auto-rows: 200px;
}
img:nth-child(6n + 1),
img:nth-child(6n + 2){
grid-column: span 2;
}
img {
width: 100%;
height: 100%;
}
<div class="grid-container">
<img src="https://picsum.photos/id/10/400" />
<img src="https://picsum.photos/id/20/400" />
<img src="https://picsum.photos/id/30/400" />
<img src="https://picsum.photos/id/40/400" />
<img src="https://picsum.photos/id/50/400" />
<img src="https://picsum.photos/id/60/400" />
</div>
Second Grid Style
For second grid style, you just need to change the nth-child selector, rest of the code will be same.
img:nth-child(6n),
img:nth-child(6n + 1) {
grid-column: span 2;
}
.grid-container {
display: grid;
grid-template-columns: repeat(4, 1fr);
grid-gap: 10px;
grid-auto-rows: 200px;
}
img:nth-child(6n),
img:nth-child(6n + 1) {
grid-column: span 2;
}
img {
width: 100%;
height: 100%;
}
<div class="grid-container">
<img src="https://picsum.photos/id/10/400" />
<img src="https://picsum.photos/id/20/400" />
<img src="https://picsum.photos/id/30/400" />
<img src="https://picsum.photos/id/40/400" />
<img src="https://picsum.photos/id/50/400" />
<img src="https://picsum.photos/id/60/400" />
</div>
PHP Code
Use following code to render this layout using PHP
<div class="grid-container">
<?php foreach($citydata as $citydatas): ?>
<img src="<?=$citydatas['banner]?>"/>
<?php endforeach; ?>
</div>
Related
I'm writing here today because I need some help to insert different class into a foreach loop.
CURRENT SITUATION
I have a foreach loop like this one:
<?php
$propertyImages = get_field('property_images');
if( $propertyImages ):
?>
<div class="container">
<?php foreach( $propertyImages as $propertyImage ): ?>
<a class="gallery-item href="<?php echo esc_url($propertyImage['url']); ?>">
<img class="gallery-img" src="<?php echo esc_url($propertyImage['sizes']['medium']); ?>"/>
</a>
<?php endforeach; ?>
</div>
<?php endif; ?>
DESIRED SITUATION
With this loop I want to display the images in a grid pattern that loops itself (like the one you can see in the images below.
I think that to achieve this I need to add a "grid-lg-img" for the first 2 element of the loop then add a "grid-sm-img" for the 3rd 4th 5th items of the loop and then again and again with the same 2-3-2-3-... pattern.
Is it possible to craft a solution like this? Or maybe I'm looking in the wrong direction?
Thank you.
You can use only CSS.
.container {
width: 100%;
display: flex;
flex-wrap: wrap;
}
a.gallery-item {
display: block;
}
a.gallery-item:nth-child(5n+1) {
flex: 1 50%;
}
a.gallery-item:nth-child(5n+2) {
flex: 1 50%;
}
a.gallery-item:nth-child(5n+3),
a.gallery-item:nth-child(5n+4)
a.gallery-item:nth-child(5n+5) {
flex: 1 33,333333336%;
}
a.gallery-item img {
width: 100%;
height: 100%;
object-fit: cover;
}
You can do this easily with CSS grid.
If you make the grid a total number of columns which is divisible by the amount of actual columns you want in both your 'large' and 'small' size, then you can just target those first two elements and make them span however many columns you'd like.
So, here you want your large images to be half width, and the smaller ones will be one third width. 6 is divisible by both 2 and 3, so your half width images can be 3 of 6 available columns, and your third width images can be 2 of 6.
html,
body {
min-height: 100%;
height: 100%;
}
body {
display: flex;
align-items: center;
justify-content: center;
}
.gallery {
max-width: 75%;
padding: 1.5em;
background: #f2f2f2;
display: grid;
gap: .5em;
grid-template-columns: repeat(6, 1fr);
}
.gallery__item:nth-of-type(1),
.gallery__item:nth-of-type(2) {
grid-column: span 3;
}
.gallery__item {
width: 100%;
grid-column: span 2;
}
<html lang="en">
<head></head>
<body>
<div class="gallery">
<img class="gallery__item" src="https://unsplash.it/300/200/?random" />
<img class="gallery__item" src="https://unsplash.it/300/200/?random" />
<img class="gallery__item" src="https://unsplash.it/300/200/?random" />
<img class="gallery__item" src="https://unsplash.it/300/200/?random" />
<img class="gallery__item" src="https://unsplash.it/300/200/?random" />
<img class="gallery__item" src="https://unsplash.it/300/200/?random" />
<img class="gallery__item" src="https://unsplash.it/300/200/?random" />
<img class="gallery__item" src="https://unsplash.it/300/200/?random" />
</div>
</body>
</html>
Further reading on grid here.
you can wrap it all with while
<?php
$propertyImages = get_field('property_images');
while($propertyImages) : $i =0;
if( $propertyImages ):
?>
<div class="container">
<?php foreach( $propertyImages as $propertyImage ): ++$i; ?>
<a class="gallery-item-<?php echo $i?> href="<?php echo esc_url($propertyImage['url']); ?>">
<img class="gallery-img" src="<?php echo esc_url($propertyImage['sizes']['medium']); ?>"/>
</a>
<?php endforeach; ?>
</div>
<?php endif; ?>
<?php endwhile; ?>
am working on a codeigniter website and currently trying to achieve a layout like this where i have equal heights for all images in row but different widths.
My images are stored in a MYSQL database with url paths pointing to the directory where they are located. I have also stored their heights and widths (the height and widths are stored in pixels).
Here is the sql for fetching the contents in PhotoController
$data['data'] = $this->model
->select('photo_path, photo_height, photo_width')
->get()
->getResultArray();
Then in Home view, here is what i have so far. (using bootstrap 4 for styling)
<div class="container">
<div class="row">
<?php foreach ($data as $row) : ?>
<div class="col-md-4 col-lg-6">
<img src="<?= base_url($row['photo_path']); ?>" class="img-lazy img-fluid">
</div>
<?php endforeach; ?>
</div>
</div>
Am stuck on what to do next here.
html
<div class="masonry-with-columns-2">
<?foreach ($data as $row){?>
<div>
img here
</div>
<?}?>
</div>
scss
// Within style tags in your html file
body {
margin: 0;
padding: 1rem;
}
// SCSS
// Masonry layout horizontal
.masonry-with-columns-2 {
display: flex;
flex-wrap: wrap;
div {
height: 150px;
line-height: 150px;
background: #9B1B30;
color: white;
margin: 0 1rem 1rem 0;
text-align: center;
font-weight: 900;
font-size: 2rem;
flex: 1 0 auto;
}
#for $i from 1 through 36 {
div:nth-child(#{$i}) {
$h: (random(400) + 70) + px;
width: $h;
}
}
}
Source https://mdbootstrap.com/docs/angular/layout/masonry/
I have an array of images in php that I am trying to display as a gallery. It is working pretty well, except that the first row of the gallery comes out diagonal, with each image slightly below the one before it.
Here is the code I am using:
CSS
.gallery {
margin: 5px;
border: 1px solid #ccc;
float: left;
display: inline;
width:180px;
height:180px;
}
.gallery:hover {
border: 1px solid #777;
}
.gallery img {
max-height:100%;
max-width:100%;
margin:0 auto;
}
HTML/PHP
<div class="gallery-container">
<?php
$count = 0;
foreach($images as $image) {
echo "<div class='gallery'><a href=''><img src='".$dir.$image."'/></a></div>"; //$dir is the path to the image
if(count % 4 == 0) {
echo "<br>";
}
$count = $count + 1;
}
?>
</div>
Here is a screenshot of the result on the first row:
All the other rows come out fine. Thank you very much for any help.
If you want to do it using float, you would better do that like this:
CSS
.gallery{
margin: 5px;
border: 1px solid #ccc;
float: left;
display: block;
width:180px;
height:180px;
}
.gallery.clear-left{
clear: left;
}
PHP/HTML
<div class="gallery-container">
<?php
$count = 0;
foreach( $images as $image ){?>
<div class="gallery <?php echo( 0 == $count % 4 ? 'clear-left' : '' );?>">
<a href="">
<img src="<?php echo $dir.$image;?>">
</a>
</div>
<?php
$count++;
}
?>
</div>
But there is an even better solution to this, called flex:
Instead of br element render "clear" div
<div style="clear:both"></div>
Have you tried starting the count at 1 instead of 0?
Checkout the resulting html. It is likely that you count is off and the br is being put to the dom too often. This fiddle shows that your desired html and css should work.
<div class='gallery'><a href=''><img src='https://upload.wikimedia.org/wikipedia/commons/b/b2/Hausziege_04.jpg'/></a></div>
<div class='gallery'><a href=''><img src='https://upload.wikimedia.org/wikipedia/commons/b/b2/Hausziege_04.jpg'/></a></div>
<div class='gallery'><a href=''><img src='https://upload.wikimedia.org/wikipedia/commons/b/b2/Hausziege_04.jpg'/></a></div>
<div class='gallery'><a href=''><img src='https://upload.wikimedia.org/wikipedia/commons/b/b2/Hausziege_04.jpg'/></a></div>
<br>
Though, I would consider not using the br, but instead using a containing div:
like this
Morning all,
I'm having an alignment issue.
I am using a MYSQL array to create a sports profile page.
I want it to display the image, then to the right, show their name, position and number below each other.
Then i want it to dynamically display 3 or 4 of these next to each other before swapping down to the next line.
I have managed to get it to work with just the pictures, not with the text in between. they all just show on new lines currently.
<style>
#container .profile{
display: inline-block;
vertical-align:top;
}
</style>
<div class="profile">
<img src="wp-content/uploads/profile/jh7.jpg" alt="" /><br />
<h3 class="widget-title">Player 1</h3>
<p>Defence</br>#7</br></br>
<img src="wp-content/uploads/profile/dw21.jpg" alt="" /><br />
<h3 class="widget-title">Player 2</h3>
<p>Defence</br>#21</br></br>
<img src="wp-content/uploads/profile/pn22.jpg" alt="" /><br />
<h3 class="widget-title">Player 3</h3>
<p>Defence</br>#22</br></br>
</div>
</div><!-- .entry-content -->
</div>
Thanks guys
You can use float left for the image:
<style>
.profile img{
float: left;
margin-right: 5px;
}
</style>
Checkout this DEMO: http://jsbin.com/jigotamamu/1/
Quick example : float the images and clear the float with a block element having a clear: both property :
http://jsfiddle.net/L8jtwkw1/2/
You can wrap each profile in a container and use inline block to list them horizontally then.
It still didn't do exactly what I was after, However I managed to sort it by putting a column in:
<style>
.profile img{
float: left;
margin-right: 10px;
margin-top: 2px; }
.profile h3 { display: inline-block; }
.profile pos{ }
.column-left{ float: left; width: 33%; }
</style>
I think that thie problem is in <h3> tag - you need either replace it with <span> or override styles for it.
For example:
echo '<img src="wp-content/uploads/profile/'.$row['Id'].'.jpg" alt="" />',
'<span class="widget-title">'.$row['FirstName'].' '.$row['Surname'].'</span>',
$row['PositionLong'].'</br>',
'#'.$row['Number'].'</br>';
If you show the css styles for widget-title, we can help you.
This question already has answers here:
Dividing long list of <li> tags into columns?
(3 answers)
Closed 9 years ago.
I have the query on php which takes a lot of images from database, I need to show 4 images on the first row, the other 4 images on the next row and so on.
I thought I could do that with the help css, but I can't =(
HTML
<div id="photo_block" style="border:2px dotted <?echo $color;?>; height:1000px;" >
<?
while ($item=$select_items->fetch()) {
?>
<ul class="items">
<li>
<div class="item_block">
<img src="<?echo $item['src']?>">
</div>
</li>
</ul>
<?
}
?>
</div>
CSS
#photo_block{
width:800px;
}
.items li{
display: inline;
}
.items li .item_block img{
width: 100px;
height: 100px;
}
thank you for helping me!
you should reset the padding and margin of the ul element
your img size is set to 100px so they will fit inside your #photo_block 8 times in a row
How about: http://jsfiddle.net/4Ntzq/
Why are you creating a new list with one item for every separate image?
I would not use a list at all, and just put them in divs four at a time, then scale the images to (a little under) 25%.
<div class='imagerow'>
<?
$i = 0;
while ($item=$select_items->fetch()) {
if($i > 0 && $i % 4 == 0) {
?>
</div>
<div class='imagerow'>
<?
} // end if
?>
<img src="<?echo $item['src']?>">
<?
} // end while
?>
</div>
with CSS
.imagerow img { width: 24.75%; }
where the width is just short of 25% because experience taught me that if you take exactly 25% the last one often still overruns onto the next line.
HTML
<div id="photo_block" style="border:2px dotted <?echo $color;?>; height:1000px;" >
<ul class="items">
<? while ($item=$select_items->fetch()) { ?>
<li>
<div class="item_block">
<img src="<?echo $item['src']?>">
</div>
</li>
<? } ?>
</ul>
</div>
CSS
#photo_block{
width:500px;
}
.items li{
width:100px;
margin-right:5px;
float: left;
list-style: none;
}
.items li .item_block img{
width: 100px;
height: 100px;
}
Use float: left; instead of display: inline, then add a new rule that does a clear: left every 4th <li> element.
HTML:
<div id="photo_block" style="border:2px dotted <?echo $color;?>; height:1000px;" >
<ul class="items">
<? while ($item=$select_items->fetch()): ?>
<li>
<div class="item_block">
<img src="<?echo $item['src']?>">
</div>
</li>
<? endwhile ?>
</ul>
</div>
CSS:
#photo_block{
width:800px;
}
.items li{
float: left;
}
.items li:nth-child(4n)
{
clear: left;
}
.items li .item_block img{
width: 100px;
height: 100px;
}