What is the best practice for displaying only the latest 5 news items on a PHP website while still allowing visitors to access older news articles?
To display only the latest 5 news items on a PHP website while still allowing visitors to access older news articles, you can retrieve the latest 5 news items from your database and display them on the homepage. Additionally, you can provide a link to a separate page where visitors can access older news articles.
<?php
// Connect to your database
$connection = mysqli_connect("localhost", "username", "password", "database");
// Retrieve the latest 5 news items
$query = "SELECT * FROM news ORDER BY date DESC LIMIT 5";
$result = mysqli_query($connection, $query);
// Display the latest news items
while ($row = mysqli_fetch_assoc($result)) {
echo "<h2>" . $row['title'] . "</h2>";
echo "<p>" . $row['content'] . "</p>";
}
// Provide a link to access older news articles
echo "<a href='older_news.php'>View Older News</a>";
// Close the database connection
mysqli_close($connection);
?>
Keywords
Related Questions
- What are the best practices for updating data in one database based on data from another database in PHP?
- How can the code provided be optimized for better performance and readability in a PHP Tic Tac Toe game?
- What are some potential issues with PHP scripts when dealing with multiple database tables and joining them?