Here is a php file that i at the moment it displays the latest news on the site but I want to add 5 should my query be?


    $stmt = $db->query('SELECT postID, postTitle, postURL, postDesc, postDate FROM posts where postid=(SELECT max(postID) from posts)');
    while($row = $stmt->fetch()){
    $output = ' <div class="panel-group panel-group-lists" id="accordion">';
    $output .= '<div class="panel"> <div class="panel-heading"> <h4 class="panel-title"> <a data-toggle="collapse" data-parent="#accordion" href="#';
    $output .= $row['postID'];
    $output .= '">';
    $output .= $row['postTitle'] ;
    $output .= 
    '</a> </h4> </div> <div id="';
    $output .= $row['postID'];
    $output .= '" class="panel-collapse collapse in"> <div class="panel-body"> <p>Posted on ';
    $output .= date('jS M Y H:i:s', strtotime($row['postDate']));
    $output .= $row['postDesc'];
    $output .= '<p><a href="'.$row['postURL'].'"><button type="button" class="btn btn-primary col-md-offset-8">Read More</button></a></p>';
    $output .= '</div> </div></div>';

    echo $output;
    $output .= '</div>';

Recommended Answers

All 7 Replies

Member Avatar for diafol
'SELECT postID, postTitle, postURL, postDesc, postDate FROM posts ORDER BY postID DESC LIMIT 6'

Assuming you wanted 6 records in total (the original + 5 more as stated)

sir, it gave me multiple copies and for some reason my collapsible shit aint working..what I want is to query the latest posts on my earlier query gives the latest post....what I wanted was to display the..lets 5 latest post..(I used max(postID) since it increments everytime I add a post..therefore it gives the latest post) I want something similar to this but instead gives the top 5 latest posts..

i think theres something wrong in the code..ima try find what it is...its not the query cause I researched on it and it should give what I want

Member Avatar for diafol

sir, it gave me multiple copies

Sorry I don't understand
I'll run through this:

SELECT postID, postTitle, postURL, postDesc, postDate FROM posts ORDER BY postID DESC LIMIT 5

The order clause:


Should order the resultset by postID, starting with the largest value, working downwards.

The limit clause:


Should limit the resultset to 5 records, in this case the 5 latest records when both clauses are used together.

MAX(...) when used on an autoincrement field will return the largest value (therefore the latest record if used in a subquery or WHERE clause), but in a simple query, you wouldn't expect more than one record.

and for some reason my collapsible shit aint working

I have no idea what this means either.

after putting the code it duplicates:

Member Avatar for diafol

It's because of this line:

$output = ' <div class="panel-group panel-group-lists" id="accordion">';

Every iteration of your loop is overwritten by the next record.

Do you want an accordion for each individual story/record or one for all of them?

I get it..thanks sir!!I know what I have to do

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.