Jump to content

PHP line o pick only the newest entries


mccorvic

Recommended Posts

Practicing with PHP and trying to rig up a poor man's blog. How can I make a string that will show only the newest five entries I make using a MySQL table?

 

This is what I've kinda come up with:

 

<?php

while($row=mysqli_fetch_array($data)) {

if($row['id'] < 5) {

echo '' . $row['post'] . '

';

echo 'Posted: ' . $row['time'] . ' by ' . $row['name'] . '';

}}

?>

 

But obviously this will only show the first 5 entires I've made and nothing else. Like I said, I'm just trying to get a proof of concept thing going, so I'm not worried about making the best and cleanest bog php ever.

 

Suggestions?

Link to comment
Share on other sites

In that case, you'll need to do two things -- first off, order the posts by date, and then limit the results.

 

Order by: http://www.tizag.com/mysqlTutorial/mysqlorderby.php

Limit: http://dev.mysql.com/doc/refman/6.0/en/select.html

 

Limit description:

"The LIMIT clause can be used to constrain the number of rows returned by the SELECT statement. LIMIT takes one or two numeric arguments, which must both be nonnegative integer constants (except when using prepared statements).

 

With two arguments, the first argument specifies the offset of the first row to return, and the second specifies the maximum number of rows to return. The offset of the initial row is 0 (not 1):

 

SELECT * FROM tbl LIMIT 5,10; # Retrieve rows 6-15"

 

You'd need to do something like this: "$query = "SELECT * FROM blog order by date limit 5";"

 

For reference, I usually suggest http://www.tizag.com/mysqlTutorial/. They have some good articles introducing readers to basic PHP/MySQL concepts.

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
×
×
  • Create New...