-1

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>
Don't Panic
  • 41,125
  • 10
  • 61
  • 80
Sean Ford
  • 23
  • 4
  • 1
    you'd need some code to detect when you are outputting the final post. quick/dirty way would be to get a row count, and see if you're outputting the final row. or fetch/cache into an array, then loop on the array, and you'll know then how big the array is. – Marc B Jul 19 '16 at 19:54

3 Answers3

1

This is the assumption that latest is your first result which based on the query seems to be the case.

You need a conditional and a way to know when to trim so outside your while loop add:

$shouldTrim = false; //Sine the latest is your first item you do not want to trim first

Then at the top of your while loop

$content = "";
if($shouldTrim){//Anything but the latest
   $content = trimNewsContent($row[Content]);
} else { //This is your latest
   $content = $row[Content]; //output to full content
   $shouldTrim = true; //Set this to true going forward will trim
}

Now you can do

<p>".$content."</p>
nerdlyist
  • 2,842
  • 2
  • 20
  • 32
1

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']));
Community
  • 1
  • 1
trincot
  • 317,000
  • 35
  • 244
  • 286
0

It's as Marc B and nerdlyist said. You need to know where is the last post. One way of doing it :

$query2 = mysql_query("SELECT count(*) AS nb_news FROM news");
$nbNews = mysql_fetch_array($query2);

With that you know how much news you have.

Before you while :

$i = 0;

Just after your while, increment $i : $i++;

Then test if $i is equal to the number of news, if so, you are at the last post:

if($i == $nbNews['nb_news'])
{
    $content = $row[Content];
}
else
{
    $content = trimNewsContent($row[Content]);
}
Nordine
  • 824
  • 7
  • 25