Jump to content

How to repeat an output until the last row then assign a different style to it?


Neil_D

Recommended Posts

Hey guys, bascially I'm developing a simple news script, and part of it has a "quick news" section, which will display the latest 3 (or any number I want) news items, each seperated by a line (<hr> in the example below).

 

What I want to acheive is the top 2 news items have a set LI class assigned to them, and then the 3rd LI in the list to have a different style attached to it (one without a bottom border basically), so it'll bascially repeate out the normal <li> until it hits the last item to be output, at which point it uses a LI with a class attached to it.

 

What I don't know how to do, is tell the PHP that once it's out putted 2 LI's, then the 3rd LI is to have a different class on it.

 

eg

 

<li>News Item 1</li>
<li>News Item 2</li>
<li class="last">News Item 3</li>

 

I'm pretty new to PHP so not sure how to achieve this and would appreicate any help you can offer.

 

Here's my code so far...

 

<?php
$latestnews_sql = "SELECT * FROM news ORDER BY date DESC LIMIT 3";
$latestnews_query = mysql_query($latestnews_sql) or die (mysql_error());
$list_latestnews = mysql_fetch_assoc ($latestnews_query);
?>
<ul>
<?php do { ?>
<li><p><strong><?php echo $list_latestnews['title']; ?></strong></p>
<?php echo substr($list_latestnews['body'],0,80)."...";?> <a href="news-detail.php?ID=<?php echo $list_latestnews['ID']; ?>">view more</a></p>
<p><span style="font-size:13px; font-style:italic;"><?php echo $list_latestnews['author']; ?> - <?php echo date("l, jS F Y", strtotime($list_latestnews['date'])); ?></span></p>
<hr />
</li>
<?php } while ($list_latestnews = mysql_fetch_assoc ($latestnews_query)) ?>
</ul>

Link to comment
Share on other sites

I've been given another snippet of code from somewhere which fundamentally does what I want...

 

if($list_latestnews['ID'] == 2) { $li = '<li class="last">'; } else { $li = '<li>'; }?>

 

However, as the ID's change as more news is added, the 2nd item in the news list isn't always going to be ID==2, how do I change this to become the 2nd output entry, rather than a set ID?

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.