What is the best way to track the number of comments for each news article in PHP?

To track the number of comments for each news article in PHP, you can create a database table to store the comments and link them to the corresponding article using a foreign key. You can then query the database to count the number of comments for each article.

// Assuming you have a database connection established
// Create a table to store comments with a foreign key referencing the article
// Table structure: comments (id, article_id, comment_text)

// Query to count the number of comments for each article
$query = "SELECT article_id, COUNT(*) AS comment_count FROM comments GROUP BY article_id";

// Execute the query
$result = mysqli_query($connection, $query);

// Loop through the result and display the comment count for each article
while ($row = mysqli_fetch_assoc($result)) {
    echo "Article ID: " . $row['article_id'] . " - Comment Count: " . $row['comment_count'] . "<br>";
}