php - How to proper user in-line concatenation? -


i'm working on pagination of website , got point need highlight number of page on.

i'm having hard time semantic @ line bellow, more what's between **. whole code bellow.

please note ** not part of code.

could put line together.

    <? $saida .='<li><a'.**if $i == $_get['page'] echo 'class="active"';**.'href="?page='.$i.'">'.$i.'</a></li>'; ?> 

here whole code

<?php  function montatexto($texto){     $texto = str_replace("<hr>","<hr />",$texto);     $texto = str_replace("<hr/>","<hr />",$texto);     $texto = explode('<hr />',$texto);      $x = 0;     foreach($texto $s =>$v){         $x++;         $texto[$x] = $v;     }      if (!empty($_get['page'])){         $saida = $texto[$_get['page']];     } else {         $saida = $texto[1];     }      if ($x > 1){         $saida .= '<div class="pagination bottom">               <p>-'.strip_tags($categoria['titulo_topo']).'-</p>               <ul>';                       ($i = 1; $i <= $x; $i++){               $saida .='<li><a'. if $i == $_get['page'] echo 'class="active"'; .'href="?page='.$i.'">'.$i.'</a></li>';         }         $saida .='</ul>               <p><a href="?page='.($x).'">&gt;&gt;</a></p>               </div> ';     }     return $saida; }  ?> 

you need ternary conditional operator:

$saida .='<li><a'. ($i == $_get['page'] ? ' class="active"' : '') .' href="?page='.$i.'">'.$i.'</a></li>'; 

you can read more here.

edit fixed bug :)

test:

$ cat paging.php  <?php     $_get['page'] = '5';      ($i = 0; $i < 10; $i++) {         $saida .='<li><a'. ($i == $_get['page'] ? ' class="active"' : '') .' href="?page='.$i.'">'.$i.'</a></li>' . "\n";     }      var_dump($saida); $ php paging.php  string(345) "<li><a href="?page=0">0</a></li> <li><a href="?page=1">1</a></li> <li><a href="?page=2">2</a></li> <li><a href="?page=3">3</a></li> <li><a href="?page=4">4</a></li> <li><a class="active" href="?page=5">5</a></li> <li><a href="?page=6">6</a></li> <li><a href="?page=7">7</a></li> <li><a href="?page=8">8</a></li> <li><a href="?page=9">9</a></li> " 

Comments

Popular posts from this blog

ios - iPhone/iPad different view orientations in different views , and apple approval process -

java Extracting Zip file -

C# WinForm - loading screen -