[英]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. 在while循环中从数据库中回显结果,以便我可以一次回显5个结果。 Currently have put a standard string into the loop in order to test.
目前已经将标准字符串放入循环中以便进行测试。 The
<li>
elements have a border-bottom
attribute in their stylings. <li>
元素的样式具有border-bottom
属性。 Is it possible to have the last result echoed (number 5) to have another class applied that would rule out the border? 是否可以让最后的结果回显(数字5)以应用另一个将排除边界的类?
$i = 1;
while($i <= 5) {
// On the 5th, change the class here to rule out the border-bottom.
echo "<li>jQuery &HTML5 audio player.</li>";
$i++;
}
So, somewhere in there, throw in an if statement? 那么,在那里的某个地方,抛出if语句? 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 &HTML5 audio player.</li>";
}
else
{
echo "<li class='fluffeh'>jQuery &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 &HTML5 audio player.</li>";
}
else
{
echo "<li class='fluffeh'>jQuery &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 &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. 与其总是使用数字5作为占位符,不如计算数组中的元素可能会更好,如果是最后一个,则省略该行。
$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. 您已经有一个计数器,您的变量$ i。 The variable $i which you count to know when reach 5th loop to end the loop.
您计数的变量$ i知道何时到达第5个循环以结束循环。
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. 您也可以将它用于if-或其他语句,以便在第三元素或secend元素或其他东西中执行某些操作,例如,当$ i计数为4时,它应该输出其他类或执行函数或其他操作。
All you have to do is to asking, when the 5 loop is reached insert an class: 您所要做的就是询问何时到达5循环时插入一个类:
<?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 &HTML5 audio player.</li>" : "<li class='someclass'>jQuery &HTML5 audio player.</li>";
$i++;
}
Being "someclass" the style you want to apply to the last li. 作为“某类”您要应用于最后一个li的样式。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.