Aaron Alfonso February 2016

Modifying the first item of the array

I have to modify the first item of the array, however I failed, but I manage to do a code:

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

Assuming that I will get 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 only have to add active on the first item of the array.

Answers


ameenulla0007 February 2016

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

you need to increment the x value after the iteration

OR

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

you can just utilize associative key of the array $data and change the condition to $x==0


Nabin Kunwar February 2016

You don't need to do extra $x for this.You can use foreach with key => value and check if it's first index

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


Ben February 2016

Problem

You have put $x=1 before the foreach, then increment it at the start.

That means when you show the first image, $x==2.


Solution

You can either start with $x=0 or move the line $x++ to the end of the loop:

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


n00dl3 February 2016

You need to increment $x after the iteration

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

Post Status

Asked in February 2016
Viewed 1,917 times
Voted 5
Answered 4 times

Search




Leave an answer