Domain Manage

Select latest article and display differently?

Discussion in 'Scripts and Coding' started by theonemanyuri, Mar 10, 2014.

Thread Status:
Not open for further replies.
  1. theonemanyuri

    theonemanyuri Active Member

    Joined:
    Jun 2010
    Posts:
    556
    Likes Received:
    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>
     
  2. Domain Forum

    Acorn Domains Elite Member

    Joined:
    1999
    Messages:
    Many
    Likes Received:
    Lots
     
  3. AssetDomains United Kingdom

    AssetDomains Well-Known Member

    Joined:
    Feb 2010
    Posts:
    2,951
    Likes Received:
    52
    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
     
  4. Skinner

    Skinner Well-Known Member

    Joined:
    Jul 2008
    Posts:
    4,325
    Likes Received:
    81
    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.
     
  5. theonemanyuri

    theonemanyuri Active Member

    Joined:
    Jun 2010
    Posts:
    556
    Likes Received:
    2
    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
     
  6. AssetDomains United Kingdom

    AssetDomains Well-Known Member

    Joined:
    Feb 2010
    Posts:
    2,951
    Likes Received:
    52
    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
     
  7. theonemanyuri

    theonemanyuri Active Member

    Joined:
    Jun 2010
    Posts:
    556
    Likes Received:
    2
    Awesome, that works lovely job. Thank you both very much!
     
  8. Skinner

    Skinner Well-Known Member

    Joined:
    Jul 2008
    Posts:
    4,325
    Likes Received:
    81
    You haven't posted the SQL part to implement it, but look for something which looks like the select statement I posted, and should be easy to modify that :)
     
  9. theonemanyuri

    theonemanyuri Active Member

    Joined:
    Jun 2010
    Posts:
    556
    Likes Received:
    2
Thread Status:
Not open for further replies.

Share This Page