I want to tile thumbnails into 3 columns using bootstrap's grid classes like this (this entry only has 3 images):
The 4th image would go to the next row <div class="row"></div> within a <div class="col-sm-4"></div>, 5th and 6th images in the same row but separate <div class="col-sm-4"></div>. Then the 7th image goes to the 3rd row etc...
The details of the images including urls are taken from DB using php.
<div class="panel-body">
<?php foreach($screenshots as $key=>$screenshot): ?>
<div class="col-sm-4">
<div class="panel panel-default">
<div class="panel-heading">
<h3 class="panel-title"><?=$screenshot["ss_name"]?></h3>
</div>
<div class="panel-body">
<img class="img-rounded" style="display: block; text-align:center;" src="<?=UPLOADS_FOLDER.$screenshot['ss_url']?>" alt="<?=$screenshot['ss_name']?>">
<p><?=$screenshot["ss_description"]?></p>
</div>
</div>
</div>
<?php endforeach; ?>
</div>
I've managed to figure out the algorithim:
<?php
$total_entries = count($screenshots);
$total_cols = 3;
$total_rows = ceil($total_entries / $total_cols);
for($col = 0; $col < $total_cols; ++$col):
?>
<div class="row" style="margin: 1% 0.5%;">
<?php for($row = 0; $row < $total_rows; ++$row): ?>
<div class="col-sm-4">
<div class="panel panel-default">
<div class="panel-heading">
<h3 class="panel-title">Panel Title</h3>
</div>
<div class="panel-body">
row: <?=$row?> | col: <?=$col?>
</div>
</div>
</div>
<?php endfor; ?>
</div>
<?php endfor; ?>
But I'm stuck trying to figure out how to find of the index of the screenshot to show.
You have row and column mixed up in the output above.
Once that is fixed, if you need an integer index you should be able to calculate it from the row and column values. For a zero based array of images, something like (row*3)+column
That said, in Bootstrap, you should not need to create the individual rows in the way you have. If you put all the col-sm-3 divs one after the other, without breaking out new rows, this will sort itself out anyway.
Doing it this way, you can use col-Xxx to specify different numbers of columns for different screen widths without changing your code.
The trick is to send every new data in a single column define by <div class="column-sm-4"> and in order to have three items in a row, calling <div class="row"> after every three items. Which can be done by using a counter initialized at 0 and then incrementing its value by 1 every time and inserting new row when dividing it by 3 results an integer:
$count = 0;
if (is_int($count/3)){
echo '<div class="row">';
}
We need to insert a div before first element so we check if its the starting:
if ($count==0 OR is_int($count/3)){
echo '<div class="row">';
}
Then in order to close the div we need to check again if the division results an integer:
if(is_int($count/3)){
echo '</div>' ;
}
We also need to close the div after the last element so we check whether the element is last one or not by:
if($count==$total_entries OR is_int($count/3)){
echo '</div>' ;
}
The full code is:
<?php
$count = 0;
$total_entries = count($screenshots);
foreach($screenshots as $key=>$screenshot):
if ($count==0 OR is_int($count/3)){
echo '<div class="row">';
}
?>
<div class="column-sm-4">
<div class="panel panel-default">
<div class="panel-heading">
<h3 class="panel-title"><?=$screenshot["ss_name"]?></h3>
</div>
<div class="panel-body">
<img class="img-rounded" style="display: block; text-
align:center;" src="<?=UPLOADS_FOLDER.$screenshot['ss_url']?>"
alt="<?=$screenshot['ss_name']?>">
<p><?=$screenshot["ss_description"]?></p>
</div>
</div>
</div>
<?php
$count++;
if($count==$total_entries OR is_int($count/3)){
echo '</div>' ;
}
endforeach;
?>
Related
I have 2 tables in database first one is $slide_images and second is $why_us and I am using owl carousel for images.
The $slide_images contains 10 rows.
The $why_us contains 3 rows.
I need each image take one row of $why_us that's mean the first 3 images will have a text from $why_us table.
I tried many ways to do that but it's not given me what I want and I can't edit tables to do that and the id column is not nested to join them with SQL.
Can i solve them with nested foreach?
<div class="home_area">
<!-- start Carousel -->
<div class="owl-carousel block_1">
<?php
if(is_array($slide_image)){
foreach($slide_image as $src){
?>
<div class="overlay-text">
<div class="carousel_item" style="background-image:url('<?php echo base_url(); echo $src->url; ?>');"></div>
<div class="text-layer">
<!--============ start we us ================ -->
<?php if($why_us != ''){ ?>
<div class="we">
<div class="container">
<div class="row">
<?php foreach($why_us as $we){ ?>
<div class="box">
<h6><?php echo $we->name; ?></h6>
<div class="text"><?php echo $we->desc; ?></div>
</div>
<?php }?>
</div>
</div>
</div>
<?php } ?>
<!--============= end we us ========== -->
</div>
</div>
<?php } }?>
</div>
<!-- end Carousel -->
</div>
I hope I described my question well.
You should not use nested loops. That creates a cross product between the arrays.
Instead, you should just have one loop that uses the corresponding elements of both arrays. Since one of the arrays is smaller, you should check that the index exists.
foreach ($slide_image as $index => $src) {
// code that uses `$src` here
if (isset($why_us[$index])) {
$we = $why_us[$index];
// code that uses $we here
}
}
I have a foreach and I want to place 4 columns then break in a new row and start again but for some reason i got the column number 5(test2) bad pushed like this picture:
should be like this:
here is the code to generate that:
<div class="container">
<div class="row">
<div class="col-md-8 col-md-offset-2">
<div class="panel panel-default">
<div class="panel-heading">
Secciones
</div>
<div class="panel-body">
<div class="row">
#foreach ($boards as $board)
<div class="col-md-3">
<h3>{{$board->boaName}}</h3>
#foreach ($subboards as $subboard)
#if($subboard->boaId == $board->id)
<ul>
<li>{{$subboard->subboaName}}</li>
</ul>
#endif
#endforeach
</div>
#endforeach
</div>
</div>
</div>
</div>
</div>
</div><!--End container-->
Try moving the foreach loop outside of the <div class="row">? If that doesn't work do you think you could mock this up in a jsFiddle example?
You need a test after 4 iterations to close and open again the row div.
If you know how many columns to expect, you could do the following (I've ommited your inner content of each column for my example - I've also left the counter in so you can see the incrementation for testing):
$i = 1;
foreach($boards as $board) {
if($i == 1 || $i == 5 || $i == 9) {
echo "<div class='row'>";
echo "<div class='col-md-3'>" . $i++ . "</div>";
}
elseif($i%4 == 0) {
echo "<div class='col-md-3'>" . $i++ . "</div>";
echo "</div>";
} else {
echo "<div class='col-md-3'>" . $i++ . "</div>";
}
}
What's happening here is if the column count is the 4th iteration, it should echo out an opening <div class="row"> as well as a column. If the current column is divisible by 4, it should echo out a column with an extra closing </div> (to close the row class.)
If the current count is neither of the above, it simply echo's out a column.
You can, of course, mod the above if the amount of potential columns is unknown.
I am building a wordpress site using bootstrap, and I want to create a featured work section on my homepage using masonry to display the thumbnails and titles of my 3 most recent portfolio items.
I would like the portfolio items to be placed in a seemingly random fashion (similar to this: http://studiosweep.com/) rather than just an orderly grid format. I can't figure out how to assign different widths to my portfolio items because they're just being generated in the featured work section via wordpress loop.
Here's my HTML:
<section id="work">
<div class="container-fluid">
<div class="row">
<div class="col-sm-offset-6 col-sm-6 text-center">
<h1>Featured Work</h1>
</div>
</div>
<div class="row" id="ms-container">
<?php query_posts('posts_per_page=3&post_type=work'); ?>
<?php while ( have_posts() ) : the_post();
$thumbnail = get_field('thumbnail');
$medium = get_field('medium');
$size = "large";
?>
<div class="ms-item col-lg-6 col-md-6 col-sm-6 col-xs-12">
<figure>
<?php echo wp_get_attachment_image($thumbnail, $size); ?>
</figure>
<h3><?php the_title(); ?></h3>
<div class="clearfix"></div>
</div>
<?php endwhile; // end of the loop. ?>
<?php wp_reset_query();
</div>
<div class="clearfix"></div>
</div>
</section>
Here's the script:
<script type="text/javascript">
jQuery(window).load(function() {
// MASSONRY Without jquery
var container = document.querySelector('#ms-container');
var msnry = new Masonry( container, {
itemSelector: '.ms-item',
columnWidth: '.ms-item',
});
});
</script>
As for CSS, I don't really know how to go about assigning the different column widths, so I only have this so far:
.ms-item {
width: 38.23%;
margin-right: 80px;
margin-bottom: 70px;
}
Any help would be appreciated!
Let's say you have 3 different classes for column widths:
.ms-item-width-1, .ms-item-width-2, .ms-item-width-3
A possible solution is to add those 3 classes to your css file and randomly assign one of your classes to your container after .ms-item class. Masonry will give the width of the last class you added to that container. For example:
<div class="ms-item <?php echo 'ms-item-width-' . (rand(0, 3) + 1); ?> col-lg-6 col-md-6 col-sm-6 col-xs-12">
<figure>
<?php echo wp_get_attachment_image($thumbnail, $size); ?>
</figure>
Update: To have a randomized without getting repeated values you could have an array $widths = array(1,2,3); and then shuffle this array shuffle($widths); and instead of calling the random function you would be removing the elements of the array and replace it with the following code:
<div class="ms-item <?php echo 'ms-item-width-' . array_shift($widths); ?> col-lg-6 col-md-6 col-sm-6 col-xs-12">
This will provide an unique width to this 3 items.
So I haven't programmed in a while, but i decided to tackle a small web app to get back in the game. Im having a bit of trouble though, and its kind of annoying because I know the answer is simple. So i'm making a databaseless image gallery, and i'm using Twitter Bootstrap as the front end. I want to display the images that i'm reading from a directory using the classic bootstrap row setup like this:
<div class="row">
<div class="col-md-3 portfolio-item">
<img class="img-responsive" src="http://placehold.it/750x450">
</div>
<div class="col-md-3 portfolio-item">
<img class="img-responsive" src="http://placehold.it/750x450">
</div>
<div class="col-md-3 portfolio-item">
<img class="img-responsive" src="http://placehold.it/750x450">
</div>
<div class="col-md-3 portfolio-item">
<img class="img-responsive" src="http://placehold.it/750x450">
</div>
</div>
The problem is that I can only put four images into a row so I cant just do something like:
<div class="row">
<?php echo"$img";?>
</div>
How can I echo a
<div class="row">
every four
<?php echo"$img";?>
I feel like a nested loop may work, but i just cant seem to get it.
Thanks.
You need a loop with an index; then you can check if the index is divisible by 4:
if ($i % 4 == 0) {
?><div class="row"></div><?php
}
Alternately, you can array_chunk your data, and run a nested loop on the results, outputting the row in the outer loop.
For each row
for($i=0; $i<$num_item; $i++) {
Start a new row before the first item and every 4th item following
if($i % 4 == 0) {
echo '<div class="row">';
}
End a row after the 4th item and every 4th item following
if($i % 4 == 3) {
echo '</div>
}
Full code:
for($i=0; $i<$num_item; $i++) {
if($i % 4 == 0) {
echo '<div class="row">';
}
// Print the item here
if($i % 4 == 3) {
echo '</div>
}
}
When dealing with a lot of html code using the (notice the colon and endif)
<?php for (index; condition; step) : ?>
<h1>HTML STUFF</h1>
<?php endif; ?>
alternative syntax is a bit cleaner than the conventional markup.
I would suggest:
<div class="row">
<?php for($i = 0; $i < $total_pics; $i++): ?>
<div class="col-md-3 portfolio-item">
<img class="img-responsive" src="<?php echo $img_link; ?>">
</div>
<?php if ($i % 4 == 3 && $i < $total_pics): ?>
</div>
<div class="row">
<?php endif;
endfor; ?>
</div>
This allows the new row only to be put in place if there are more pictures to show and after the fourth one only.
This allows the html code to be indented as you want and clean the php code up a bit in the process. This also allows the initial row to be self closing if there are no pictures to show.
I have this html code:
<div class="row elem2">
<div class="item"></div>
<div class="item"></div>
</div>
<div class="row elem4">
<div class="item"></div>
<div class="item"></div>
<div class="item"></div>
<div class="item"></div>
</div>
<div class="row elem3">
<div class="item"></div>
<div class="item"></div>
<div class="item"></div>
</div>
and I am looking for a way to implement it in my php while (wordpress).
The while is as
while ( have_posts() ) : the_post();
echo '<div class="item"></div>';
endwhile;
I've tried a lot of stuff, but none have worked. I need to divide every 2 items and put them in a wrap <div class="row elem2"> after that the next 4 items in <div class="row elem4"> and after that the next 3 items in <div class="row elem3">
I did a lot of searching, but I am not even sure what to search for.
A little crude but here's one solution
$i = 0; // Number of items made so far in the row
$mode = 0; // Current row type enumerated by $elem
$elem = array(2,4,3); // Enumeration of the desired row sizes
while ( have_posts() ) : the_post();
// Make a new row when there's no items yet
if ($i == 0) echo '<div class="row elem'. $elem[$mode] .'">';
echo '<div class="item"></div>';
$i++;
// Once the items in the current row has reached the row's maximum size
if ($i % $elem[$mode] == 0):
echo '</div>';
$i = 0; // Reset items made for the row back to 0
$mode = ($mode + 1) % 3; // Increment mode and wrap if necessary
endif;
endwhile;
if ($i > 0) echo '</div>'; // Finish the last row if it wasn't finished
This is what the modulos was built for!