php foreach loop implode every 3 loop - php

I need to make a gallery. I have images from 01 to 09. I need to divide them on foreach. The first div must have an active class. And we need to repeat the div every 3 image. I need exactly this html:
<div class="item active">
<div class="row">
<div class="col-sm-4">
<div class="col-item">
<div class="photo">
<img src="01.jpg" class="img-responsive" alt="a" />
</div>
</div>
</div>
<div class="col-sm-4">
<div class="col-item">
<div class="photo">
<img src="02.jpg" class="img-responsive" alt="a" />
</div>
</div>
</div>
<div class="col-sm-4">
<div class="col-item">
<div class="photo">
<img src="03.jpg" class="img-responsive" alt="a" />
</div>
</div>
</div>
</div>
</div>
...
<div class="item">
<div class="row">
<div class="col-sm-4">
<div class="col-item">
<div class="photo">
<img src="07.jpg" class="img-responsive" alt="a" />
</div>
</div>
</div>
<div class="col-sm-4">
<div class="col-item">
<div class="photo">
<img src="08.jpg" class="img-responsive" alt="a" />
</div>
</div>
</div>
<div class="col-sm-4">
<div class="col-item">
<div class="photo">
<img src="09.jpg" class="img-responsive" alt="a" />
</div>
</div>
</div>
</div>
</div>
But my code doesn't work. I don't know how to complete this...
Here is my PHP code:
<div id="carousel-example-generic" class="carousel slide hidden-xs" data-ride="carousel">
<!-- Wrapper for slides -->
<div class="carousel-inner">
<?php
$countedimage = 0;
$counteddiv = 0;
$count = count($images_bottom);
for ($x = 0; $x <= $count; $x++) {
$countedimage++;
$counteddiv++;
if($countedimage == '4') {
$countedimage = 0;
?>
<div class="item<?php if($counteddiv != '1') { ?> active<?php } ?>">
<div class="row">
<div class="col-sm-4">
<div class="col-item">
<div class="photo">
<img src="<?php echo $images_bottom[$x]['popup']; ?>" class="img-responsive" alt="a" />
</div>
</div>
</div>
</div>
</div>
<?php } ?>
<?php } ?>
</div>
</div>

Firstly, lets start with grouping your images into multiples of three.
// An arbitrary list of image file names (used for clarity).
$images = ['image-1.jpg', 'image-2.jpg', 'image-3.jpg', 'image-4.jpg', 'image-5.jpg', 'image-6.jpg', 'image-7.jpg', 'image-8.jpg', 'image-9.jpg'];
// Separate images into groups of three.
$images = array_chunk($images, 3);
Next we need to look at the output. So taking your example HTML:
<div id="carousel-example-generic" class="carousel slide hidden-xs" data-ride="carousel">
<div class="carousel-inner">
<?php // Loop over each group of three images ?>
<?php foreach ($images as $g => $group): ?>
<div class="item <?= ($g == 0) ? 'active' : '' ?>">
<div class="row">
<?php // Loop over each image in group ?>
<?php foreach ($group as $i => $image): ?>
<div class="col-sm-4">
<div class="col-item">
<div class="photo">
<img src="<?= $image ?>" class="img-responsive" alt="a" />
</div>
</div>
</div>
<?php endforeach ?>
</div>
</div>
</div>
<?php endforeach ?>
</div>
</div>

So, you basically want to print this portion for every item:
<div class="col-sm-4">
<div class="col-item">
<div class="photo">
<img src="<?php echo $src ?>" class="img-responsive" alt="a"/>
</div>
</div>
</div>
And you only want to print this portion every 3rd time, around groups of 3 of the above chunks:
<div class="item active">
<div class="row">
</div>
</div>
Instead of trying to keep track of 2 separate counts as you are doing, you can simplify things by using the modulus operator % along with the index of your loop $x.
This is really useful when you want to repeat something at certain intervals. Essentially, you can repeat something every $z number of times by doing something like:
if($totalCount % $z == 0)
Example:
for ($x = 1; $x < 50; $x++) {
if ($x % 5 == 0) echo $x . '<br>';
}
// results:
// 5
// 10
// 15
// 20
// 25
// 30
// 35
// 40
// 45
In your case, something like this would work:
<?php for ($x = 0; $x < count($images_bottom); $x++) {
$src = $images_bottom[$x]['popup'];
$item_active = $x==0 ? 'item active' : 'item'; // only use 'item active' first one
if ($x % 3 == 0) { // only display every 3rd time?>
<div class="<?php echo $item_active ?>">
<div class="row">
<?php } ?>
<div class="col-sm-4">
<div class="col-item">
<div class="photo">
<img src="<?php echo $src ?>" class="img-responsive" alt="a"/>
</div>
</div>
</div>
<?php if ($x % 3 == 2 || $x == count($images_bottom)-1){ // only display after every 3rd time (after items 0, 1, 2) or on last one ?>
</div>
</div><?php } ?>
<?php } ?>

Related

Dynamically add active class to an acf group

How Can i add dynamically active class in this code? I have tried using $i=0 in nested if loop with $i== at end of nested is loop. but it seems not working.
<div class="tab-content mt-5">
<?php
if (have_rows('whats_included', 2959)) {
while (have_rows('whats_included', 2959)) {
the_row();
if (have_rows('list_data', 2959)) {
while (have_rows('list_data', 2959)) {
the_row();
?>
<div id="<?= the_sub_field('id'); ?>" class="container tab-pane active"><br>
<div class="row align-items-center justify-content-center">
<div class="col-lg-8">
<div class="talabat-clone-wrap radius5">
<img src="<?= bloginfo('template_directory') ?>/assets/images/talabat-app/triangle.png" class="triangle-shape" alt="">
<p><?= the_sub_field('text'); ?></p>
</div>
</div>
</div>
</div>
<?php
}
}
}
}
?>
</div>
I have solved the issue,thank you.
<div class="tab-content mt-5">
<?php
$i = 0;
if (have_rows('whats_included', 2959)) {
while (have_rows('whats_included', 2959)) {
the_row();
if (have_rows('list_data', 2959)) {
while (have_rows('list_data', 2959)) {
the_row();
?>
<div id="<?= the_sub_field('id'); ?>" class="container tab-pane <?php if ($i++ == 1) : echo 'active';endif; ?>"><br>
<div class="row align-items-center justify-content-center">
<div class="col-lg-8">
<div class="talabat-clone-wrap radius5">
<img src="<?= bloginfo('template_directory') ?>/assets/images/talabat-app/triangle.png" class="triangle-shape" alt="">
<p><?= the_sub_field('text'); ?></p>
</div>
</div>
</div>
</div>
<?php
}
}
}
}
?>
</div>

Dynamic div close in php while loop

code i have written below is working fine but at the end of the looping the div is not closed its still opening a loop
<div class="carousel-inner">
<div class="item active">
<div class="row">
<?php
$recent_projects_sql="SELECT * from recent_projects where service_type='upholstery'";
$recent_projects_conn=mysql_query($recent_projects_sql) or die(mysql_error());
$i=0; $split=0;
while($projects=mysql_fetch_array($recent_projects_conn)) {
$i++;
?>
<div class="col-sm-3">
<div class="col-item" style="">
<div class="photo-shadow"></div>
<div class="photo">
<img src="admin/assets/images/uploads/projects/<?php echo $projects['attachment1']; ?>" alt="User one">
</div>
<div class="info">
<div class="name">
<?php echo $projects['service_name']; ?>
</div>
<div class="degination">
<?php echo $projects['sub_title']; ?>
</div>
<div class="buttons">
<a class="btn btn-theme ripple-effect" href="#">View More</a>
</div>
<div class="clearfix"></div>
</div>
</div>
</div>
<?php
$split++;
if ($split % 4 == 0){
echo '</div></div><div class="item"><div class="row">';
}
}
?>
</div>
</div>
The Div has splited very well but in end of the loop div has not been closed. Thats only the problem please provide me the help to sort out the problem
When I inspect the element the last loop will show at the given result as follows:
<div class="col-sm-3">
<div class="col-item">
<div class="photo-shadow"></div>
<div class="photo">
<img src="admin/assets/images/uploads/projects/1557301934.jpg" alt="User one">
</div>
<div class="info">
<div class="name">UPHOLSTERY</div>
<div class="degination">UPHOLSTERY</div>
<div class="buttons">
<a class="btn btn-theme ripple-effect" href="#">View More</a>
</div>
<div class="clearfix"></div>
</div>
</div>
</div>
</div></div><div class="item"><div class="row">
I want to remove the two opening div's as dynamically. How can i set this to remove opened div's at then end of the looping
I just took a quick look and it looks like you are not closing the "carousel-inner" div
<div class="carousel-inner">
<div class="item active">
<div class="row">
<?php
$recent_projects_sql = "SELECT * from recent_projects where service_type='upholstery'";
$recent_projects_conn = mysql_query( $recent_projects_sql ) or die( mysql_error() );
$i = 0;
$split = 0;
while ( $projects = mysql_fetch_array( $recent_projects_conn ) ) {
$i ++;
?>
<div class="col-sm-3">
<div class="col-item" style="">
<div class="photo-shadow"></div>
<div class="photo">
<img src="admin/assets/images/uploads/projects/<?php echo $projects['attachment1']; ?>"
alt="User one">
</div>
<div class="info">
<div class="name">
<?php echo $projects['service_name']; ?>
</div>
<div class="degination">
<?php echo $projects['sub_title']; ?>
</div>
<div class="buttons">
<a class="btn btn-theme ripple-effect" href="#">View More</a>
</div>
<div class="clearfix"></div>
</div>
</div>
</div>
<?php $split ++;
if ( $split % 4 == 0 ) {
echo '</div></div><div class="item"><div class="row">';
}
}
?>
</div>
</div>
Add a Boolean check for the execution of loop, such as $check = true;, add this within the loop.
after the loop add this
if($check){
echo " </div></div>";
}
That's because at the end of iteration (in case of mod 4 and even without it), you keep 2 divs opened
echo '</div></div><div class="item"><div class="row">';

how to view data from database with static images in php

When I try to view multiple data from database my static images are also showing multiple time. I try to use foreach and while with i++ but both time I get the same result.
How to view one image but multiple data.
<div class="row">
<?php
// set array
$array = array();
while($row=mysqli_fetch_assoc($result1))
{
// add each row returned into an array
$array[] = $row;
}
// debug:
foreach ($array as $arrays)
{
?>
<div class="col-sm-3">
<div class="address_item">
<img src="img/soul-icon/address-1.png" alt="">
<h3>Address</h3>
<h4><?php echo $arrays['address']; ?></h4>
</div>
</div>
<div class="col-sm-3">
<div class="address_item">
<img src="img/soul-icon/call.png" alt="">
<h3>Mobile </h3>
<h4><?php echo $arrays['mobile_no']; ?></h4>
</div>
</div>
<div class="col-sm-3">
<div class="address_item">
<img src="img/soul-icon/address-2.png" alt="">
<h3>Phone</h3>
<h4><?php echo $arrays['phone_no'];?> </h4>
</div>
</div>
<div class="col-sm-3">
<div class="address_item">
<img src="img/soul-icon/address-3.png" alt="">
<h3>Email</h3>
<h4><?php echo $arrays['email']; ?></h4>
</div>
</div>
</div>
<?php } ?>
</div>
It's normal because your images are in the foreach.
Remove your first foreach
Make function like
foreach($array as $value){
echo $value;
}
and put them below your images.
example with your code : (but better make functions for all the foreach)
<div class="row">
<?php
// set array
$array = array();
while($row=mysqli_fetch_assoc($result1))
{
// add each row returned into an array
$array[] = $row;
}
// debug:
?>
<div class="col-sm-3">
<div class="address_item">
<img src="img/soul-icon/address-1.png" alt="">
<h3>Address</h3>
<h4><?php foreach ($array as $arrays)
{
echo $arrays['mobile_no'];
}?></h4>
</div>
</div>
<div class="col-sm-3">
<div class="address_item">
<img src="img/soul-icon/call.png" alt="">
<h3>Mobile </h3>
<h4>//same here</h4>
</div>
</div>
<div class="col-sm-3">
<div class="address_item">
<img src="img/soul-icon/address-2.png" alt="">
<h3>Phone</h3>
<h4>//same here </h4>
</div>
</div>
<div class="col-sm-3">
<div class="address_item">
<img src="img/soul-icon/address-3.png" alt="">
<h3>Email</h3>
<h4>//same here</h4>
</div>
</div>
</div>
</div>
Define the Image field outside the loop:
<div class="row">
<?php
// set array
$array = array();
while($row=mysqli_fetch_assoc($result1))
{
// add each row returned into an array
$array[] = $row;
}
// debug:
<div class="col-sm-3">
<div class="address_item">
<img src="img/soul-icon/address-1.png" alt="">
<h3>Address</h3>
</div>
</div>
<div class="col-sm-3">
<div class="address_item">
<img src="img/soul-icon/call.png" alt="">
<h3>Mobile </h3>
</div>
</div>
<div class="col-sm-3">
<div class="address_item">
<img src="img/soul-icon/address-2.png" alt="">
<h3>Phone</h3>
</div>
</div>
<div class="col-sm-3">
<div class="address_item">
<img src="img/soul-icon/address-3.png" alt="">
<h3>Email</h3>
</div>
</div>
foreach ($array as $arrays)
{
?>
<div class="col-sm-3">
<div class="address_item text-center">
<h4><?php echo $arrays['address']; ?></h4>
</div>
</div>
<div class="col-sm-3">
<div class="address_item text-center">
<h4><?php echo $arrays['mobile_no']; ?></h4>
</div>
</div>
<div class="col-sm-3">
<div class="address_item text-center">
<h4><?php echo $arrays['phone_no'];?> </h4>
</div>
</div>
<div class="col-sm-3">
<div class="address_item text-center">
<h4><?php echo $arrays['email']; ?></h4>
</div>
</div>
</div>
<?php } ?>
</div>
Simplest put not the most beatiful solution is to just check if its your "header" line and adding those images at first run.. you can also add the if around your images instead of dublication to much. but you will get the idea
<div class="row">
<?php
// set array
$array = array();
while($row=mysqli_fetch_assoc($result1))
{
// add each row returned into an array
$array[] = $row;
}
// debug:
$i = 0;
foreach ($array as $arrays)
{
?>
if($i == 0){
//first line
<div class="col-sm-3">
<div class="address_item">
<img src="img/soul-icon/address-1.png" alt="">
<h3>Address</h3>
<h4><?php echo $arrays['address']; ?></h4>
</div>
</div>
<div class="col-sm-3">
<div class="address_item">
<img src="img/soul-icon/call.png" alt="">
<h3>Mobile </h3>
<h4><?php echo $arrays['mobile_no']; ?></h4>
</div>
</div>
<div class="col-sm-3">
<div class="address_item">
<img src="img/soul-icon/address-2.png" alt="">
<h3>Phone</h3>
<h4><?php echo $arrays['phone_no'];?> </h4>
</div>
</div>
<div class="col-sm-3">
<div class="address_item">
<img src="img/soul-icon/address-3.png" alt="">
<h3>Email</h3>
<h4><?php echo $arrays['email']; ?></h4>
</div>
</div>
}else{
<div class="col-sm-3">
<div class="address_item">
<h3>Address</h3>
<h4><?php echo $arrays['address']; ?></h4>
</div>
</div>
<div class="col-sm-3">
<div class="address_item">
<h3>Mobile </h3>
<h4><?php echo $arrays['mobile_no']; ?></h4>
</div>
</div>
<div class="col-sm-3">
<div class="address_item">
<h3>Phone</h3>
<h4><?php echo $arrays['phone_no'];?> </h4>
</div>
</div>
<div class="col-sm-3">
<div class="address_item">
<h3>Email</h3>
<h4><?php echo $arrays['email']; ?></h4>
</div>
</div>
</div>
}
<?php } ?>
</div>

How to use foreach in codeigniter

I am trying to get values from database table with use of foreach , as i am getting result but it is showing all value from table as i am using it for slide show how can i get 1 detail at single time... This is my code
<div class="col-md-10">
<div class="carousel slide" data-ride="carousel">
<div class="carousel-inner">
<?php
$result_array = $this->db->get('vm_feedback')->result_array();
foreach ($result_array as $key => $v) {?>
<div class="item active">
<div class="col-md-12">
<div class="testimonial-text">
<p style="color:black;"><?php echo $v['feed_desc'];?></p>
<span class="testimonial-by"><?php echo $v['feed_name'];?></span>
</div>
</div>
</div>
<?php } ?>
</div>
</div>
</div>
Result i am geting:
what i need :
<?php $i = 1;
foreach ($result_array as $key => $v ) {?>
<div class="testimonial-item <?php if($i == 1) echo "active"; ?>">
<span> <img src="<?php echo base_url().'backend_assets/media/feedimg/'.$v['feed_img'];?>" class="testimonial-img img-responsive img-circle" alt="image"></span>
<div class="testimonial-text">
<p ><?php echo $v['feed_desc'];?></p>
<span class="testimonial-by"><div class="text-center"><?php echo $v['feed_name'].'<br>'.$v['feed_des'];?></div></span>
</div>
</div> <?php $i++; } ?>
If you want only one record you can go with row_array();
<?php
$v = $this->db->get('vm_feedback')->row_array();
<div class="item active">
<div class="col-md-12">
<div class="testimonial-text">
<p style="color:black;"><?php echo $v['feed_desc'];?></p>
<span class="testimonial-by"><?php echo $v['feed_name'];?></span>
</div>
</div>
</div>
You have to put a condition on as currently all the items div are active so it is not looking as you want. So use another variable to add active at once.
<div class="col-md-10">
<div class="carousel slide" data-ride="carousel">
<div class="carousel-inner">
<?php
$result_array = $this->db->get('vm_feedback')->result_array();
$i = 1;
foreach ($result_array as $key => $v) {?>
<div class="item <?php if($i == 1) echo "active"; ?>">
<div class="col-md-12">
<div class="testimonial-text">
<p style="color:black;"><?php echo $v['feed_desc'];?></p>
<span class="testimonial-by"><?php echo $v['feed_name'];?></span>
</div>
</div>
</div>
<?php $i++; } ?>
</div>
</div>
</div>
This will give you the exact result what you want.
Thanks

how to break design into pieces after x loops

I have a loop which I want to take this behaver
first to add item div then loop into this div to add 6 items And after complete 6 items it will close this div and start again with next six items until finish
my current code like this :
<?php $x = 0; ?>
<?php foreach ($files as $lst) { ?>
<div class="item <?= ($x == 1 ? 'active' : ''); ?>">
<div class="row">
<div class="col-xs-2">
<a href="<?= base_url() ?>global/uploads/<?= $lst['FileName'] ?>"
rel="prettyPhoto[gallery1]">
<?php if ($lst['FileName'] != "" && file_exists(PUBPATH . "global/uploads/" . $lst['FileName'])) { ?>
<img src="<?= thumb($lst['FileName'], 150, 150); ?>" class="img-responsive">
<?php } else { ?>
<img src="<?= base_url() ?>global/site/data/1.jpg" class="img-responsive">
<?php } ?>
</a>
</div>
</div>
</div>
<?php $x++;
} ?>
but i want final structure to be like this
<div class="item active">
<div class="row">
<div class="col-xs-2">
<img src="data/1.jpg" class="img-responsive">
</div>
<div class="col-xs-2">
<img src="data/1.jpg" class="img-responsive">
</div>
<div class="col-xs-2">
<img src="data/1.jpg" class="img-responsive">
</div>
<div class="col-xs-2">
<img src="data/1.jpg" class="img-responsive">
</div>
<div class="col-xs-2">
<img src="data/1.jpg" class="img-responsive">
</div>
<div class="col-xs-2">
<img src="data/1.jpg" class="img-responsive">
</div>
</div>
</div>
<div class="item">
<div class="row">
<div class="col-xs-2">
<img src="data/1.jpg" class="img-responsive">
</div>
<div class="col-xs-2">
<img src="data/1.jpg" class="img-responsive">
</div>
<div class="col-xs-2">
<img src="data/1.jpg" class="img-responsive">
</div>
<div class="col-xs-2">
<img src="data/1.jpg" class="img-responsive">
</div>
<div class="col-xs-2">
<img src="data/1.jpg" class="img-responsive">
</div>
<div class="col-xs-2">
<img src="data/1.jpg" class="img-responsive">
</div>
</div>
</div>
You should clean the code a bit, instead of filtering inside the loop the array should contain the files and urls you want to enlist.
That would separate HTML from your PHP code making it much easier to read.
$files = [
'data/1.jpg', // can make an array as well, ['src'=>'data/1.jpg', 'href => 'link.com']
'data/2.jpg',
'data/3.jpg',
'data/4.jpg',
'data/5.jpg',
'data/6.jpg',
'data/7.jpg',
];
foreach(array_chunk($files, 6) as $row){
echo '<div class="item"><div class="row">';
foreach($row as $col){
echo '<div class="col-xs-2">';
echo '<img src="'.$col.'" class="img-responsive">';
//echo '<img src="'.$col['src'].'" class="img-responsive">';
echo '</div>';
}
echo '</div></div>';
}

Categories