简体   繁体   中英

PHP How to add specific class to last echoed <li> in while loop

Echoing out results from a database in a while loop so that I can echo 5 of them out at once. Currently have put a standard string into the loop in order to test. The <li> elements have a border-bottom attribute in their stylings. Is it possible to have the last result echoed (number 5) to have another class applied that would rule out the border?

$i = 1;
while($i <= 5) {
    // On the 5th, change the class here to rule out the border-bottom.
    echo "<li>jQuery &amp;HTML5 audio player.</li>";
    $i++;
}

So, somewhere in there, throw in an if statement? Sorry. This could be really simple, but it's been a long day

Can't you use a simple check inside the loop and output depending on your counter?

<?php
    $i = 1;
    while($i <= 5)
    {
        if($i<5)
        {
            echo "<li>jQuery &amp;HTML5 audio player.</li>"; 
        }
        else
        {
            echo "<li class='fluffeh'>jQuery &amp;HTML5 audio player.</li>"; 
        }
        // On the 5th, change the class here to rule out the border-bottom.
        $i++;
    }       
?>

Or if you wanted every fifth one different, you could do:

<?php
    $i = 1;
    while($i <= 10)
    {
        if($i%5!=0)
        {
            echo "<li>jQuery &amp;HTML5 audio player.</li>"; 
        }
        else
        {
            echo "<li class='fluffeh'>jQuery &amp;HTML5 audio player.</li>"; 
        }
        // On the 5th, change the class here to rule out the border-bottom.
        $i++;
    }       
?>
<?php
$i = 1;
while($i <= 5){
    $class = "";
    if($i===5)
        $class = " class=\"last\"";
    echo "<li$class>jQuery &amp;HTML5 audio player.</li>"; // On the 5th, change the class here to rule out the border-bottom.
    $i++;
}

of course that this is a blind answer to a closed question, you would do greater if you follow other people answers's advice.

Rather than always using the number five as a placeholder, it would probably be better to count the elements in the array, and if it's the last one, omit the line.

$elementsCount = count($elements);
for ($index = 0; $index < $elementsCount; ++$index) {
    $class = $index == $elementsCount - 1 ? 'lastElement' : 'standardElement';
    echo '
        <li class="', $class, '">', $elements[$index], '</li>';
}

You have already an counter, your variable $i. The variable $i which you count to know when reach 5th loop to end the loop.

Also you can use it for if- or other statments to do somethin in third or secend element or other stuff like, when $i counts 4 it should ouput other class or do a function or somenthing else.

All you have to do is to asking, when the 5 loop is reached insert an class:

<?php
  $class = null;
  $li_element = null;
  for($i=0;$i<=5;$i++)
  {
    if($i==5)
    {
      $class="last_element";
    }
    $li_element .= '<li '.$class.'>...</li>';
  }

  echo '<ul>'.$li_element.'</ul>';

If you don't want to add to much lines to your code you could also do something like this:

$i = 1;
while ($i<= 5) {
    echo ($i < 5) ? "<li>jQuery &amp;HTML5 audio player.</li>" : "<li class='someclass'>jQuery &amp;HTML5 audio player.</li>";
    $i++;
}

Being "someclass" the style you want to apply to the last li.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM