Forum Moderators: coopster

Message Too Old, No Replies

Style Most Recent Post Differently

Style Most Recent Post Differently

         

quitano

8:37 pm on Dec 26, 2008 (gmt 0)

10+ Year Member



Hello everyone, 1st time post... long time reader...

This site has been an amazing source for me over the years. I ALWAYS found my answers and never need to post. However this one I cannot find and i know you guys can help.

I want to style my most recent news article differently then the other articles on the page. I am using PHP/MYSQL.

function get_news_feature($where='1=1'){
return db_query("SELECT * FROM `newsfeature` WHERE $where ORDER BY `id` DESC LIMIT 3");

foreach(get_news_feature () as $news){

if($news['here is where i need help'] <= ("")){
echo "<div class='newsphoto'>".$news['photo']."</div>";
echo "<div class='headlinefeature'><a href='/feature.php?id=" . $news[id] ." '>".$news['subject']."</div></a>";
echo "<div class='datefeature'>".$news['date']."</div>";
echo "<div class='authorfeature'>".$news['author']."</div>";
echo "<div class='summaryfeature'>".$news['summary']."</div>";
echo "<div class='readmoref'><a href='/feature.php?id=" . $news[id] ." '>Read More</div></a>";

}else{ echo "<div class='newsphoto2'>".$news['photo']."</div>";
echo "<div class='headlinefeature2'><a href='/feature.php?id=" . $news[id] ." '>".$news['subject']."</div></a>";
echo "<div class='datefeature2'>".$news['date']."</div>";
echo "<div class='authorfeature2'>".$news['author']."</div>";
echo "<div class='summaryfeature2'>".$news['summary']."</div>";
echo "<div class='readmoref2'><a href='/feature.php?id=" . $news[id] ." '>Read More</div></a>";
}
}

.... the "here is where i need help" is the most recent news article in the database. And of course where i need the help..i think. So i want to style the most recent article differently on the same page as the other articles.

Thanks everyone

coopster

9:01 pm on Dec 26, 2008 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



Welcome to WebmasterWorld, quitano.

Is that your actual code or is the first part just a little snippet of your "get_news_feature" function? I'm assuming it is because you need to close that function before you start your

if
and
foreach
logic loops.

coopster

9:06 pm on Dec 26, 2008 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



One way to approach loops such as this is to initialize a variable that will hold all the echo output as opposed to echo each line and then echo it when the loop is complete. Something like this ...
$out = ''; // initialize 
foreach(get_news_feature () as $news){
if (!$out) {
$out .= "<div class='newsphoto'>".$news['photo']."</div>";
// etc, etc, etc.
} else {
// other formatting
}
}
echo $out;

quitano

9:08 pm on Dec 26, 2008 (gmt 0)

10+ Year Member



this is just a snippet. Everything is working perfect. I just cannot figure out how to style the "most recent" posted article differently then the rest.

coopster

9:31 pm on Dec 26, 2008 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



I posted a second message while you were replying to my first response. Does it make sense?

quitano

9:36 pm on Dec 26, 2008 (gmt 0)

10+ Year Member



i think i follow what your saying. I am sort new to php/mysql. I dont follow you here, "hold all the echo output as opposed to echo each line"

is there anyway to get the most recent article id here:

if($news[''] <= ("")){

quitano

1:10 am on Dec 27, 2008 (gmt 0)

10+ Year Member



hey man thanks, i figured it our with the code you gave me..... i got it to work. I don't how..lol, but its working.

coopster

2:50 pm on Dec 27, 2008 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



is there anyway to get the most recent article id

Well, I guess the most recent article would indeed have the highest id number if it was an automatic incrementing column. And when you ORDER BY that column in DESC order you would have the latest values first in your result set. You could also have a date or timestamp in your table and ORDER BY <date field here> DESC.

The example logic I showed was how to recognize if the first one had been read in yet. If there is no formatted output on the first loop, we are on the first news article, otherwise we are on 2nd or greater news article(s).