mccorvic Posted August 10, 2009 Report Share Posted August 10, 2009 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? Quote Link to comment Share on other sites More sharing options...
falkencreative Posted August 10, 2009 Report Share Posted August 10, 2009 What is the SQL statement that you are using to get the blog data from the database? ($data). That will be the part you need to modify. Quote Link to comment Share on other sites More sharing options...
mccorvic Posted August 10, 2009 Author Report Share Posted August 10, 2009 What is the SQL statement that you are using to get the blog data from the database? ($data). That will be the part you need to modify. $query = "SELECT * FROM blog"; $data=mysqli_query($dbc, $query); Quote Link to comment Share on other sites More sharing options...
falkencreative Posted August 10, 2009 Report Share Posted August 10, 2009 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. Quote Link to comment Share on other sites More sharing options...
mccorvic Posted August 10, 2009 Author Report Share Posted August 10, 2009 That did it. Thanks so much! Quote Link to comment Share on other sites More sharing options...
Recommended Posts
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.