php - Modifying the first item of the array -


i have modify first item of array, failed, manage code:

<div class="carousel-inner">                             <?php if($results) {         $x= 1;         foreach ($results $data)  { $x++; ?>             <div class="item <?php if($x == 1) { echo 'active';} ?>">                 <img alt="" src="<?=base_url('uploads/'.$data->file)?>">             </div>         <?php }     } ?>                         </div> 

assuming these result:

<div class="item active">     <img alt="" src="http://localhost/ideal_visa/uploads/fd5fa6cfbe64c8b68664ddbf0546d81b.jpg"> </div> <div class="item">     <img alt="" src="http://localhost/ideal_visa/uploads/c43c064de5cb9e751c723eb9791f2107.jpg"> </div> <div class="item">     <img alt="" src="http://localhost/ideal_visa/uploads/e3137475f6330d52e9cd9c6fc5efba1e.jpg"> </div> 

i have add active on first item of array.

you don't need $x this.you can use foreach key => value , check if it's first index

<?php if($results) {     foreach ($results $key=>$data)  { ?>         <div class="item <?php echo ($key == 0)? 'active':''; ?>">             <img alt="" src="<?=base_url('uploads/'.$data->file)?>">         </div>   <?php } } ?>  

Comments

Popular posts from this blog

javascript - jQuery: Add class depending on URL in the best way -

caching - How to check if a url path exists in the service worker cache -

Redirect to a HTTPS version using .htaccess -