I would like to have the latest post show full rather than trim it, but then the other posts be trimmed. I am unsure how to do so, so here I am! Here's some code!
<div id="news">
<?php
$query=mysql_query("SELECT NewsID, Title, Content, Author,
date_format(Date, '%m/%d/%y') AS Date
FROM news ORDER BY NewsID DESC LIMIT 2");
while($row=mysql_fetch_assoc($query)){
echo "<div class=\"news_item\">
<div class=\"title\">
<a href=\"article.php?newsID=$row[NewsID]\">$row[Title]</a>
</div>
<div class=\"date\">
Posted: $row[Date] by $row[Author]
</div>
<div class=\"news_content\">
<p>".trimNewsContent($row[Content])."</p>
</div>
</div>";
}
?>
</div>
First of all: don't use mysql_
functions, but mysqli_
or PDO
. mysql_
functions were deprecated several years ago and are no longer supported in the most recent versions of PHP.
There are several ways described in the answers to this question on how to detect the last row of a result set.
I will not give a solution based on deprecated mysql_
functions; you should first convert your code.
If you go for mysqli_
and get a connection in $con, then you can do this:
$query = mysqli_query($con, "SELECT NewsID, Title, Content, Author, date_format(Date, '%m/%d/%y') AS Date FROM news ORDER BY NewsID DESC LIMIT 2");
// fetch all rows
$rows = mysqli_fetch_all($query); // pass MYSQLI_ASSOC as 2nd argument if needed
foreach($rows as $i => $row) {
$content = $i == count($rows)-1 ? $row['Content'] : trimNewsContent($row['Content']));
echo ".... // etc
$content
....// etc
";
}
If the goal is to treat the first record differently, then just change the condition accordingly:
$content = $i == 0 ? $row['Content'] : trimNewsContent($row['Content']));