Reputation: 366
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 &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
Upvotes: 0
Views: 312
Reputation: 33502
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++;
}
?>
Upvotes: 2
Reputation: 1
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.
Upvotes: 0
Reputation: 17019
<?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.
Upvotes: 1
Reputation: 109
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>';
Upvotes: 0
Reputation: 744
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>';
}
Upvotes: 0