0

Get all the results $index = 1;

Why $index not added 1 Although I wrote $index++; Before closing foreach?

$index = 1;
foreach ($months as $month) {

if( $index = '1' ){ ?>

   <div class="col-md-12">
     <div class="card border-info">
       <a href="vote.php?id=<?php echo $month['id']; ?>"><?php echo $month['month_name']; ?></a>
     </div>
   </div>
<?php }

elseif( $index = '2' || $index = '5' || $index = '8' || $index = '11' ){  ?>
   <div class="col-md-3">
     <div class="card border-primary">
       <a href="vote.php?id=<?php echo $month['id']; ?>"><?php echo $month['month_name']; ?></a>
     </div>
   </div>
<?php }

elseif( $index = '3' || $index = '6' || $index = '9' || $index = '12' ){  ?>
   <div class="col-md-3">
     <div class="card border-success">
       <a href="vote.php?id=<?php echo $month['id']; ?>"><?php echo $month['month_name']; ?></a>
     </div>
   </div>
<?php }

elseif( $index = 4 || $index = 7 || $index = 10 || $index = 13 ){  ?>
   <div class="col-md-3">
     <div class="card border-danger ">
       <a href="vote.php?id=<?php echo $month['id']; ?>"><?php echo $month['month_name']; ?></a>
     </div>
   </div>
<?php }



$index++;
}
Zakaria Acharki
  • 63,488
  • 15
  • 64
  • 88

2 Answers2

3

$index = '1'

will set 1 to $index, you are assigning values instead of checking them. It has to be like

if($index == '1')
Danyal Sandeelo
  • 11,068
  • 6
  • 37
  • 64
0

The main problem comes from the following line :

if( $index = '1' ){ ?>

The = must be == :

if( $index == '1' ){ ?>

Else you're initializing the $index to 1 in every iteration.

Zakaria Acharki
  • 63,488
  • 15
  • 64
  • 88