How to use foreach in codeigniter - php

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

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>

Wrap div using 2 wrapper inside array loop

I am having difficulty while having the dual container div inside my for each array loop. Getting "two-half" div after each "wrap" div. But need to wrap "two-half" div with 2 "wrap" inside.
Expected:
<div class="item">
<div class="two-half">
<div class="wrap">1</div>
<div class="wrap">2</div>
</div>
<div class="two-half">
<div class="wrap">3</div>
<div class="wrap">4</div>
</div>
</div>
<div class="item">
<div class="two-half">
<div class="wrap">5</div>
<div class="wrap">6</div>
</div>
<div class="two-half">
<div class="wrap">7</div>
<div class="wrap">8</div>
</div>
</div>
Code:
<div class="item">
<?php
$count = 1;
$array = array(1,2,3,4,5,6,7,8);
foreach($array as $item) { ?>
<div class="two-half">
<div class="wrap">
<?php echo $item; ?>
</div>
</div>
<?php if ($count%4 == 0) { ?>
</div>
<?php } $count++; } ?>
Please help me to get the expected output. Thanks!
Use array_chunk to split an array into chunks.
<?php
$array = range(1, 8);
$half = array_chunk($array, 2); // chunk for halfs
$item = array_chunk($half, 2); // chunk for item
$count = 1;
?>
{{-- Create div-item --}}
<?php foreach ($item as $parent) {?>
<div class="item">
{{-- Create div-half --}}
<?php foreach ($parent as $half) {?>
<div class="two-half">
{{-- Create div-wrap --}}
<?php foreach ($half as $item) {?>
<div class="wrap"><?php echo $count; ?></div>
<?php $count++;?>
<?php }?>
</div>
<?php }?>
</div>
<?php }?>
Result
<div class="item">
<div class="two-half">
<div class="wrap">1</div>
<div class="wrap">2</div>
</div>
<div class="two-half">
<div class="wrap">3</div>
<div class="wrap">4</div>
</div>
</div>
<div class="item">
<div class="two-half">
<div class="wrap">5</div>
<div class="wrap">6</div>
</div>
<div class="two-half">
<div class="wrap">7</div>
<div class="wrap">8</div>
</div>
</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">';

Add a Row After 6 Records Automatically

Here Is The Code
<div class="row">
<div class="col-md-9 column">
<div class="services-listing">
<?php if(!empty($data)):
foreach ($data as $rows ) : ?>
<div class="service">
<div class="service-img"><span><img src="dashboard/uploads/<?php echo $rows['img'];?>" alt="" /></div>
<div class="service-detail">
<h3><?php echo $rows['name'];?></h3>
</div>
</div>
<!-- Service -->
<?php endforeach; else : echo "No Record Found Against This Services"; endif;?>
</div>
<!-- Service Listing -->
</div>
i want to start design from <div class="row"> not from <div class="service"> now it repeating this div i want new <div class="row"> genrated after every 6 records
<div class="row">
<div class="col-md-9 column">
</div>
</div>
Looking for something like this, let me know if it works for you
<?php
$start = 6;
$end = 1;
if(!empty($data)){
foreach ($data as $rows ) {
if ($start % 6 == 0) { ?>
<div class="row">
<div class="col-md-9 column">
<?php } ?>
<div class="services-listing">
<div class="service">
<div class="service-img"><span><img src="dashboard/uploads/<?php echo $rows['img'];?>" alt="" /></div>
<div class="service-detail">
<h3><?php echo $rows['name'];?></h3>
</div>
</div>
<!-- Service -->
</div>
<?php if ($end % 6 == 0) { ?>
</div>
</div>
<?php } $start++; $end++;
}
}
else{ echo "No Record Found Against This Services"; }
?>
you need to wrap div row with foreach first and then:
<?php $i = 0; ?>
<?php if(!empty($data)):
foreach ($data as $rows ) : ?>
<?php $i++; ?>
<div class="row">
<div class="col-md-9 column">
<div class="services-listing">
<div class="service">
<div class="service-img"><span><img src="dashboard/uploads/<?php echo $rows['img'];?>" alt="" /></div>
<div class="service-detail">
<h3><?php echo $rows['name'];?></h3>
</div>
<!-- Service -->
</div>
<!-- Service Listing -->
</div>
<!-- col-md-9 column -->
</div>
<!-- Row -->
</div>
<?php if($i%6==0):?>
<div class="row">
<div class="col-md-9 column">
</div>
</div>
<?php endif; ?>
<?php endforeach; else : echo "No Record Found Against This Services"; endif;?>
just please re-check all divs structure to be sure whether all of them are closed

How to divide categories in rows of three columns

I have a foreach loop which retrieves the subcategories of the current category.
<?php $_categories = $this->getCurrentChildCategories(); ?>
<?php foreach ($_categories as $_category): ?>
<div class="item">
<?php echo $this->htmlEscape($_category->getName()) ?>
</div>
<?php endforeach; ?>
I want to divide the results in rows of three columns. The desired format is then:
<div class="row">
<div class="item">
content
</div>
<div class="item">
content
</div>
<div class="item">
content
</div>
</div>
<div class="row">
<div class="item">
content
</div>
<div class="item">
content
</div>
<div class="item">
content
</div>
</div>
...
---------------EDIT----------------
My code is now as follows. How and where do i close the row div?
<?php $_categories = $this->getCurrentChildCategories(); ?>
<?php foreach ($_categories as $_category): ?>
<?php if ($i % 3 == 0) { ?>
<div class="row">
<?php } ?>
<div class="item">
<h2><?php echo $this->htmlEscape($_category->getName()) ?> »</h2>
</div>
<?php $i++; endforeach; ?>
---------------EDIT 2----------------
Getting closer. This is the result:
<div class="category-list">
<div class="row"></div>
<div class="row">
<div class="item">
content
</div>
<div class="item">
content
</div>
<div class="item">
content
</div>
</div>
<div class="row">
<div class="item">
content
</div>
<div class="item">
content
</div>
<div class="item">
content
</div>
</div>
<div class="row">
<div class="item">
content
</div>
<div class="item">
content
</div>
<div class="item">
content
</div>
</div>
</div>
How do I get rid of the empty row?
The code is now as follows:
<div class="category-list">
<?php
$i=0;
echo '<div class="row">';
$_categories = $this->getCurrentChildCategories();
foreach($_categories as $_category):
{
if($i %3 ==0)
{ ?>
</div>
<div class="row">
<div class="item">
<h2><?php echo $this->htmlEscape($_category->getName()) ?> »</h2>
</div>
<? }
else
{ ?>
<div class="item">
<h2><?php echo $this->htmlEscape($_category->getName()) ?> »</h2>
</div>
<? }
$i++;
}
endforeach;
?>
</div>
---------------EDIT 3----------------
Solved by hiding the empty row via CSS.
You have to introduce az iterator (?) variable for example $i = 0
if ($i % 3 == 0) { //modulo
// use for whatever you want
}
In every cycle (maybe at the end of it, it depends on your real solution) you have to increment $i (++$i);
Hope this helps
<?php
$i=0;
echo"<div class="row" >
foreach($category as $cat)
{
if($i %3 ==0)
{
echo"</div><div class='row'> <div class='item'> Content </div>";
}
else
{
echo"<div class='item'> Content </div>";
}
$i++;
}
echo"</div>";
?>

Categories