Mohanish
Mohanish

Reputation: 269

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:enter image description here

what i need :enter image description here

Upvotes: 1

Views: 118

Answers (3)

Leozz_den
Leozz_den

Reputation: 85

             <?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++;  } ?>

Upvotes: 1

Sumit Sharma
Sumit Sharma

Reputation: 24

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

Upvotes: 0

Giri Annamalai M
Giri Annamalai M

Reputation: 809

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>

Upvotes: 0

Related Questions