DeJu
DeJu

Reputation: 47

Add Attribute to First Element in For Loop PHP

I'm wondering how to add an "act" class to the first element in the following for loop?

if (!isset($this->params['page'])){
                $this->params['page'] == 1;
            }
             for($i=1; $i< $news_cont['response']['pager']['total_page']+1; $i++) {
                 if (isset($this->params['page']) && $this->params['page'] == $i){
                     $act = 'class="act"';
                 }else{
                     $act = '';
                 }
             echo '<a href="/' . $this->params['lang'] . '/' . $this->params['action'] . '/' . $i . '" ' . $act . '>' . $i . '</a>';
             }

I need it when $this->params['page'] is not isset the first for cycle element have act class, else class "act" is defined to the element matching $this->params['page'] Thanks for any advice.

Upvotes: 3

Views: 139

Answers (1)

Greg Motyl
Greg Motyl

Reputation: 2545

  for($i=1; $i< $news_cont['response']['pager']['total_page']+1; $i++) {
    echo '<a href="/' . $this->params['lang'] . '/' . $this->params['action'] . '/' . $i . '" ' . ((($i == 1) && (!isset($this->params['page'])) ) ? 'class="act"' : '') . '>' . $i . '</a>';
  }

Upvotes: 1

Related Questions