Jump to content

How to display all fields in tforst row and only selected fields after


AdRock

Recommended Posts

Is this possible and how?

 

I want to get the last 5 records out of a table order desc and I want the last record entered to display the title, date and content so it's like the intro.  The other 4 records I would like just the title to be displayed.

 

$sql="SELECT title, content, DATE_FORMAT(time, '%D %M %Y') as date, alternate FROM news WHERE archived='n' ORDER BY date desc LIMIT 1";

// Perform a query getting back a MySQLResult object
$result = $db->query($sql);

// Iterate through the results
while ($row = $result->fetch()) {
    echo ( '<h4 class="news-heading"><a href="http://www.jackgodfrey.org.uk/news/latest-news/1">'.$row['title'].'</a></h4><p style="text-align:left; font-size: 10px;margin-top:0 !important;">Date added: '.$row['date'].'</p>' );
    echo ('<p>'.nl2br(ShortenText(str_replace('&','&',$row['content']))).'<a href="http://www.jackgodfrey.org.uk/news/latest-news/1">Read More</a></p>'); 
}

 

This is what i have at the moment which displays only the first record.  What i tried is have another query which gets all ther records and have another while loop underneath the first while loop which only displays the titles but it's outputting the title of the already displayed article.  IS there a way of getting 5 records before the last record inserted?

here's how you grab only the 5....

$sql="SELECT title, content, DATE_FORMAT(time, '%D %M %Y') as date, alternate FROM news WHERE archived='n' ORDER BY date desc LIMIT 5";

 

not sure about the other one.

Simple if/else logic -

 

<?php
// execute query here

$first_line = TRUE; // initialize a flag before your loop

// Iterate through the results
while ($row = $result->fetch()) {
    if($first_line)
    {
        // this is the first line, do any special processing needed for the first line here

        $first_line = FALSE; // set the flag false
    } else {
       // this is not the first line, do normal processing here
   }
}
?>

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.