Bootstrap multiple rows based on mysql query count - php

I am using Bootstrap to create a row of 6 colunm images.
<div class="row">
<?php
$sql = //my query
while($row = mysqli_fetch_array($sql)){
?>
<div class="col-xs-6 col-md-2">
<img id="img" class="img-responsive" src="img/<?php echo $row['image']; ?>" />
</div>
<?php
}
?>
</div>
Which works fine when there is only 6 rows from the database, however when there is 7 the extra one gets bumped to the far right rather than the far left on the next row, see example below.
| img | img | img | img | img | img
| img
Normal result i am looking for would be
| img | img | img | img | img | img
| img
I want to create a new bootstrap row after every 6 results, any help would be appriciated

You could fetch all the values into an array first, count how many elements are in the array and then dynamically control what span classes you use.
<div class="row">
<?php
$sql = //my query
$images = array();
while($row = mysqli_fetch_assoc($sql)){
$images[] = $row['image'];
}
$column_class = count($images) > 6 ? "col-xs-3 col-md-1" : "col-xs-6 col-md-2";
foreach ($images as $image) {
?>
<div class="<?= $column_class ?>">
<img id="img" class="img-responsive" src="img/<?= $image ?>" />
</div>
<?php
}
?>

i have seen this before when the heights of the images are not identical. the float left only floats to the point where the bottom line is uneven. try adding a style height with a reasonable fixed height.
<div class="row">
<?php
$sql = //my query
while($row = mysqli_fetch_array($sql)){
?>
<div class="col-xs-6 col-md-2">
<img id="img" class="img-responsive" style="height:100px" src="img/<?php echo $row['image']; ?>" />
</div>
<?php
}
?>
</div>

I ran into this problem in a recent project and was able to solve it with this code. I only have two views so I only need the two lines but you could have more if you wanted. You can see this in action on this page
<div class="visible-md-block visible-lg-block clearfix">
<div class="visible-sm-block visible-xs-block clearfix">
Here is the full php code for the gallery page
<?php
$c = 1;
foreach($images as $image) {?>
<div class="col-xs-4 col-md-3">
<a class="thumbnail" href="path to full size image" data-toggle="lightbox" data-footer="<?php echo $image['caption']; ?>">
<img class="img-responsive" src="path to thumbnail" alt="...">
</a>
</div>
<?php
if(($c % 4) == 0) {
echo '<div class="visible-md-block visible-lg-block clearfix"></div>';
}
if(($c % 3) == 0) {
echo '<div class="visible-sm-block visible-xs-block clearfix"></div>';
}
$c++;
}
?>
Sorry while this code works for when you want the number of images in a row to change at different screen sizes I just re read your question and you say you just want 6 across all the time so you could just use one line
if(($c % 6) == 0) {
echo '<div class="clearfix"></div>';
}
The $c variable is just a counter and the code $c % 6 == 0 will put the clearfix div every time $c is divisible by 6.

Related

Get 2 rows from a table having same column values in PHP

TABLE
id m_id image
1 1 img1.jpg
2 2 img2.jpg
3 1 img11.jpg
4 2 img22.jpg
5 1 img111.jpg
........ etc
What i need is - Get only 2 rows from the table group by m_id as below
//here getting images where m_id=1 and it is not more than 2
<section class='test'>
<div class="n-2">
<img src='img1.jpg'>
</div>
<div class="n-2">
<img src='img11.jpg'>
</div>
</section>
//next fetching images with m_id=2
<section class='test'>
<div class="n-2">
<img src='img2.jpg'>
</div>
<div class="n-2">
<img src='img22.jpg'>
</div>
</section>
......
What i tried is
<?php
$query=$db->query("Select * from gallery order by m_id asc");
$result = $query->fetchAll(\PDO::FETCH_ASSOC);
for ($i=0; $i < count($result); $i+=2) {
?>
<section class='test'>
<div class="n-2">
<img src='<?php $result[$i]['image']; ?>'>
</div>
<div class="n-2">
<img src='<?php $result[$i+1]['image']; ?>'>
</div>
</section>
<?php
}
?>
But i am getting all the fields to the showed section class'test', How to get the only 2 rows as showed section class.
Any experts?
Hi this query will get you only two record
you should read the MySql tutorials in w3schools.com
Select * from gallery order by m_id asc LIMIT 2
Update 1: this will work hopefully in your case.
$query = $db->query("Select * from gallery order by m_id asc");
$items = $query->fetchAll(\PDO::FETCH_ASSOC);
$result = [];
$temp = [];
foreach ($items as $item) {
$m_id = $item['m_id'];
if (!isset($temp[$m_id]) || count($temp[$m_id]) < 2) {
$temp[$m_id][] = $item['image'];
$result = $item['image'];
}
}
for ($i = 0; $i < count($result); $i += 2) {
?>
<section class='test'>
<div class="n-2">
<img src='<?php $result[$i]['image']; ?>'>
</div>
<div class="n-2">
<img src='<?php $result[$i + 1]['image']; ?>'>
</div>
</section>
<?php
}

HTML Image gallery with Dynamic with PHP

I am trying to display images on a page using the bootstrap grid system, where their names are dynamically created by taking it from a database. For example I have this database, and I want to use the imageID in the src name of each image. Is there any cleaner way to do it without having to manually add a new div etc for each image?
PHP Code for getting image id:
<?php
//dynamically render images
include "../storescripts/connect-mysql.php";
$sql = mysql_query("SELECT * FROM imageGallery ORDER BY dateAdded ASC");
$images = array();
$imageCount = mysql_num_rows($sql); //Count the amount of products
if($imageCount > 0){
while($row = mysql_fetch_array($sql)){
$image = $row['imageID'];
$images[] = $image;
}
}else{
$image_gallery = "<h2>You have no images in the database</h2>";
}
?>
My HTML for displaying the images:
<div class="col-md-12">
<div class="col-md-3 galleryImg">
<img onclick="myfunction(this)" class="galleryImage" src="../img/gallery/<?php echo $images[0] ?>.png" alt="Jedi Cycle Sport Gallery Image">
</div>
<div class="col-md-3 galleryImg">
<img onclick="myfunction(this)" class="galleryImage" src="../img/gallery/<?php echo $images[1] ?>.png" alt="Jedi Cycle Sport Gallery Image">
</div>
<div class="col-md-3 galleryImg">
<img onclick="myfunction(this)" class="galleryImage" src="../img/gallery/<?php echo $images[2] ?>.png" alt="Jedi Cycle Sport Gallery Image">
</div>
<div class="col-md-3 galleryImg">
<img onclick="myfunction(this)" class="galleryImage" src="../img/gallery/<?php echo $images[3] ?>.png" alt="Jedi Cycle Sport Gallery Image">
</div>
</div>
See the image source for how I used the PHP.
As you have images available within $images array. So make a foreach loop inside html div.
<div class="col-md-12">
<?php foreach ($images as $image): ?>
<div class="col-md-3 galleryImg">
<img onclick="myfunction(this)" class="galleryImage" src="../img/gallery/<?php echo $image; ?>.png" alt="Jedi Cycle Sport Gallery Image">
</div>
<?php endforeach; ?>
</div>
Update
This is for if you want only four divs inside a parent div over and over again.
<?php
$i = 0;
foreach ($images as $image):
if ($i % 4 == 0) {
echo '<div class="col-md-12">';
}
echo '<div class="col-md-3 galleryImg">';
echo '<img onclick="myfunction(this)" class="galleryImage" src="../img/gallery/' . $image .'" alt="Jedi Cycle Sport Gallery Image">';
echo '</div><!-- outputs child div -->';
$i++;
if ($i % 4 == 0) {
echo '</div> <!-- outputs parent div -->';
}
endforeach;
if ($i % 4 != 0) {
echo '</div> <!-- outputs parent div-->';
}
Simply use the foreach. Use the code as follows
<div class="col-md-12">
<?php foreach($images as $image) {?>
<div class="col-md-3 galleryImg">
<img onclick="myfunction(this)" class="galleryImage" src="../img/gallery/<?php echo $image ?>.png" alt="Jedi Cycle Sport Gallery Image">
</div>
<?php } ?>
</div>
Instead of manually adding divs, you can simply use a foreach loop like this:
<div class="col-md-12">
<?php
foreach($images as $image){
?>
<div class="col-md-3 galleryImg">
<img onclick="myfunction(this)" class="galleryImage" src="../img/gallery/<?php echo $image; ?>.png" alt="Jedi Cycle Sport Gallery Image">
</div>
<?php
}
?>
</div>

Display multiple images from database to bootstrap carousel

I have a lot of images in a DB and I want to display it in a bootstrap carousel using php.
[Problem]
I am rookie with php so I hit the wall. Let me explain with code.
<div class="carousel-inner">
<div class="item active">
<div class="row">
<div class="col-md-3"><img src="http://placehold.it/250x250" alt="Image" style="max-width:100%;"></div>
<div class="col-md-3"><img src="http://placehold.it/250x250" alt="Image" style="max-width:100%;"></div>
<div class="col-md-3"><img src="http://placehold.it/250x250" alt="Image" style="max-width:100%;"></div>
<div class="col-md-3"><img src="http://placehold.it/250x250" alt="Image" style="max-width:100%;"></div>
</div><!--.row-->
</div><!--.item-->
<?php
$pdo = connect();
// display the list of all members in table view
$sql = "SELECT * FROM filmovi";
$query = $pdo->prepare($sql);
$query->execute();
$list = $query->fetchAll();
?>
<div class="item">
<div class="row">
<?php foreach($list as $rs) { ?>
<div class="col-md-3"><img src="assets/img/movies/<?php echo $rs['slika'] ?>" alt="Image" style="max-width:100%;"></div>
<?php } ?>
</div><!--.row-->
</div><!--.item-->
</div>
As you can see, carousel shows 4 images at once and another 4 and so on. In a foreach loop as it is now I get all my images at once and item active is empty.
I need to get 4 by 4 images from the DB to carousel but don't know which way to go.
You need to specify a counter that will check after each 4 iterations, to create a new <div class="item">...</div> element.
<?php
$counter = 0; //Set the counter to zero
foreach ($list as $single_list){
if($counter % 4 == 0) { // On every fourth iteration create a new item and row DIV
echo '<div class="item"><div class="row">';
}
... your code to output the images ...
if($counter % 4 == 0) {
echo '</div></div>'; // Close the row and item DIV
}
$counter++;
}
If you have problems implementing the code, or you don't understand what I've done here, don't hesitate to ask.

Foreach 4 items use a div, and then every other 4 items repeat another div

I am trying to create an image carousel, but that requires 2 seperate divs for the scroller, one active one, and then another which is not active but can be scrolled to...
This is what I'm trying to acomplish:
//the first 4 of a foreach
<div class="item active">
<img src="<?php $images[$i]; ?>">
<img src="<?php $images[$i]; ?>">
<img src="<?php $images[$i]; ?>">
<img src="<?php $images[$i]; ?>">
</div>
//the next 4 of a foreach
<div class="item">
<img src="<?php $images[$i]; ?>">
<img src="<?php $images[$i]; ?>">
<img src="<?php $images[$i]; ?>">
<img src="<?php $images[$i]; ?>">
</div><!-- /item -->
//now after every 4 repeat the <div class="item"> with the images inside, </div>
This is my foreach
<?php $i = 1; foreach(explode(",",$this->product->images) as $images): ?>
On each 4 I don't want the 4 divs to be repeated on the img inside the div.
I'm not sure how I will accomplish this?
Thanks to the posters. but now it's showing 5 instead of 4?
<div class="item active">
<?php $i=1 ; foreach(explode( ",",$this->product->images) as $images): ?>
<div data-target="#carousel" data-slide-to="<?php echo $i; ?>" class="thumb">
<img src="<?php echo Config::get('URL') .'images/products/'.System::escape($this->product->username). '/item' .System::escape($this->product->id) .'/'. System::escape($images); ?>"></div>
<?php if ($i % 4==0 ) {
echo '</div><div class="item">';
echo '<div data-target="#carousel" data-slide-to="'.$i. '" class="thumb"><img src="'.Config::get( 'URL') . 'images/products/'.System::escape($this->product->username). '/item' .System::escape($this->product->id) .'/'. System::escape($images).'"></div>';
} $i+=1; endforeach; ?>
</div>
<!-- /item -->
You can use the modulo operation to check, if your counter variable is the multiple of 4:
if ($i % 4 == 0) {
echo '</div><div>';
}
You just need to make sure that your foreach actually increases the value of $i on every iteration.
Alternatively you could reset $i back to 0 inside of the if body, so it always counts only to 4 and never higher. But I prefer the modulo solution, because that way you have an actual counter variable, not a fake one. :)
You can use Simple approach , add at start and also closing at the end like
<?php
$i=1;
foreach($arrayItems as $single){
if($i%4 == 0){
?>
<div class="Special">
<?php
}
?>
<div class="regular">Regular content</div>
<?php
if($i%4 == 0){
?>
</div>
<?php
}
$i+=1;
}
?>

Simple php logic

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.

Categories