Home > Net >  I'm create dynamic year button using php for loop in shorthand ,How do i put active class in fo
I'm create dynamic year button using php for loop in shorthand ,How do i put active class in fo

Time:12-24

I am trying this here is my code all things are working but active "class" not working. I'm used short hand loop

$cur_year = date('Y');

$url = 'url_here';

$active = "($get_year == $i)?'active':''";

for ($i=2020; $i<=$cur_year; $i  ) echo '<a href="'.$url.'/'.$i.'" >'.$i.'</a>';
     

Thanks

CodePudding user response:

You need to correct variable name in comparison code as well as its syntax too

Also put comparison code inside loop

<?php

$cur_year = date('Y');

$url = 'url_here';
for ($i=2020; $i<=$cur_year; $i  ) {
    $active = ($cur_year == $i)?'active':''; //its $cur_year
    echo '<a href="'.$url.'/'.$i.'" >'.$i.'</a>';
}

Output : https://3v4l.org/ItEWY

Shorthand for loop : https://3v4l.org/PRiVt

  •  Tags:  
  • php
  • Related