mardi 5 mai 2015

Display different value for each foreach index

I have css class that I don't want to add into every li tag. It should be added into one li and next li should be empty and so on...

I tried to add

for example

<?php 

 $css_class = 'class="pull_rigt"';

 echo "<ul>";
 foreach($posts as $key => $value ) {

   echo "<li $css_class>";
    echo $value['data'];
   echo "</li>";

}
echo "<ul>";
?>

the output should be

<li class="pull_rigt">
    data
<li>
<li>
    data
</li>

I tried matching power of two numbers in every foreach index but couldn't figure out the math.

<?php 

 $css_class = 'class="pull_rigt"';

 echo "<ul>";

 $i = 0;
 foreach($posts as $key => $value ) {

   <li <?php echo ($i & ($i - 1)) == 0 ? $css_class; ?>

    echo $value['data'];

   echo "</li>";

   $i++;
}
echo "<ul>";
?>

Aucun commentaire:

Enregistrer un commentaire