Membership is FREE – with unlimited access to all features, tools, and discussions. Premium accounts get benefits like banner ads and newsletter exposure. ✅ Signature links are now free for all. 🚫 No AI-generated (LLM) posts allowed. Share your own thoughts and experience — accounts may be terminated for violations.

UK domains
being liquidated

digitaljunkyard.com
buy now

Select latest article and display differently?

Status
Not open for further replies.
Joined
Jun 1, 2010
Posts
548
Reaction score
2
At the moment I use the code below to display all articles from my database in small, rectangular boxes with a small thumbnail on one side. However, I would like to display the latest article as a large picture with the other info underneath. I thought it would be (and probably is to non-noobs) simple to do but I can't workout how to grab the latest article. I can do everything styling wise, it's just getting the latest article. The articles all have an incremental id. Any help would be much appreciated!

<ul id="headlines">

<?php foreach ( $results['articles'] as $article ) { ?>

<li>

<?php if ( $imagePath = $article->getImagePath( IMG_TYPE_THUMB ) ) { ?>

<a href=".?action=viewArticle&amp;articleId=<?php echo $article->id?>"><img class="articleImageThumb" src="<?php echo $imagePath?>" alt="Article Thumbnail" /></a>

<div id="shortsummary">

<h2><a href=".?action=viewArticle&amp;articleId=<?php echo $article->id?>"><?php echo htmlspecialchars( $article->title )?></a>
<span class="pubDate"><?php echo date('j F', $article->publicationDate)?></span>
</h2>

<p class="summary">

<?php } ?>
<?php echo htmlspecialchars( $article->summary )?>
</p></div><?php } ?>

</li>

</ul>
 
Could set a count such $imcount =1 before the start off your loop then. before the end of the loop $imcount++

Then around your image display a different class with an if statement when $imcount is >2
 
Selecting the latest article would be a mysql thing, a quick and easy way is something like

SELECT id, text, whatevr FROM `articles` order by id desc limit 1;

that would select just the last one, changing 1 to 5 would select the last 5 in reverse order.
 
I understand what both of you are saying, but could you give an example how I would integrate either answer with what I already have (can't emphasize my noobness enough)? Thanks
 
Was on the tablet last night so couldn't put it in your code this should work

Code:
<ul id="headlines">
<?php 
$imcount = 1;
foreach ( $results['articles'] as $article ) { ?>
<li>
<?php if ( $imagePath = $article->getImagePath( IMG_TYPE_THUMB ) ) { ?>
<a href=".?action=viewArticle&amp;articleId=<?php echo $article->id?>"><img class="<?php if ($imcount < 2) { echo "articleImageThumbLarge";} else {  echo "articleImageThumb"; } ?>" src="<?php echo $imagePath?>" alt="Article Thumbnail" /></a> 
<div id="shortsummary">
<h2><a href=".?action=viewArticle&amp;articleId=<?php echo $article->id?>"><?php echo htmlspecialchars( $article->title )?></a>
<span class="pubDate"><?php echo date('j F', $article->publicationDate)?></span>
</h2>
<p class="summary">
<?php } ?>
<?php echo htmlspecialchars( $article->summary )?>
</p></div>
<?php 
$imcount++;
} ?>
</li>

In your css create an articleImageThumbLarge class and apply the styling you want to it
 
Status
Not open for further replies.
Top Bottom