NewUser
NewUser

Reputation: 13333

Php add class dynamically to the first and last block

I have an array like this

$products_array = array('test product', 'test new product', 'test lipsum', 'test lorem',  ....);

I just got the values from array like this

echo '<ul>';
foreach( $products_array as $product_array ) {
    echo '<li>$product_array</li>';
}
echo '</ul>';

But here I want something dynamic. I want to add class name according to the value set by the user. Lets say user wants to show 5 lists in a row then the markup will be like this

<ul>
    <li class="first">test product</li>
    <li>test new product</li>
    <li>test lipsum</li>
    <li>test lorem</li>
    <li class="last">test update</li>
    <li class="first">test new product</li>
    <li>test a product</li>
    <li>test new lipsum</li>
    <li>test lorem</li>
    <li class="last">test new update</li>   
 </ul>

So here you can see at last means after each 5 post its adding class last and it is adding class first to the first list and after the fifith list blocks. So in this when user will set $class = 3 then it will add last class to the third block and the first will be added to the first and the list block just after the 3rd, 6th, 9th etc

I have done like this

$last = '4' //set 4. so for 4th,8th,12th it will add class last. and for 1st, 5th, 9th it will add class first

 echo '<ul>';
 $i = 0;
 $count = count($products_array);
foreach( $products_array as $product_array ) {
$i++;
$class = '';
if( $i == $count ) {
$class = 'last';
}
    echo '<li class='.$class.'>$product_array</li>';
}
echo '</ul>';

But its not working. So can somone tell me how to do this? Any help and suggestions will be really appreciable. Thanks

Upvotes: 2

Views: 1197

Answers (2)

Phil
Phil

Reputation: 164795

Use a modulus to determine the class to add. The logic goes...

  1. When the remainder is 0, we are on the last item of each group (the nth of n)
  2. When the remainder is 1, we are on the first item of each group (the 1st of n)
  3. Otherwise, we are somewhere in the middle

For example

$last = 4;
?>
<ul>
<?php
foreach ($products_array as $index => $product) :
switch(($index + 1) % $last) { // array indexes are 0-based so add 1
    case 0 :
        $class = 'last';
        break;
    case 1 :
        $class = 'first';
        break;
    default :
        $class = '';
}
?>
<li class="<?= $class ?>"><?= htmlspecialchars($product) ?></li>
<?php endforeach ?>
</ul>

eval.in demo

Upvotes: 3

Tobias Golbs
Tobias Golbs

Reputation: 4616

This should do the trick. You could also use modulus, but i am not quite sure how it behaves with small perPage-settings.

//your perPage-setting, change this for more elements per page
$perPage = 3;
$count = count($products_array);
//loop over elements
for($i = 1; $i <= $count; $i++) {
    $className = "";
    if($i / $perPage == 0) {
        $className = "last";
    } else if((floor($i / $perPage) * $perPage + 1 == $i)
        $className = "first";
    echo '<li class='.$className.'>'.$product_array[$i-1].'</li>';
}

Upvotes: 0

Related Questions