0

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

<?php 


    $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>';
?> 
2
Contributors
7
Replies
33
Views
2 Years
Discussion Span
Last Post by RonKevinT.Manuela
1
'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)

0

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 database...my earlier query gives the latest post....what I wanted was to display the..lets say...top 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..

0

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

1

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:

ORDER BY postID DESC    

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

The limit clause:

LIMIT 5

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.

Edited by diafol

1

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?

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.