What are some best practices for organizing and displaying user-generated content on a website using PHP?

Organizing and displaying user-generated content on a website using PHP requires proper sorting and filtering to ensure a seamless user experience. One best practice is to store user-generated content in a database and retrieve it using SQL queries based on relevant criteria such as date, popularity, or user preferences. Additionally, implementing pagination can help manage large amounts of content and improve loading times.

// Connect to the database
$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "your_database";

$conn = new mysqli($servername, $username, $password, $dbname);

// Retrieve user-generated content sorted by date
$sql = "SELECT * FROM user_content ORDER BY date_created DESC";
$result = $conn->query($sql);

// Display content with pagination
$limit = 10;
$page = isset($_GET['page']) ? $_GET['page'] : 1;
$start = ($page - 1) * $limit;

$sql .= " LIMIT $start, $limit";
$result = $conn->query($sql);

while($row = $result->fetch_assoc()) {
    echo "<div>{$row['content']}</div>";
}

// Pagination links
$total_pages = ceil($result->num_rows / $limit);
for ($i=1; $i <= $total_pages; $i++) {
    echo "<a href='?page=$i'>$i</a>";
}